关键词:ping
方法一:
#!/bin/bash function Check_Network(){ check=$( cat /root/tansk/hostname.txt ) for ip in $check do ping -c 4 -i 0.2 -w 3 $ip &> /dev/null; if [ $? -eq 0 ] ; then echo "host $ip online!"; else echo "host $ip offline!"; fi done } Check_Network;
效果:
[root@test tansk]# ./test.sh host www.baidu.com offline! host 192.168.247.100 online! host 8.8.8.8 online! host 192.168.247.2 online! host 192.168.247.200 offline! [root@test tansk]#
方法二:
#!/bin/bash echo "请输入IP:" times=0 while true do let times++ read ip echo "计数:$times " ping -c 4 -i 0.2 -w 3 $ip &> /dev/null if [ $? -eq 0 ] then echo "host $ip is online" else echo "host $ip is offline" fi echo "继续输入IP:" done
效果:
[root@test tansk]# ./test.sh 请输入IP: 192.168.247.2 计数:1 host 192.168.247.2 is online 继续输入IP: 192.168.247.100 计数:2 host 192.168.247.100 is online
来源:https://www.cnblogs.com/tanshouke/p/12405238.html