├── README.md └── setDualNet.sh /README.md: -------------------------------------------------------------------------------- 1 | 2 | Android/Linux Shell 脚本:自动获取Wlan/EtherNet DHCP分配的动态IP,然后配置为静态IP并添加默认网关, 最后测试两个网络的联网状态 3 | 4 | 5 | 6 | 详细解析见博客:Shell脚本实现动态配置IP与路由:解决嵌入式Android/Linux有线和无线网卡双网共存问题 https://blog.csdn.net/HowieXue/article/details/75937972 7 | 8 | 9 | #Function: Auto set static IP for wlan/ethernet, which dynamically assigned from dhcp,and add default gateway 10 | 11 | #Param in: default gateway that can access internet, if not enter, this value will be *.*.*.1 of wlan ip 12 | 13 | #Notice: make sure that wlan has not reonnect, we recommend that script is only execute 1 time when network environment changed. 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /setDualNet.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin 2 | #Name: setDualNet 3 | #Author: Howie Xue, 2017/6/12 4 | #Function: Auto set static IP for wlan/ethernet, which dynamically assigned from dhcp,and add default gateway 5 | #Param in: default gateway that can access internet, if not enter, this value will be *.*.*.1 of wlan ip 6 | #Notice: make sure that wlan has not reonnect, we recommend that script is only execute 1 time when network environment changed. 7 | 8 | function CheckWlanEthInitialStatus() 9 | { 10 | if route|grep wlan >/dev/null && route|grep eth >/dev/null; 11 | then 12 | if ifconfig wlan0|grep 'inet addr' >/dev/null && ifconfig eth0|grep 'inet addr'>/dev/null; 13 | then 14 | echo "WLAN and Ethernet is all connected, Script running..." 15 | else 16 | echo "WLAN or Ethernet not have dhcp ip address, now exit..." 17 | exit 18 | fi 19 | else 20 | echo "WLAN or Ethernet is disconnected, now exit..." 21 | exit 22 | fi 23 | } 24 | function CheckWlanStatus() 25 | { 26 | if route|grep wlan >/dev/null; 27 | then 28 | if ifconfig wlan0|grep 'inet addr' >/dev/null; 29 | then 30 | : 31 | else 32 | echo "WLAN is reconnected, scripts may failed...pls check wlan status" 33 | fi 34 | else 35 | echo "WLAN is reconnected, scripts may failed...pls check wlan status" 36 | fi 37 | } 38 | CheckWlanEthInitialStatus 39 | WlanIP=$(ifconfig wlan0|grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g') 40 | EtherIP=$(ifconfig eth0|grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g') 41 | echo "Wlan original IP from DHCP is: "$WlanIP 42 | echo "Ethernet original IP from DHCP is: "$EtherIP 43 | 44 | InterIP=${WlanIP%.*} 45 | InterIPLast=${WlanIP##*.} 46 | let InterIPLastNew="$InterIPLast+1" 47 | # WlanInterIP=$InterIP.$InterIPLastNew 48 | WlanInterIP=$InterIP.255 49 | WlanDefaultGw=$InterIP.1 50 | 51 | InterIPLast="255" 52 | InterIP=${EtherIP%.*} 53 | EtherInterIP=$InterIP.$InterIPLast 54 | echo "eth interm ip is:"$EtherInterIP" wlan interm ip is:"$WlanInterIP 55 | # 56 | ifconfig wlan0 $WlanInterIP netmask 255.255.255.0 up 57 | ifconfig eth0 $EtherInterIP netmask 255.255.255.0 up 58 | # sleep 3; 59 | CheckWlanStatus 60 | for i in $(seq 1 1) 61 | do 62 | ifconfig eth0 $EtherIP netmask 255.255.255.0 up 63 | ifconfig wlan0 $WlanIP netmask 255.255.255.0 up 64 | # ifconfig wlan0 $WlanInterIP netmask 255.255.255.0 up 65 | # echo "WlanIP is :"$WlanIP 66 | echo "wait "$i" s..." 67 | CheckWlanStatus 68 | sleep 1; 69 | done 70 | ifconfig wlan0 up 71 | ifconfig eth0 up 72 | 73 | WlanIP=$(ifconfig wlan0|grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g') 74 | echo "Now Wlan Static IP is: " $WlanIP 75 | EtherIP=$(ifconfig eth0|grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g') 76 | echo "Now Ethernet Static IP is: " $EtherIP 77 | 78 | DefaultGWStatus=$(route | grep default) 79 | if [$DefaultGWStatus == ""]; 80 | then 81 | if [ ! -n "$1" ]; then 82 | echo "Param1 Not Enter, default gw use *.*.*.1 of wlan ip" 83 | route add default gw $WlanDefaultGw 84 | echo "Add Default Gateway:" $WlanDefaultGw 85 | else 86 | route add default gw $1 87 | echo "Add Default Gateway:" $1 88 | fi 89 | else 90 | echo "Default Gateway Existence, pls check it" 91 | fi 92 | 93 | # pingtime=$(ping -W 1 -c $1 | grep 'ttl') 94 | if [ ! -n "$1" ]; 95 | then 96 | if ping -c 2 -W 2 $WlanDefaultGw >/dev/null; 97 | then 98 | echo "Ping default gw from Para1 "$WlanDefaultGw" Sucess!" 99 | else 100 | echo "Ping default gw from Para1"$WlanDefaultGw" failed, now reconfig ip, pls check later..." 101 | ifconfig eth0 $EtherIP netmask 255.255.255.0 up 102 | # ifconfig wlan0 $WlanIP netmask 255.255.255.0 up 103 | fi 104 | else 105 | if ping -c 2 -W 2 $1 >/dev/null; 106 | then 107 | echo "Ping default gw "$1" Sucess!" 108 | else 109 | echo "Ping default gw "$1" failed, now reconfig ip, pls check later..." 110 | ifconfig eth0 $EtherIP netmask 255.255.255.0 up 111 | # ifconfig wlan0 $WlanIP netmask 255.255.255.0 up 112 | fi 113 | 114 | fi 115 | CheckWlanStatus 116 | if ping -c 2 -W 2 8.8.8.8 >/dev/null; 117 | then 118 | echo "Ping Internet 8.8.8.8 Sucess!" 119 | else 120 | echo "Ping Internet failed, now reconfig ip, pls also check Wlan..." 121 | ifconfig eth0 $EtherIP netmask 255.255.255.0 up 122 | # ifconfig wlan0 $WlanIP netmask 255.255.255.0 up 123 | fi 124 | 125 | --------------------------------------------------------------------------------