Today wrote a circular ping script to scan the network segment some of the IP address, the code is as follows:
#!/bin/bash
Ping_info () {
Ping-c 2-w 1 $;
if (($?! =0));
Then echo $ >>/tmp/iplist;
Fi
}
For i in ' seq 129 254 ';d O
ip_dx=192.168.1. $i
ip_lt=192.168.2. $i
ip_yd=192.168.3. $i
Ping_info $ip _DX
Ping_info $ip _lt
Ping_info $ip _yd
Done
After execution found a problem: regardless of the address can ping, are written to the/tmp/iplist, and the original intention of the script does not match.
After the command: Ping-c 2-w $1 Separate run with a pass-through IP and a non-pass IP to do the test, found that the value of the echo $? are all the same as the values in the. $. $. $.
Ping-c 2-w 1 IP:-C 2 means ping two packets, and-W 1 means the wait time is 1 seconds (in order to save time to execute the script), the problem is in this-W 1 here. Depending on the environment, the ping delay does not pass, ping two packets of time also does not pass. I use the time test here to ping two packets over a period of more than 1 seconds. So if you specify a time-out of 1 seconds, the second package may end up without a ping, so echo $? results are 1. After setting the time-out to 3 seconds, the script runs successfully.
#!/bin/bash
Ping_info () {
Ping-c 2-w 3 $;
if (($?! =0));
Then echo $ >>/tmp/iplist;
Fi
}
For i in ' seq 129 254 ';d O
ip_dx=192.168.1. $i
ip_lt=192.168.2. $i
ip_yd=192.168.3. $i
Ping_info $ip _DX
Ping_info $ip _lt
Ping_info $ip _yd
Done
This article is from the "I am very simple" blog, please be sure to keep this source http://easyyx.blog.51cto.com/8603153/1734825
Shell Script Ping-w Usage