脚本:通过ping命令测试193.168.0.151到192.168.0.254主机是否在线

通过ping命令测试193.168.0.151到192.168.0.254主机是否在线。
如果在线,就显示“ip is up”,其中ip换作真正的ip地址,且以绿色显示
如果不在线,就显示“ip is down”,其中ip换作真正的ip地址,且以红色显示
使用while语句

1 #! /bin/bash
2 declare -i I=151
3
4 while [ $I -le 254 ];do
5 if [ ping 192.168.0.$I -w 8 &> /dev/null ];then
6 echo -e "\033[32m 192.168.0.$I is up \033[0m"
7 else
8 echo -e "\033[31m 192.168.0.$I is down \033[0m "
9 fi
10 I=$[$I+1]
11 done
12

使用until

1 #! /bin/bash
2 declare -i I=151
3 
4 until [ $I -gt 254 ];do
5 if [ ping 192.168.0.$I &> /dev/null ];then
6    echo -e "\033[32m 192.168.0.$I is up \033[0m"
7 else
8    echo -e "\033[31m 192.168.0.$I is down \033[0m "
9 fi
10 I=$[$I+1]
11 done
12

使用for第一种格式:

1 #! /bin/bash
2 
3 for I in {151..254};do
4     if [ ping 192.168.0.$I &> /dev/null ];then
5 echo -e "\033[32m 192.168.0.$I is up \033[0m"
6    else 
7       echo -e "\033[31m 192.168.0.$I is down \033[0m "
8     fi
9 done

for的第二种用法

1  #! /bin/bash
2  declare -i I=0
3 
4  for ((I=151;I<=254;I++));do
5       if [ ping 192.168.0.$I &> /dev/null ];then
6           echo -e "\033[32m 192.168.0.$I is up \033[0m"
7       else
8           echo -e "\033[31m 192.168.0.$I is down \033[0m "
9   fi
10  done
发布了56 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_41363156/article/details/84482672