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 @   惊起千层浪  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示