1.需求:

编写脚本测试 192.168.10.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机。

2.shell脚本

ping-for.sh

#!/bin/bash
#ping-for shell

for i in {1..254}
do
ping -c 2 -i 0.5 -W 1 192.168.10.$i &> /dev/null
#-c:The Count of ping
#-i:The Interval of ping
#-W:The Timeout of single ping
if [ $? -eq 0 ];then
  echo "192.168.10.$i is up"
else
  echo "192.168.10.$i is down"
fi
done

验证

[root@elasticsearch ~]# sh ping-for.sh 
192.168.10.1 is up
192.168.10.2 is up
192.168.10.3 is down
192.168.10.4 is down
192.168.10.5 is down
192.168.10.6 is down
192.168.10.7 is down
192.168.10.8 is down
192.168.10.9 is down
192.168.10.10 is down
......
[root@elasticsearch ~]#