写shell脚本,使用ping判断指定IP或者IP端内的主机存活。
只接受规定的IP格式输入和IP段输入(例如:192.168.2.88 或 192.168.2.)注意,写IP段的时候后面有个点...脚本简陋...看官将就一下吧;上代码:
#!/bin/bash
read -p "Please input ip:(192.168.0.22) or (192.168.0.)" Ip
Test=`echo $Ip|grep "\<[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\$"`
Test1=`echo $Ip|grep "\<[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.\$"`
#echo "a IP is: $Test"
#echo "IP.is $Test1"
if [[ -z $Test && -z $Test1 ]];then
echo "input error!"
exit 1
elif [[ -n $Test ]];then
for i in `seq 1 4`
do
Cut=`echo $Test|cut -d"." -f$i`
if [ $i -eq 1 ];then
if [ $Cut -gt 223 -o $Cut -lt 1 ];then
echo "input $Test ---->> $Cut invalid"
exit 2
fi
else
if [ $Cut -gt 254 ];then
echo "input $Test ---->> $Cut invalid"
exit 2
fi
fi
done
if ping -c 2 -W 1 $Test &> /dev/null ;then
echo "Have $Test"
else
echo "No $Test"
fi
else
for i in `seq 1 3`
do
Cut=`echo $Test1|cut -d"." -f$i`
if [ $i -eq 1 ];then
if [ $Cut -gt 223 -o $Cut -lt 1 ];then
echo "input $Test1 ---->> $Cut invalid"
exit 2
fi
else
if [ $Cut -gt 254 ];then
echo "input $Test1 ---->> $Cut invalid"
exit 2
fi
fi
done
declare -i Num
declare -i Fail
Num=0
Fail=0
for i in `seq 1 10`