Android adb查看网络连接情况

Android adb查看网络连接情况

文章目录

  • Android adb查看网络连接情况
    • Android 使用adb查看网络使用情况主要的adb命令:
      • 1、adb shell ifconfig
      • 2、adb shell dumpsys connectivity
      • 3、adb shell netstat
      • 3、adb shell ip ru
      • 总结一下:

Android 使用adb查看网络使用情况主要的adb命令:


adb shell ifconfig //查看配置信息

adb shell dumpsys connectivity  //查看连接情况

db shell netstat //查看连接状态和地址

adb shell ip ru //查看路由策略

下面展开说一下几个命令输出后的数据如何分析或者查看。

1、adb shell ifconfig

输入命令,获取到如下信息:


C:\Users\liwenzhi>adb shell ifconfig

wlan0     Link encap:Ethernet  HWaddr 20:32:33:91:23:9e  Driver usb
          inet addr:192.168.43.53  Bcast:192.168.43.255  Mask:255.255.255.0
          inet6 addr: 2409:8955:3848:1a7e:5123:75a1:44e9:d36b/64 Scope: Global
          inet6 addr: fe80::8ea1:6249:e2e9:1458/64 Scope: Link
          inet6 addr: 2409:8955:3848:1a7e:9850:3a4a:f949:6b6e/64 Scope: Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:409 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4295 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:89876 TX bytes:1597990

p2p0      Link encap:Ethernet  HWaddr 22:32:33:91:23:9e

          RX bytes:0 TX bytes:432083

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          RX bytes:2341 TX bytes:2341

dummy0    Link encap:Ethernet  HWaddr 6e:c6:e9:ec:c9:ec
          inet6 addr: fe80::6cc6:e9ff:feec:c9ec/64 Scope: Link


eth0      Link encap:Ethernet  HWaddr 02:ad:36:01:8a:3d  Driver meson8b-dwmac
          inet addr:192.5.1.17  Bcast:192.5.1.255  Mask:255.255.255.0
          inet6 addr: fe80::d09e:fcd3:3411:2da1/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8905 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7287 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1549861 TX bytes:1983944
          Interrupt:48


C:\Users\liwenzhi>


上面表示已经打开的连接端口/网卡名称,如果没有打开是看不到的
比如:只有wifi开启状态才显示wlan0这项

这里一般关心wlan0(wifi)和eth0(有线网络)

如果连接成功inet addr会显示具体ip地址

其中inet addr:表示连接成功后的ip地址,如果没有连接成功,是没有的。

2、adb shell dumpsys connectivity

这个打印的内容是比较多的,看重点地方即可。

简单的说,就是连接上网络后Current Networks才看到相关信息
ip地址,网络的Score分值(分值越高越优先,一般针对优先网络和wifi的切换)等信息

具体情况如下图如所示:

Android adb查看网络连接情况_第1张图片

总结就是注意看Current Networks项里面的内容即可。

部分信息:


Active default network: 101

