Copy codeThe Code is as follows :#! /Bin/bash
# Main ---
Network = 192.168.1
Ping_count = 3
IP = 1
:> IP_use
:> IP_idle
:> Ping_action
Echo "'date" + % Y % m % d % H: % M: % S "'-----> run the script ......"
While [$ ip-lt 255]
Do
Host = $ network. $ IP
Echo "--------> start to check if $ host server communication is normal. ping times $ ping_count ."
Ping $ host-c $ ping_count>. ping_tmp
Sleep 1
Cat. ping_tmp> ping_action
Echo "--------> the server $ host detection has been completed ."
Sum_ping = 'Tail-2. ping_tmp | head-1 | awk-F, '{print $2}' | cut-c 2-2'
Loss_ping = 'Tail-2. ping_tmp | head-1 | awk-F, '{print $4}' | cut-c 2-5'
If [$ sum_ping-eq $ ping_count]; then
Echo "--> $ host IP is in use"
Echo "--> $ host IP is in use"> IP_use
Else
Echo "$ host IP is currently idle: $ loss_ping"
Echo "$ host IP is currently idle"> IP_idle
Fi
IP = $ (IP + 1 ))
Done
Echo "'date" + % Y % m % d % H: % M: % S "'-----> the script has been run ......"
To Ping all IP addresses in a CIDR Block and check whether the network connection status is normal, you can use many methods. The following describes two methods:
Script 1Copy codeThe Code is as follows :#! /Bin/sh
# Ping all IP addresses of a CIDR Block
#2012/02/05
Ip = 1 # modifying the Initial Value
While [$ ip! = "254]; do
# Yes: The no host does not exist or is abnormal.
Ping 192.168.0. $ ip-c 2 | grep-q "ttl =" & echo "192.168.0. $ ip yes" | echo "192.168.0. $ ip no"
Ip = 'expr "$ ip" "+" "1 "'
Done
Output result:
192.168.0.1 yes
192.168.0.2 no
192.168.0.3 no
192.168.0.4 no
192.168.0.5 yes
192.168.0.6 no
192.168.0.7 yes
...
Script 2Copy codeThe Code is as follows :#! /Bin/sh
# Ping all IP addresses of a CIDR Block
#2012/02/05
Ip = "192.168.0 ."
For I in 'seq 1 254'
Do
Ping-c 2 $ ip $ I | grep-q 'ttl = '& echo "$ ip $ I yes" | echo "$ ip $ I no"
# Yes: The no host does not exist or is abnormal.
Done
Output result:
192.168.0.1 yes
192.168.0.2 no
192.168.0.3 no
192.168.0.4 no
192.168.0.5 yes
192.168.0.6 no
192.168.0.7 yes
...