Linux实现ping ip是否在线shell
#!/bin/bash
str="192.168.11."
for num in {1..25}
do
ip=${str}${num}
ping -c1 -w1 ${ip} &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is online"
echo $ip >>/root/online.txt
else
echo "$ip is offline"
echo $ip >> /root/offline.txt
fi
done
online=`cat /root/online.txt |wc -l`
offline=`cat /root/offline.txt|wc -l`
echo "目前在线的IP总数为$online"
echo "目前不在线的IP总数为$offline"
验证输出:
[root@patrolagent ~]# sh ping.sh
192.168.11.1 is offline
192.168.11.2 is offline
192.168.11.3 is offline
192.168.11.4 is offline
192.168.11.5 is offline
192.168.11.6 is offline
192.168.11.7 is offline
192.168.11.8 is offline
192.168.11.9 is offline
192.168.11.10 is offline
192.168.11.11 is offline
192.168.11.12 is offline
192.168.11.13 is offline
192.168.11.14 is offline
192.168.11.15 is offline
192.168.11.16 is offline
192.168.11.17 is offline
192.168.11.18 is offline
192.168.11.19 is offline
192.168.11.20 is offline
192.168.11.21 is offline
192.168.11.22 is offline
192.168.11.23 is offline
192.168.11.24 is offline
192.168.11.25 is offline
cat: /root/online.txt: No such file or directory
目前在线的IP总数为0
目前不在线的IP总数为25