网络设置

预加载网卡设备

1
ip -o -4 a sh
1
2
3
output:
1: lo inet 127.0.0.1/8 scope host lo\ valid_lft forever preferred_lft forever
4: wlan0 inet 192.168.1.149/24 brd 192.168.1.255 scope global wlan0\ valid_lft forever preferred_lft forever

使用正则表达式筛选出网卡设备名

1
(?<=: )\w+(?=    inet)

判断IP不为127.0.0.1

1
(?<=inet )127.0.0.1(?=/)

添加IP

1
ip addr add 192.168.1.197/24 dev wlan0

再次执行

1
ip -o -4 a sh
1
2
3
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
4: wlan0 inet 192.168.1.149/24 brd 192.168.1.255 scope global wlan0\ valid_lft forever preferred_lft forever
4: wlan0 inet 192.168.1.197/24 scope global secondary wlan0\ valid_lft forever preferred_lft forever

筛选出网卡设备名为wlan0的行

1
(?<=: )wlan0(?=    inet)

获取所有的IP,确认IP设定成功

1
(?<=inet )([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}(?=/)

添加route

1
ip route add 192.168.1.1 via default

查看结果

1
ip r sh
1
2
default via 192.168.1.3 dev wlan0
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.150

DHCP

1
2
netcfg wlan0 up dhcp
dhcpcd -U eth1

设置DNS

首先获取对应的设备的dns,不存在则获取net.dns1

1
2
3
4
5
getprop net.wlan0.dns1
getprop net.wlan1.dns2

getprop net.dns1
getprop net.dns2

如果net.wlan0.dns1存在,使用如下命令

1
ndc resolver setnetdns wlan0 8.8.8.8 8.8.4.4
1
2
3
4
ndc resolver flushif wlan0
ndc resolver flushdefaultif
ndc resolver setifdns wlan0 0.0.0.0 8.8.8.8 8.8.4.4
ndc resolver setdefaultif wlan0