Android平台网络常用命令

本文详细介绍Android平台上通过Shell命令设置及获取IP地址、网关、DNS等网络参数的方法,并提供了DHCP自动获取网络配置的详细流程。

Android平台网络常用命令

工作中经常用到的一些命令,整理一下,方便以后进行参考

1.IP设置

 ifconfig eth0 128.224.156.81 up  //一般的嵌入式linux中设置IP。
ifconfig eth0 128.224.156.81 netmask 255.255.255.0 up
netcfg eth0 up dhcp   //通过dhcp 自动获取ip,网关,掩码等。

2. gateway

route add default gw 128.224.156.1  //一般的嵌入式linux会用这个命令添加网关,但是android中不用。直接“netcfg eth0 up dhcp” 自动分配IP等。

3. dns:
echo "nameserver 128.224.160.11" > resolv.conf  //一般嵌入式linux中设置dns的方法
nameserver 128.224.160.11

setprop net.dns1 128.224.160.11   //android中设置DNS的命令。
setprop net.dns2 147.11.100.30

4、自动获取IP

# ./dhcpcd -U eth1
broadcast_address='192.168.100.255'
dhcp_lease_time='30'
dhcp_message_type='5'
dhcp_server_identifier='192.168.100.1'
ip_address='192.168.100.10'
network_number='192.168.100.0'
routers='192.168.100.1'
subnet_cidr='24'
subnet_mask='255.255.255.0'

 

5、自动获取IP的详细调用过程

# ./dhcpcd -h eth1 -d
dhcpcd[1534]: version 5.6.8 starting
dhcpcd[1534]: eth0: using hwaddr a2:47:2c:1e:af:3b
dhcpcd[1534]: eth0: executing `/home/xxx/work/Android_Net/install/libexec/dhcpcd-run-hooks', reason PREINIT
dhcpcd[1534]: eth0: executing `/home/xxx/work/Android_Net/install/libexec/dhcpcd-run-hooks', reason CARRIER
dhcpcd[1534]: eth1: using hwaddr a2:47:2c:1e:af:3b
dhcpcd[1534]: eth1: executing `/home/xxx/work/Android_Net/install/libexec/dhcpcd-run-hooks', reason PREINIT
dhcpcd[1534]: eth1: executing `/home/xxx/work/Android_Net/install/libexec/dhcpcd-run-hooks', reason CARRIER
dhcpcd[1534]: eth0: sending IPv6 Router Solicitation
dhcpcd[1534]: eth0: reading lease `/var/db/dhcpcd-eth0.lease'
dhcpcd[1534]: eth0: rebinding lease of 192.168.20.14
dhcpcd[1534]: eth0: sending REQUEST (xid 0x425e5b0e), next in 3.62 seconds
dhcpcd[1534]: eth1: sending IPv6 Router Solicitation
dhcpcd[1534]: eth1: reading lease `/var/db/dhcpcd-eth1.lease'
dhcpcd[1534]: eth1: rebinding lease of 192.168.100.10
dhcpcd[1534]: eth1: sending REQUEST (xid 0x18442cad), next in 3.15 seconds
dhcpcd[1534]: eth0: acknowledged 192.168.20.14 from 192.168.3.185
dhcpcd[1534]: eth0: checking for 192.168.20.14
dhcpcd[1534]: eth0: sending ARP probe (1 of 3), next in 1.22 seconds
dhcpcd[1534]: eth1: acknowledged 192.168.100.10 from 192.168.100.1
dhcpcd[1534]: eth1: checking for 192.168.100.10
dhcpcd[1534]: eth1: sending ARP probe (1 of 3), next in 1.88 seconds
dhcpcd[1534]: eth0: sending ARP probe (2 of 3), next in 1.88 seconds
dhcpcd[1534]: eth1: sending ARP probe (2 of 3), next in 1.95 seconds
dhcpcd[1534]: eth0: sending ARP probe (3 of 3), next in 2.00 seconds
dhcpcd[1534]: eth0: sending IPv6 Router Solicitation
dhcpcd[1534]: eth1: sending IPv6 Router Solicitation
dhcpcd[1534]: eth1: sending ARP probe (3 of 3), next in 2.00 seconds
dhcpcd[1534]: eth0: leased 192.168.20.14 for 172800 seconds
dhcpcd[1534]: eth0: renew in 86400 seconds, rebind in 86400 seconds
dhcpcd[1534]: eth0: adding IP address 192.168.20.14/24
dhcpcd[1534]: eth0: router 192.168.3.254 requires a host route
dhcpcd[1534]: eth0: adding route to 192.168.20.0/24
dhcpcd[1534]: eth0: adding host route to 192.168.3.254
dhcpcd[1534]: eth0: adding default route via 192.168.3.254
dhcpcd[1534]: eth0: writing lease `/var/db/dhcpcd-eth0.lease'
dhcpcd[1534]: eth0: executing `/home/xxx/work/Android_Net/install/libexec/dhcpcd-run-hooks', reason BOUND
dhcpcd[1534]: forking to background
dhcpcd[1534]: forked to background, child pid 1575

转自 http://www.biemmeitalia.net/blog/android-network-configuration/

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

查看所有网络信息

C:\>adb shell
root@android:/ # netcfg
netcfg
ip6tnl0  DOWN                                   0.0.0.0/0   0x00000080 00:00:00:
00:00:00
gre0     DOWN                                   0.0.0.0/0   0x00000080 00:00:00:
00:00:00
eth0     UP                               192.168.0.180/24  0x00001043 00:00:00:
ec:0a:00
sit0     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
tunl0    DOWN                                   0.0.0.0/0   0x00000080 00:00:00:
00:00:00

查看eth0

root@android:/ # ifconfig eth0
ifconfig eth0
eth0: ip 192.168.0.180 mask 255.255.255.0 flags [up broadcast running multicast]

查看dns

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

root@android:/ # getprop net.eth0.dns2
getprop net.eth0.dns2
8.8.4.4

设置ip

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

root@android:/ # ifconfig eth0
ifconfig eth0
eth0: ip 192.168.0.173 mask 255.255.255.0 flags [up broadcast running multicast]

设置网关Gateway

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

添加dns

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

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

查看eth配置信息

root@android:/ # getprop | grep eth0
getprop | grep eth0
[dhcp.eth0.dns1]: [192.168.0.1]
[dhcp.eth0.dns2]: []
[dhcp.eth0.dns3]: []
[dhcp.eth0.dns4]: []
[dhcp.eth0.gateway]: [192.168.0.1]
[dhcp.eth0.ipaddress]: [192.168.0.180]
[dhcp.eth0.leasetime]: [3600]
[dhcp.eth0.mask]: [255.255.255.0]
[dhcp.eth0.pid]: [13800]
[dhcp.eth0.reason]: [PREINIT]
[dhcp.eth0.result]: [failed]
[dhcp.eth0.server]: [192.168.0.1]
[dhcp.eth0.vendorInfo]: []
[net.change]: [net.eth0.dns2]
[net.eth0.dns1]: [8.8.8.8]
[net.eth0.dns2]: [8.8.4.4]




评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值