Copy Code code 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 "'-----> Script starts executing ..."
While [$IP-LT 255]
Todo
host= $network. $IP
echo "--------> start to detect $host server communication is normal, ping times $ping_count."
Ping $host-C $ping _count >.ping_tmp
Sleep 1
Cat. Ping_tmp >>ping_action
echo "--------> Server $host detection is complete."
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 already in use"
echo "--> $host IP is already in use" >>ip_use
Else
echo "$host IP is currently idle: $loss _ping"
echo "$host IP currently idle" >>ip_idle
Fi
ip=$ ((ip+1))
Done
echo "' Date" +%y%m%d%h:%m:%s "'-----> Script completed ..."
To ping a network segment of all the IP, and detect the network connection status is normal, many methods can be implemented, the following brief description of two, as follows:
Script 1
Copy Code code as follows:
#!/bin/sh
# Ping network segment all IP
# 2012/02/05
Ip=1 #通过修改初值
While [$ip!= "254]; Todo
#yes正常, no host does not exist or is not normal
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 results:
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 2
Copy Code code as follows:
#!/bin/sh
# Ping network segment all IP
# 2012/02/05
Ip= "192.168.0."
For i in ' SEQ 1 254 '
Todo
Ping-c 2 $ip $i | Grep-q ' ttl= ' && echo "$ip $i yes" | | echo "$ip $i No"
#yes正常, no host does not exist or is not normal
Done
Output results:
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
...