通过ping命令,测试192.168.1.100到192.168.1.120之间的所有主机是否在线,如果在线,就显示“ip is up.”其中IP要换为真正的IP地址,并且以绿色显示;如果不在线,就显示“ip is down.”其中IP要换为真正的IP地址,并且以红色显示。要求,分别使用while,until和for(两种)实现。
1, #!/bin/bash
#
for I in {100..120} ;do
ping -c 2 -W 3 192.168.1.$I &>/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ];then
echo -e "\033[32m192.168.1.$I \033[0mis up."
else
echo-e "\033[31m192.168.1.$I \033[0m is down."
fi
done
2,#!/bin/bash
#
forI ((I=100;I<=120;I++)) ;do
if ping -c 2 -W 3 192.168.1.$I&>/dev/null;then
echo -e "\033[32m192.168.1.$I \033[0mis up."
else
echo -e "\033[31m192.168.1.$I \033[0mis down."
fi
done
3,#!/bin/bash
#
declare-i I=100
declare-i sum=0
while[ $I -ge 100 -a $I -lt 120 ] ;do
sum=$(($I+1))
let I++
ping -c 3 -W 3 192.168.1.$sum&>/dev/null
RETVAL=$?
if[ $RETVAL -eq 0 ];then
echo -e "\033[32m192.168.1.$sum\033[0m is up."
else
echo -e "\033[31m192.168.1.$sum\033[0m is down."
fi
done
4,#!/bin/bash
#
declare-i I=100
declare-i sum=0
until[ $I -lt 100 -o $I -gt 119 ] ;do
sum=$(($I+1))
let I++
if ping -c 3 -W 3 192.168.1.$sum&>/dev/null;then
echo -e "\033[32m192.168.1.$sum\033[0m is up."
else
echo -e "\033[31m192.168.1.$sum\033[0m is down."
fi
done
本文出自 “LawLer” 博客,请务必保留此出处http://10728233.blog.51cto.com/10718233/1695725
原文地址:http://10728233.blog.51cto.com/10718233/1695725