编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

for循环:

#!/bin/bash
#
NetId=192.168.0.
for HostId in {1..254};do
{
    if /bin/ping -c 1 -W 1 $NetId$HostId > /dev/null;then
        echo "$NetId$HostId lived,Ping Test Successed"
    else
        echo "$NetId$HostId not lived,Ping Test Failed"
    fi

} &
done
wait
while 循环:
#!/bin/bash
#
NetId=192.168.0.
declare -i HostId=1
while [ $HostId -le 254 ];do
{
/bin/ping -c 1 -W 1 $NetId$HostId > /dev/null if [ $? -eq 0 ];then echo "$NetId$HostId lived,Ping Test Successed" else echo "$NetId$HostId not lived,Ping Test Failed" fi
} & let HostId++ done
wait
posted @ 2022-04-16 16:35  海月如希  阅读(70)  评论(0)    收藏  举报