安卓手机使用数据线连接电脑上网

安卓手机使用数据线连接电脑上网

gnirehtet

gnirehtet:
https://github.com/Genymobile/gnirehtet
提供了手机使用电脑USB数据线上网的功能。

下载gnirehtet

进入releases页面:https://github.com/Genymobile/gnirehtet/releases

下载你电脑系统对应的版本即可,如果是Windows系统,则下载Windows版本。
当然,如果想夸平台使用的话,也可以下载Java版本。

解压,添加Path环境变量

我下载的是Windows版本的,下载后将其解压,把gnirehtet.exe所在的目录添加到path环境变量中。

运行gnirehtet

手机使用USB线连接到电脑上,并开启USB调试功能。

然后运行gnirehtet-run.cmd即可,第一次运行会安装gnirehtet.apk,并且要求开启VPN,点确定,授予权限即可。

这个时候手机就可以直接使用usb数据线上网了。可以关掉手机的网络验证。

gnirehtet命令

帮助文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
D:\Desktop>gnirehtet
Syntax: gnirehtet (install|uninstall|reinstall|run|autorun|start|autostart|stop|restart|tunnel|relay) ...

gnirehtet install [serial]
Install the client on the Android device and exit.
If several devices are connected via adb, then serial must be
specified.

gnirehtet uninstall [serial]
Uninstall the client from the Android device and exit.
If several devices are connected via adb, then serial must be
specified.

gnirehtet reinstall [serial]
Uninstall then install.

gnirehtet run [serial] [-d DNS[,DNS2,...]] [-p PORT] [-r ROUTE[,ROUTE2,...]]
Enable reverse tethering for exactly one device:
- install the client if necessary;
- start the client;
- start the relay server;
- on Ctrl+C, stop both the relay server and the client.

gnirehtet autorun [-d DNS[,DNS2,...]] [-p PORT] [-r ROUTE[,ROUTE2,...]]
Enable reverse tethering for all devices:
- monitor devices and start clients (autostart);
- start the relay server.

gnirehtet start [serial] [-d DNS[,DNS2,...]] [-p PORT] [-r ROUTE[,ROUTE2,...]]
Start a client on the Android device and exit.
If several devices are connected via adb, then serial must be
specified.
If -d is given, then make the Android device use the specified
DNS server(s). Otherwise, use 8.8.8.8 (Google public DNS).
If -r is given, then only reverse tether the specified routes.
Otherwise, use 0.0.0.0/0 (redirect the whole traffic).
If -p is given, then make the relay server listen on the specified
port. Otherwise, use port 31416.
If the client is already started, then do nothing, and ignore
the other parameters.
10.0.2.2 is mapped to the host 'localhost'.

gnirehtet autostart [-d DNS[,DNS2,...]] [-p PORT] [-r ROUTE[,ROUTE2,...]]
Listen for device connexions and start a client on every detected
device.
Accept the same parameters as the start command (excluding the
serial, which will be taken from the detected device).

gnirehtet stop [serial]
Stop the client on the Android device and exit.
If several devices are connected via adb, then serial must be
specified.

gnirehtet restart [serial] [-d DNS[,DNS2,...]] [-p PORT] [-r ROUTE[,ROUTE2,...]]
Stop then start.

gnirehtet tunnel [serial] [-p PORT]
Set up the 'adb reverse' tunnel.
If a device is unplugged then plugged back while gnirehtet is
active, resetting the tunnel is sufficient to get the
connection back.

gnirehtet relay [-p PORT]
Start the relay server in the current terminal.

D:\Desktop>

安装apk

在您的Android设备上安装APK:

1
gnirehtet install [serial]

启动手机客户端

在另一个终端中,对于每个客户端,执行:

1
gnirehtet start [serial]

停止手机客户端

要停止客户端,请执行以下操作:

1
gnirehtet stop [serial]

恢复连接

要重置通道(在gnirehtet处于活动状态时拔下设备并重新插入时,有助于恢复连接):

1
gnirehtet tunnel [serial]

只有当adb devices输出多个设备时,才需要serial参数。

启动电脑服务端

1
gnirehtet relay

多功能命令

gnirehtet run

