题目如下:
#!/bin/bash
#the script's name is:*********use to test the host*********
#the author is :Ro
#the version is :Ro-4-10
let PIP=$1
NetIP='192.168.1.'
if [ $1 -gt 254 ];then
echo "The fist number should <=254"
elif [ $2 -gt 254 ];then
echo "The second number should <=254"
elif [ $1 -gt $2 ];then
echo "The first number should <= the second number"
else
while [ $PIP -le $2 ];do
ping -c1 -W1 $NetIP$PIP > /dev/null && echo "The host $NetIP$PIP is UP." >> /tmp/host_state || echo "The host $NetIP$PIP is DOWN." >> /tmp/host_state
let PIP++
done
echo "=============================="
cat /tmp/host_state
echo "=============================="
sed '1,$s/\(.*DOWN\)./#\1/g' /tmp/host_state
echo "=============================="
sort -t " " -k 5 /tmp/host_state | cut -d" " -f5 | uniq -c
echo "=============================="
rm -rf /tmp/host_state
fi
#the script's name is:*********use to test the host*********
#the author is :Ro
#the version is :Ro-4-10
let PIP=$1
NetIP='192.168.1.'
if [ $1 -gt 254 ];then
echo "The fist number should <=254"
elif [ $2 -gt 254 ];then
echo "The second number should <=254"
elif [ $1 -gt $2 ];then
echo "The first number should <= the second number"
else
while [ $PIP -le $2 ];do
ping -c1 -W1 $NetIP$PIP > /dev/null && echo "The host $NetIP$PIP is UP." >> /tmp/host_state || echo "The host $NetIP$PIP is DOWN." >> /tmp/host_state
let PIP++
done
echo "=============================="
cat /tmp/host_state
echo "=============================="
sed '1,$s/\(.*DOWN\)./#\1/g' /tmp/host_state
echo "=============================="
sort -t " " -k 5 /tmp/host_state | cut -d" " -f5 | uniq -c
echo "=============================="
rm -rf /tmp/host_state
fi
测试结果:
这个脚本主要运用到了循环语句,条件判断,如IP地址的取值范围,变量的赋值等进行判断,使用if语句和while语句结合起来达到效果,还有排序等知识点,在
以后的网络中的运用也是很广泛的
转载于:https://blog.51cto.com/luoweiro/621983