Current Networks:
  NetworkAgentInfo{ ni{[type: Ethernet[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: 02:ad:36:01:8a:3d, failover: false, available: true, roaming: false]}  network{100}  nethandle{432902426637}  lp{{InterfaceName: eth0 LinkAddresses: [ fe80::5f92:6ddf:1843:9ffa/64,192.5.1.6/24 ]    Requests: REQUEST:0 LISTEN:3 BACKGROUND_REQUEST:0 total:3
      NetworkRequest [ LISTEN id=4, [ Capabilities: FOREGROUND AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
      NetworkRequest [ LISTEN id=5, [ Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN&FOREGROUND Uid: 1000 AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
      NetworkRequest [ LISTEN id=7, [ Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN Uid: 1000 AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
    Lingered: //之前的请求信息(不是当前的)
      NetworkRequest [ REQUEST id=1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ], expires 24995ms
      NetworkRequest [ TRACK_DEFAULT id=8, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED Uid: 1000 AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ], expires 24995ms
      NetworkRequest [ TRACK_DEFAULT id=9, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED Uid: 1073 AdministratorUids: [] RequestorUid: 1073 RequestorPackageName: com.android.networkstack] ], expires 24995ms
      NetworkRequest [ REQUEST id=11, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN AdministratorUids: [] RequestorUid: 1073 RequestorPackageName: com.android.networkstack] ], expires 24995ms
      NetworkRequest [ TRACK_DEFAULT id=13, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED Uid: 10068 AdministratorUids: [] RequestorUid: 10068 RequestorPackageName: com.dewmobile.kuaiya] ], expires 24995ms
  NetworkAgentInfo{ ni{[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: , failover: false, available: true, roaming: false]}  network{101}  nethandle{437197393933}  lp{{InterfaceName: wlan0 LinkAddresses: [ fe80::bbbb:2be7:6b3f:67da/64,192.5.1.27/24 ] DnsAddresses: [ /192.5.1.1 ]     Requests: REQUEST:2 LISTEN:8 BACKGROUND_REQUEST:0 total:10
      NetworkRequest [ REQUEST id=1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
      NetworkRequest [ LISTEN id=4, [ Capabilities: FOREGROUND AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
      NetworkRequest [ LISTEN id=5, [ Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN&FOREGROUND Uid: 1000 AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
      NetworkRequest [ LISTEN id=7, [ Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN Uid: 1000 AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
      NetworkRequest [ TRACK_DEFAULT id=8, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED Uid: 1000 AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: android] ]
      NetworkRequest [ TRACK_DEFAULT id=9, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED Uid: 1073 AdministratorUids: [] RequestorUid: 1073 RequestorPackageName: com.android.networkstack] ]
      NetworkRequest [ LISTEN id=10, [ Transports: WIFI Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN Uid: 1073 AdministratorUids: [] RequestorUid: 1073 RequestorPackageName: com.android.networkstack] ]
      NetworkRequest [ REQUEST id=11, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN AdministratorUids: [] RequestorUid: 1073 RequestorPackageName: com.android.networkstack] ]
      NetworkRequest [ TRACK_DEFAULT id=13, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED Uid: 10068 AdministratorUids: [] RequestorUid: 10068 RequestorPackageName: com.dewmobile.kuaiya] ]
      NetworkRequest [ LISTEN id=17, [ Transports: WIFI Capabilities: NOT_VPN AdministratorUids: [] RequestorUid: 1000 RequestorPackageName: com.skg.benqsetting] ]

我这里是在系统中注释了网络切换后网络释放的操作,所以Current Networks才会有多个网络(正常是只看到一个NetworkAgentInfo网络对象的)

但是系统默认连接的只有一个那就是Active default network的网络

从NetworkAgentInfo也可以体现出现,那就是NetworkRequest的Capabilities包含INTERNET标签;
还有就是 包含REQUEST id=1 这个基本就是当前正在使用的网络

有些设备需要支持wifi和有些网都能连通,就不能释放网络。app内可以进行不同网络的选择连接。

3、adb shell netstat

用得不多,可能是查看连接过的具体网址

注意是看上面tcp的信息

Address是本机的信息。

Foreign Address是访问的地址信息。

打印情况如下:


C:\Users\liwenzhi>adb shell netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 192.5.1.6:58752         server-13-225-103:https ESTABLISHED
tcp        0      0 192.5.1.6:35612         hkg07s40-in-f22.1:https TIME_WAIT
tcp        0      0 192.5.1.6:40632         kul06s17-in-f225.:https TIME_WAIT
tcp        0      0 192.5.1.6:57072         201.181.244.35.bc:https ESTABLISHED
tcp        0      0 192.5.1.6:52326         kul06s17-in-f226.:https ESTABLISHED
tcp        0      0 192.5.1.6:41524         server-13-225-103:https ESTABLISHED
tcp        0      0 192.5.1.6:56136         203.119.217.126:http    ESTABLISHED
tcp        0      0 192.5.1.6:60548         ec2-35-161-17-206:https ESTABLISHED
tcp        0      0 192.5.1.6:50062         hkg07s29-in-f13.1:https ESTABLISHED
tcp        0      0 192.5.1.6:35616         hkg07s40-in-f22.1:https ESTABLISHED
tcp        0      0 192.5.1.6:40634         kul06s17-in-f225.:https TIME_WAIT
。。。
tcp6      32      0 ::ffff:192.5.1.6:51762  ec2-140-179-11-12:https CLOSE_WAIT
tcp6      32      0 ::ffff:192.5.1.6:51840  ec2-140-179-11-12:https CLOSE_WAIT
tcp6      32      0 ::ffff:192.5.1.6:54924  ec2-140-179-154-1:https CLOSE_WAIT
。。。
udp        0      0 192.5.1.6:bootpc        192.5.1.1:bootps        ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State           I-Node Path
unix  110    [ ]         DGRAM                         527 /dev/socket/logdw
unix  16     [ ]         DGRAM                       25380 /dev/socket/statsdw
unix  2      [ ]         DGRAM                       26288 /data/system/unsolzygotesocket
unix  2      [ ]         DGRAM                       24418

...//下面还有很多unix的日志

3、adb shell ip ru


C:\Users\liwenzhi>adb shell ip ru
0:      from all lookup local
10000:  from all fwmark 0xc0000/0xd0000 lookup legacy_system
10500:  from all iif lo oif dummy0 uidrange 0-0 lookup dummy0
10500:  from all iif lo oif eth0 uidrange 0-0 lookup eth0
10500:  from all iif lo oif wlan0 uidrange 0-0 lookup wlan0
13000:  from all fwmark 0x10063/0x1ffff iif lo lookup local_network
13000:  from all fwmark 0x10064/0x1ffff iif lo lookup eth0
13000:  from all fwmark 0x10065/0x1ffff iif lo lookup wlan0
14000:  from all iif lo oif dummy0 lookup dummy0
14000:  from all iif lo oif eth0 lookup eth0
14000:  from all iif lo oif wlan0 lookup wlan0
15000:  from all fwmark 0x0/0x10000 lookup legacy_system
16000:  from all fwmark 0x0/0x10000 lookup legacy_network
17000:  from all fwmark 0x0/0x10000 lookup local_network
19000:  from all fwmark 0x64/0x1ffff iif lo lookup eth0
19000:  from all fwmark 0x65/0x1ffff iif lo lookup wlan0
22000:  from all fwmark 0x0/0xffff iif lo lookup wlan0
32000:  from all unreachable
C:\Users\liwenzhi>

这里是路由策略,应该是和NetworkAgentInfo里面的NetworkRequest有一定的关联

路由策略是可以进行设置的,就是通过adb禁止/允许网络的使用(wifi/有线网络)。

但是路由策略需要root权限!设备默认root或者adb root成功才行。

adb shell ip ru flush //清空所有的路由表
adb shell ip ru add from 192.168.13.X/24 lookup eth0 //把内网设置成以太网
adb shell ip ru add from all lookup wlan0 //添加wifi联网
adb shell ip ru del from XXX //删除某一个路由策略

下面是一组简单的操作示例:


C:\Users\liwenzhi>adb shell ip ru
0:      from all lookup local
10000:  from all fwmark 0xc0000/0xd0000 lookup legacy_system
10500:  from all iif lo oif dummy0 uidrange 0-0 lookup dummy0
10500:  from all iif lo oif eth0 uidrange 0-0 lookup eth0
10500:  from all iif lo oif wlan0 uidrange 0-0 lookup wlan0
13000:  from all fwmark 0x10063/0x1ffff iif lo lookup local_network
13000:  from all fwmark 0x10064/0x1ffff iif lo lookup eth0
13000:  from all fwmark 0x10065/0x1ffff iif lo lookup wlan0
14000:  from all iif lo oif dummy0 lookup dummy0
14000:  from all iif lo oif eth0 lookup eth0
14000:  from all iif lo oif wlan0 lookup wlan0
15000:  from all fwmark 0x0/0x10000 lookup legacy_system
16000:  from all fwmark 0x0/0x10000 lookup legacy_network
17000:  from all fwmark 0x0/0x10000 lookup local_network
19000:  from all fwmark 0x64/0x1ffff iif lo lookup eth0
19000:  from all fwmark 0x65/0x1ffff iif lo lookup wlan0
22000:  from all fwmark 0x0/0xffff iif lo lookup wlan0
32000:  from all unreachable

C:\Users\liwenzhi>adb shell
t7_an400:/ $ exit

C:\Users\liwenzhi>adb shell ip ru flush
RTNETLINK answers: Operation not permitted
Dump terminated

C:\Users\liwenzhi>adb root
restarting adbd as root

C:\Users\liwenzhi>
C:\Users\liwenzhi>
C:\Users\liwenzhi>adb shell ip ru flush

C:\Users\liwenzhi>adb shell ip ru
0:      from all lookup local

C:\Users\liwenzhi>adb shell ip ru add from 192.5.1.0/24 lookup eth0

C:\Users\liwenzhi>adb shell ip ru add from 192.5.1.6/24 lookup eth0

C:\Users\liwenzhi>adb shell ip ru add from all lookup wlan0

C:\Users\liwenzhi>adb shell ip ru
0:      from all lookup local
0:      from 192.5.1.0/24 lookup eth0
0:      from 192.5.1.6/24 lookup eth0
0:      from all lookup wlan0

C:\Users\liwenzhi>adb shell ip ru del from 192.5.1.0/24 lookup eth0

C:\Users\liwenzhi>adb shell ip ru
0:      from all lookup local
0:      from 192.5.1.6/24 lookup eth0
0:      from all lookup wlan0

C:\Users\liwenzhi>

可以自己测试一下,删除所有路由策略发现wifi上不了网,把wifi策略添加后就可以上网了。

总结一下:

比较简单易懂在是:
adb shell ifconfig //查看配置信息
比较全面在信息是:
adb shell dumpsys connectivity //查看连接情况

你可能感兴趣的:(Android系统,Android网络,android,android,网络,java)