检查网络连通情况

廉价感情. 提交于 2020-03-04 00:08:41

 


关键词: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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!