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

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

 

 

for循环:

[root@CentOS8 ~]# cat ping_for_loop.sh

#!/bin/bash

#

#********************************************************************

#Author:     Hbiaoming

#QQ:     123456789

#Date:     2022-07-14

#FileName     ping_for_loop.sh

#URL:     www.haha.cn

#Description The test script

#Copyright (C): 2022 All rights reserved

#********************************************************************

net=10.0.0

for i in {1..254};do

    {

        if

        ping -c1 -w1 $net.$i &> /dev/null;then

            echo $net.$i is success! >> /root/haha.txt

        else

            echo $net.$i is fall! >> /root/haha.txt

        fi

    }&

done

 

 

 

 

while循环:

[root@CentOS8 ~]# cat ping_while_loop.sh

#!/bin/bash

#

#********************************************************************

#Author:     Hbiaoming

#QQ:     123456789

#Date:     2022-07-14

#FileName     ping_while_loop.sh

#URL:     www.haha.cn

#Description The test script

#Copyright (C): 2022 All rights reserved

#********************************************************************

net=10.0.0

declare -i ip=1

while [ $ip -le 254 ];do

    {

        ping -c1 -w1 $net.$ip &> /dev/null

        if [ $? -eq 0 ];then

            echo $net.$ip is success! >> haha2.txt

        else

            echo $net.$ip is fall! >> haha2.txt

        fi

    }&

let ip++

done

 

 

posted @ 2022-07-16 15:28  惊起千层浪  阅读(113)  评论(0)    收藏  举报