OpenWrt路由器通过iPhone有线共享网络上网

2018年4月更新:

我自己的手机在openwrt上网速很慢,在电脑上又很快。应该不是被限速了,但是没找到原因。


 

三大运营商在学校争客户,手机卡开出了校内无限流量的条件。很开心,之前准备到东北大学的时候还担心校园网25G/月不够用,这就不用担心了。因为是手机卡上网,所以需要一个上网卡或者一个手机来上网络再共享网络给电脑/路由器。

方案一:使用上网卡共享。

这是我最开始认为稳妥的方法,便宜,方便。去闲鱼收了一个上网卡,50块。

到手后测试可以连电脑,也通过找资料,使得上网卡可以直接插在路由器上使用。

但是上网卡发热太高,导致网速变慢,延迟增高。这个办法就被否定了。

方案二:使用手机共享网络

用手机开热点是生活中经常办的事情。而且之前在学校也试过用iPhone连接数据线给电脑共享网络。

这里设备选择手头闲置的iPhone5s。

一、使用iPhone有线共享网络给电脑

讲解最简单的直接使用电脑共享iPhone的网络。

Windows平台需要安装iTunes,安装完之后,手机端将iPhone通过数据线连接到电脑,并在设置->个人热点->打开。电脑端在网络连接中就可以看到iPhone的网络,如果电脑网口还连接有网线,则禁用电脑网口。

Fedora直接插上手机就有了。(没太仔细研究)

二、使用OpenWrt路由器

Fedora都能使用iPhone共享的网络,我觉得OpenWrt肯定也可以,所以我就去搜索资料。发现可以,并且自己也尝试成功。下面将查到的资料总结一下。(其实一篇文章就讲清楚了,但是之前查了很多)

(一)安装OpenWrt依赖包

opkg update
opkg install kmod-usb-net-ipheth kmod-usb-net kmod-usb-ohci kmod-usb-uhci kmod-usb2 libimobiledevice-utils libplist-utils libusbmuxd-utils usbmuxd
kmod-usb-net-ipheth //这个是啥,查查
具体依赖包的作用见下面
Kernel modules  --->
    USB Support  --->
        <*> kmod-usb-net............... Kernel modules for USB-to-Ethernet convertors
        <*> kmod-usb-ohci............................... Support for OHCI controllers
        <*> kmod-usb-uhci............................... Support for UHCI controllers
        <*> kmod-usb2................................... Support for USB2 controllers

Utilities  --->
    <*> libimobiledevice-utils............ A library that talks to Apple devices.
    <*> libplist-utils............................. Apple property list converter
    <*> libusbmuxd-utils......................... USB multiplexing daemon utilies
    <*> usbmuxd.......................................... USB multiplexing daemon

 (二)配置usbmuxd

iPhone的USB tethering需要usbmuxd进程的运行。usbmuxd并不会自动运行,因此可以在OpenWrt终端手动启动,或制作以下开机自启动脚本,并通过命令使其自启动。[1]

使用命令新建/etc/init.d/usbmuxd脚本,并给脚本添加可执行权限。

touch /etc/init.d/usbmuxd
vi /etc/init.d/usbmuxd
chmod +x /etc/init.d/usbmuxd

下面是自启动脚本内容。

#!/bin/sh /etc/rc.common
# Copyright (c) 2011-2012 OpenWrt.org
START=99

stop() {
    killall usbmuxd
}

start() {
    if [ ! -d "/var/lib/lockdown" ];then
        mkdir -p /var/lib/lockdown
    fi

    ./usr/sbin/usbmuxd &
}

 脚本添加完成后,运行命令启动usbmuxd,并且添加自启动。

/etc/init.d/usbmuxd enable
/etc/init.d/usbmuxd start

(三)添加网络

在usbmuxd进程启动的前提下,使iPhone通过usb线连接路由器,并开启iPhone个人热点。

此时,手机端会弹出如图显示的是否允许电脑连接多媒体。这个可以看出路由器已经可以识别iPhone。

通过下面代码查看新增加的网络设备。原始设备一般为eth0,新增设备可能是eth1或eht2。

cat /proc/net/dev

这里,在我的路由器看到的新增设备是eth1,以此为例子,添加网络。

添加方式一:使用uci设置。

uci set network.iPhone=interface
uci set network.iPhone.proto=dhcp
uci set network.iPhone.ifname=eth1
uci commit network

添加方式二:编辑/etc/config/network文件,在文件中加入

config interface 'eth1'
  option ifname 'iPhone'
  option proto 'dhcp'

添加方式三:在luci界面手动添加

Network->Interface->Add new interface

添加网络,设备选择eth1,协议选dhcp

 

添加网络后,修改接口防火墙,与wan口在一起。(这个还没搞懂)

(四)上网吧!

 

这里将上面的思路整理了一下,写了一个自动配置脚本。

#!/bin/sh

#script to make the OpenWrt Router use net from iPhone

#install packages
opkg update
opkg install kmod-usb-net-ipheth kmod-usb-net kmod-usb-ohci libimobiledevice-utils libplist-utils libusbmuxd-utils usbmuxd

#usbmuxd auto-start
cp usbmuxd /etc/init.d/usbmuxd
chmod +x /etc/init.d/usbmuxd

/etc/init.d/usbmuxd enable
/etc/init.d/usbmuxd start

#set network
uci delete network.wan

uci set network.iPhone=interface
uci set network.iPhone.proto=dhcp
uci set network.iPhone.ifname=eth1
uci commit network

uci set firewall.@zone[1].network='wan6 iPhone'
uci commit firewall
/etc/init.d/firewall restart
/etc/init.d/network reload
/etc/init.d/network restart

也可以去我的GitHub查看

https://github.com/huipengly/OpenWrtUseiPhoneNet

 

参考

1.OpenWrt支持usb tethering

posted @ 2017-11-21 20:00  huipengly  阅读(5352)  评论(0编辑  收藏  举报