1
2
3
4
5
6
gnirehtet run [serial] [-d DNS[,DNS2,...]] [-p PORT] [-r ROUTE[,ROUTE2,...]]
Enable reverse tethering for exactly one device:
- install the client if necessary;
- start the client;
- start the relay server;
- on Ctrl+C, stop both the relay server and the client.

gnirehtet run功能如下命令

  • 会在必要的时候安装手机客户端,
  • 启动手机客户端
  • 启动电脑服务端
  • 当按下Ctrl+C时,手机客户端和电脑服务端都会停止

gnirehtet autorun

连接多个手机客户端

1
2
3
4
gnirehtet autorun [-d DNS[,DNS2,...]] [-p PORT] [-r ROUTE[,ROUTE2,...]]
Enable reverse tethering for all devices:
- monitor devices and start clients (autostart);
- start the relay server.

示例 使用USB数据线给多个手机上网

使用USB数据线连接两个手机,执行adb devices -l命令,结果如下:

1
2
3
4
D:\Desktop>adb devices -l
List of devices attached
75aed56d device product:R9PlusmA model:OPPO_R9_Plusm_A device:R9PlusA transport_id:27
U8ENW18117021408 device product:LLD-AL10 model:LLD_AL10 device:HWLLD-H transport_id:28

现在我打算对serialU8ENW18117021408的手机安装gnirehtet.apk

进入gnirehtet根目录

对于path环境变量中的目录下的命令,在cmd中可以使用where命令进行查找:

1
2
3
D:\Desktop>where gnirehtet
F:\软件\可执行_USB上网\gnirehtet-rust-win64-v2.5\gnirehtet-rust-win64\gnirehtet.exe

然后进入gnirehtet的根目录:

1
2
3
D:\Desktop>F:

D:\Desktop>cd F:\软件\可执行_USB上网\gnirehtet-rust-win64-v2.5\gnirehtet-rust-win64

安装gnirehtet.apk到手机上

输入gnirehtet install U8ENW18117021408命令把gnirehtet.apk安装到U8ENW18117021408这个手机上。

1
2
3
4
5
F:\软件\可执行_USB上网\gnirehtet-rust-win64-v2.5\gnirehtet-rust-win64>gnirehtet install U8ENW18117021408
2022-07-09 15:23:23.962 INFO Main: Installing gnirehtet client...
Performing Streamed Install
Success

看到Success就表示安装成功了

启动手机上的gnirehtet client

输入gnirehtet start U8ENW18117021408命令启动客户端。

1
2
3
4
F:\软件\可执行_USB上网\gnirehtet-rust-win64-v2.5\gnirehtet-rust-win64>gnirehtet start U8ENW18117021408
2022-07-09 15:26:18.162 INFO Main: Checking gnirehtet client...
2022-07-09 15:26:18.407 INFO Main: Starting client...
Starting: Intent { act=com.genymobile.gnirehtet.START cmp=com.genymobile.gnirehtet/.GnirehtetActivity }

此时,手机上的gnirehtet就启动好了,但是还不能使用usb数据线上网,因为电脑端的gnirehtet还没启动。

启动电脑上的gnirehtet服务端

重新打开一个cmd窗口,输入gnirehtet run,启动电脑上的客户端,这样就可以使用USB数据线上网了。

多个手机通过USB数据线上网

如果你已经在电脑端运行过gnirehtet run命令启动过gnirehtet服务端,则并且第1个手机已经成功通过USB数据线上网了。

只需要执行gnirehtet start 手机2的serial即可让第2个手机通过USB数据线使用电脑的网络

例如让serialU8ENW18117021408的第二个手机,也可以通过USB数据线上网。
则只需要执行gnirehtet start U8ENW18117021408即可,不再需要再次执行gnirehtet run

1
2
3
4
5
6
C:\\Users\\haha>gnirehtet start U8ENW18117021408
2022-07-10 18:35:47.909 INFO Main: Checking gnirehtet client...
2022-07-10 18:35:48.081 INFO Main: Starting client...
Starting: Intent { act=com.genymobile.gnirehtet.START cmp=com.genymobile.gnirehtet/.GnirehtetActivity }

C:\\Users\\haha>

此时,第2个手机也可以通过USB数据线上网了。

总结

  • 电脑端的gnirehtet run只需执行1次
  • 多个手机可执行多次的gnirehtet start serial命令

参考资料

https://github.com/Genymobile/gnirehtet