1. Testing the connectivity of the 192.168.4.0/24 entire network segment (while version)
#!/bin/bash#author: The Adventures of Tintin (Jacob) #定义变量i控制循环次数, I cycle from 1, once per loop, I self plus 1, until I equals 254 loop exit # Ping An IP in each loop, if Ping Pass, Prompt host is up # If you cannot ping the same, prompt the host is down the #ping command's-C option to control the number of ping tests,-C2 for the target host to perform 2 ping Test #ping command of the-I option, the time interval to control multiple ping tests default to 1 seconds,- i0.3 can improve the test efficiency of ping #ping the-w option of the command, you can control the time-out, the default one host cannot ping, need to wait a long time out, #才会提示无法ping通, using-W1, set the time-out to 1 seconds. i=1while [ $i -le 254 ]do ping -c2 -i0.3 -w1 192.168.4. $i &>/dev/null if [ $? –eq 0 ];then echo "192.168.4. $i is up " else echo "192.168.4. $i is down" fi let i++done
2. Test the connectivity of the 192.168.4.0/24 entire network segment (for version)
#!/bin/bash#author: The Adventures of Tintin (Jacob) #定义for循环变量i, execution cycles are 254 times, I cycle from 1 to 254# each cycle to a target host test ping connectivity, The syntax format of the ping command references the previous while version for I in {1..254}do ping-c2-i0.3-w1 192.168.4. $i &>/dev/null if [$? –eq 0];then echo "192.168.4. $i are up" else echo "192.168.4. $i are down" Fidon E
3. Test 192.168.4.0/24 connectivity across the entire network segment (multi-process version)
#!/bin/bash#author: The Adventures of Tintin (Jacob) #定义一个函数, ping a host and detect the host's survival status, ping syntax format refer to the previous while version myping () {PING-C2-I0.3-W1 $ & >/dev/nullif [$?-eq 0];thenecho "is up" Elseecho "was down" fi}for i in {1..254}do myping 192.168.4.$ I &done# use the & symbol to put the executed function into the background # The benefit of this is that you do not have to wait for the ping first host to respond, you can continue to ping the second host concurrently, and so on.
This article is from the "Ding Ding Adventures" blog, please be sure to keep this source http://manual.blog.51cto.com/3300438/1971293
Shell script testing of host connectivity in a certain network segment