Ad-Hoc 模式连接 iPhone

系统环境:

操作系统:Ubuntu 8.04 Hardy 或任何其他发行版本 GNOME 桌面环境,用到 NetworkManager
iPhone firmware 2.2^
已经越狱,开启WIFI,开启SSH服务。

适用情景:没有无线路由器或者可用无线网络,没有USB数据线。只有Laptop和内置无线网卡。如何与iPhone共享内容,比如要copy出刚拍的照片?

  1. Network Manager Applet - Create New Wireless Network
    不要尝试用Edit Connection去加或者创建Ad-Hoc,会导致无法连接到自己创建的Ad-Hoc网络,不确定是否为bug。
    Ad-Hoc

  2. Connection name: iPhone, Security - NONE

  3. Create之后自动会连接到此Ad-Hoc网络。

**注意,IPv4 Method默认是Share to other computers,这意味着有DHCP服务会给连接到此Ad-Hoc网络的客户端,包括iPhone分配IP,无须手动设置Laptop和iPhone的IP了!当然一定要手动设置也没问题。之前我一直是这么干的,因为真不知道Shared to other computers这么好使。
**

  1. iPhone无线网络连接中可以找到此网络,加入,自动会获得一个IP。

  2. Laptop端,用FileZilla的SFTP模式连接此IP,用户名密码和你SSH的是一样的。
    照片在/var/mobile/Media/DCIM下。

后面的不用教你了吧?

Mac OS X和Windows上大同小异。可以肯定的是,设置肯定比Linux下要简单;-)

关于iPhone使用笔记本网络连接
同样用Ad-Hoc实现iPhone共享Laptop的网络连接访问Internet肯定是没问题,只要网络设置正确,gateway,若需要HTTP proxy的设置好就可以了。我有每个月2G流量的3G连接,所以没有这样的需求。

笔记本使用iPhone的GRPS或者3G连接
没需求,暂时没尝试。能想到的方式:

同样用 Ad-Hoc 方式互相可以交互,笔记本通过 SSH 方式连接到 iPhone 并开启一个本地端口。至此,本地端口与iPhone的22端口之间有了一个经过SSH加密的通道。而浏览器可以使用这个通道通过iPhone的连接顺利访问网络。

英文版: https://sites.google.com/site/imterry/computer/tools/ssh

示意图来自车东的 blog
浏览器 <==secure tunnel==>ssh server <—> twitter.com

**Mac OS X **和 **Linux **直接命令行下用即可,当然也有 PuTTY 可用,Windows下当然极力推荐用 PuTTY 了。另外推荐 Mac 用户尝试 iTerm2 这个终端模拟器。

ssh -qTfnN -D 1080 batman.com

参数详解:

  • -q Quiet mode.
  • -T Disable pseudo-tty allocation.

-f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background.

  • -n Redirects stdin from /dev/null (actually, prevents reading from stdin). This MUST be used when ssh is running in the background.
  • -N Do not execute remote command
  • -D Specifies a local “dynamic” application level port forwarding.
  • -g Allows remote hosts to connect to local forwarded ports (share the socks5 proxy within the LAN/network).

或者用putty-tools包中的plink,写一个简单的脚本:

Linux/Mac OS X
plink -C -A -N username@sshserver -D 0.0.0.0:port

Windows
plink.exe -C -A -N username@sshserver -D 0.0.0.0:port

以上的效果和在PuTTY中SSH - Tunnels中选中Local ports accept connections from other hosts是一样的。

注:选Local ports accept connections from other hosts貌似就无法成功连接到SSH host并开启Tunnel了,推荐使用plink。
PuTTY

注:**-D 0.0.0.0表示与本机的所有网卡绑定,同一个LAN内所有的机器都可以通过设置此socks 5代理访问Web!**

参考:
http://www.chedong.com/blog/archives/001246.html
http://lifehacker.com/327066/use-your-iphones-internet-connection-on-your-laptop
实战SSH端口转发