搬砖:Android通过shell命令修改网络信息

Android通过shell命令修改网络信息

2018年08月14日 11:33:20 feiniao8651 阅读数:568

文章允许转载,请注明来源:https://blog.csdn.net/feiniao8651/article/details/81664039
Android一般是通过设置界面来配置各种网络信息,今天讲一下当不方便使用设置的时候,通过shell命令来查看和配置网络的方法。
注:以下shell命令在不同版本Android系统上可能存在差异。

查看网络连接信息

查看全部连接

#netcfg
ccmni2   DOWN                                   0.0.0.0/0   0x00000080 11:22:22:33:44:11
ccmni0   DOWN                                   0.0.0.0/0   0x00000080 2a:3d:e1:22:5d:11
ccmni1   DOWN                                   0.0.0.0/0   0x00000080 1e:bc:dd:5a:32:11
wlan0    UP                               192.168.1.151/24  0x00001043 5c:f7:c3:dd:44:11
tunl0    DOWN                                   0.0.0.0/0   0x00000080 00:00:00:00:00:00
lo       UP                                   127.0.0.1/8   0x00000049 00:00:00:00:00:00
sit0     DOWN                                   0.0.0.0/0   0x00000080 00:00:00:00:00:00
p2p0     UP                                     0.0.0.0/0   0x00001003 5e:f7:c3:dd:44:11
ifb0     DOWN                                   0.0.0.0/0   0x00000082 e6:a9:76:38:04:11
ifb1     DOWN                                   0.0.0.0/0   0x00000082 96:d6:f4:59:10:11
ip6tnl0  DOWN                                   0.0.0.0/0   0x00000080 00:00:00:00:00:11
  • 1

查看指定连接

#ifconfig wlan0
wlan0: ip 192.168.1.151 mask 255.255.255.0 flags [up broadcast running multicast]
  • 1
  • 2

设置IP

方法1:

//ifconfig <dev> <ip> netmask <value>
 例:ifconfig wlan0 192.168.1.10 netmask 255.255.255.0
  • 1
  • 2

方法2:

//ip address add <ip> dev <value>
例:ip address add 192.168.1.10/24 dev wlan0
  • 1
  • 2

设置默认网关

设置默认网关主要是通过路由表

ip route add 192.168.1.0/24 via 192.168.1.1
  • 1

也可以直接使用dhcp命令来自动获取

netcfg wlan0 dhcp
  • 1

设置DNS

设置dns的方法比较多,但是不同环境,以及不同的系统版本,能生效的方式不一定相同,可以多试几种,这里提供一种我用的方式,适用于Android 5.1,其他系统参照https://github.com/ukanth/afwall/wiki/DNS#resolver-commands

ndc resolver setnetdns <netid> <domain> <dns1> <dns2>
例:ndc resolver setnetdns wlan0 "" 8.8.8.8 8.8.4.4
  • 1
  • 2

之前在xda论坛上看到过其他方式,如果以上方式无效,也可以看一下这个论坛里的方法https://forum.xda-developers.com/general/xda-university/guide-how-to-change-dns-android-device-t3273769

这些命令有的比较简单,用法也比较单一,比如netcfg,但是有的就要复杂一些,比如路由表的配置。Android 的shell环境里没有man的命令,查看具体的命令用法一般就是在命令后加-h 或者help。

参考:
DNS-commands
ifconfig vs ip
[GUIDE] How to Change DNS in Android Device (8 methods)

 

 

 

 

Android 命令设置获取、IP地址、网关、dns

2017年10月17日 17:16:22 xz——华 阅读数:5076

https://blog.csdn.net/zhang__ao/article/details/78262738

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhang__ao/article/details/78262738

通过shell命令设置(获取)IP、网关、dns信息,需要获取root权限

查看所有网络信息

 
  1. C:\>adb shell

  2. root@android:/ # netcfg

  3. netcfg

  4. ip6tnl0 DOWN 0.0.0.0/0 0x00000080 00:00:00:

  5. 00:00:00

  6. gre0 DOWN 0.0.0.0/0 0x00000080 00:00:00:

  7. 00:00:00

  8. eth0 UP 192.168.0.180/24 0x00001043 00:00:00:

  9. ec:0a:00

  10. sit0 DOWN 0.0.0.0/0 0x00000080 00:00:00:

  11. 00:00:00

  12. lo UP 127.0.0.1/8 0x00000049 00:00:00:

  13. 00:00:00

  14. tunl0 DOWN 0.0.0.0/0 0x00000080 00:00:00:

  15. 00:00:00

查看eth0

 
  1. root@android:/ # ifconfig eth0

  2. ifconfig eth0

  3. eth0: ip 192.168.0.180 mask 255.255.255.0 flags [up broadcast running multicast]

查看dns

 
  1. root@android:/ # getprop net.eth0.dns1

  2. getprop net.eth0.dns1

  3. 8.8.8.8

  4.  
  5. root@android:/ # getprop net.eth0.dns2

  6. getprop net.eth0.dns2

  7. 8.8.4.4

设置ip

 
  1. root@android:/ # ifconfig eth0 192.168.0.173 netmask 255.255.255.0

  2. ifconfig eth0 192.168.0.173 netmask 255.255.255.0

  3.  
  4. root@android:/ # ifconfig eth0

  5. ifconfig eth0

  6. eth0: ip 192.168.0.173 mask 255.255.255.0 flags [up broadcast running multicast]

设置网关Gateway

 
  1. root@android:/ # route add default gw 192.168.0.1 dev eth0

  2. route add default gw 192.168.0.1 dev eth0

添加dns

 
  1. root@android:/ # setprop net.eth0.dns1 8.8.8.8

  2. setprop net.eth0.dns1 8.8.8.8

  3.  
  4. root@android:/ # setprop net.eth0.dns2 8.8.4.4

  5. setprop net.eth0.dns2 8.8.4.4

查看eth配置信息

 
  1. root@android:/ # getprop | grep eth0

  2. getprop | grep eth0

  3. [dhcp.eth0.dns1]: [192.168.0.1]

  4. [dhcp.eth0.dns2]: []

  5. [dhcp.eth0.dns3]: []

  6. [dhcp.eth0.dns4]: []

  7. [dhcp.eth0.gateway]: [192.168.0.1]

  8. [dhcp.eth0.ipaddress]: [192.168.0.180]

  9. [dhcp.eth0.leasetime]: [3600]

  10. [dhcp.eth0.mask]: [255.255.255.0]

  11. [dhcp.eth0.pid]: [13800]

  12. [dhcp.eth0.reason]: [PREINIT]

  13. [dhcp.eth0.result]: [failed]

  14. [dhcp.eth0.server]: [192.168.0.1]

  15. [dhcp.eth0.vendorInfo]: []

  16. [net.change]: [net.eth0.dns2]

  17. [net.eth0.dns1]: [8.8.8.8]

  18. [net.eth0.dns2]: [8.8.4.4]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值