在Android下利用Shell命令管理网络接口

为啥要使用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命令,完全可以绕开上层的网络管理机制来控制网络接口的状态。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值