为啥要使用shell命令操纵本地网络?因为这样就可以绕过上层connectService来干一些事情,至于具体要干点什么事情,容我先卖个关子在以后的blog里说明。
首先进入adb shell,然后去Ping一个服务器的ip地址
ping 211.69.198.222
PING 211.69.198.222 (211.69.198.222) 56(84) bytes of data.
64 bytes from 211.69.198.222: icmp_seq=1 ttl=46 time=356 ms
64 bytes from 211.69.198.222: icmp_seq=2 ttl=46 time=294 ms
64 bytes from 211.69.198.222: icmp_seq=5 ttl=46 time=342 ms
64 bytes from 211.69.198.222: icmp_seq=6 ttl=46 time=341 ms
64 bytes from 211.69.198.222: icmp_seq=8 ttl=46 time=342 ms
64 bytes from 211.69.198.222: icmp_seq=9 ttl=46 time=330 ms
^C
--- 211.69.198.222 ping statistics ---
9 packets transmitted, 6 received, 33% packet loss, time 8025ms
我们发现是可以Ping通的。
接着我们通过ifconfig命令(虽然是阉割版)来将我的3g网络接口关闭,随后再打开,然后再去尝试ping刚才的网络地址。
# ifconfig rmnet0
rmnet0: ip 172.22.39.243 mask 255.255.255.248 flags [up broadcast running multicast]
# ifconfig rmnet0 down
# ping 211.69.198.222
connect: Network is unreachable
# ifconfig rmnet0 up
# ping 211.69.198.222
connect: Network is unreachable
# ping 211.69.198.222
connect: Network is unreachable
# ifconfig rmnet0
rmnet0: ip 172.22.39.243 mask 255.255.255.248 flags [up broadcast running multicast]
这时候我奇怪的发现通过ifconfig命令这么一折腾,网络接口出了问题,虽然ifconfig显示我的rmnet0是打开的,但是却无法ping通。
经过仔细的思考,我猜测是因为当你把网络接口down掉之后,你的ip在路由器(基站?)上被释放了,虽然显示网络是可用的,但是这时候的网络是没有配置有效IP的。
这时候通过netcfg rmnet0 dhcp 重新申请一个ip ,然后继续Ping,不出所料,果然成功。
# netcfg rmnet0 dhcp
# ping 211.69.198.222
PING 211.69.198.222 (211.69.198.222) 56(84) bytes of data.
64 bytes from 211.69.198.222: icmp_seq=1 ttl=46 time=2743 ms
64 bytes from 211.69.198.222: icmp_seq=2 ttl=46 time=1747 ms
64 bytes from 211.69.198.222: icmp_seq=3 ttl=46 time=739 ms
64 bytes from 211.69.198.222: icmp_seq=4 ttl=46 time=98.9 ms
64 bytes from 211.69.198.222: icmp_seq=5 ttl=46 time=97.1 ms
^C
--- 211.69.198.222 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4015ms
结论:
通过Android Shell命令,完全可以绕开上层的网络管理机制来控制网络接口的状态。