shell编程 测试单个主机或网段主机存活
2012-09-26 21:44
197 查看
今天中午的作业,写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`
do
if ping -c 1 -W 1 ${Test1}$i &> /dev/null ;then
echo "Have ${Test1}$i"
Num=$((Num+1))
else
Fail=$((Fail+1))
echo "No ${Test1}$i"
fi
done
echo "Host online have $Num ."
echo "Host no online have $Fail ."
最后一个for循环,我嫌多,只写了10个值,有兴趣的哥们,自己改 seq 1 10改成1 254即可...还有关于a类b类c类网,由于本人网络方面知识的欠缺,所以判断的值,估计挺不正确的,列位就谅解一下吧....
只接受规定的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`
do
if ping -c 1 -W 1 ${Test1}$i &> /dev/null ;then
echo "Have ${Test1}$i"
Num=$((Num+1))
else
Fail=$((Fail+1))
echo "No ${Test1}$i"
fi
done
echo "Host online have $Num ."
echo "Host no online have $Fail ."
最后一个for循环,我嫌多,只写了10个值,有兴趣的哥们,自己改 seq 1 10改成1 254即可...还有关于a类b类c类网,由于本人网络方面知识的欠缺,所以判断的值,估计挺不正确的,列位就谅解一下吧....
相关文章推荐
- 企业内部网段检测有多少主机存活
- kali 信息录入3 测试主机是否存活
- 测试一个网段主机在线情况
- 测试网内主机存活状态
- shell脚本之测试某网段内主机连通性
- shell脚本_查看网段中的存活主机和MAC地址
- openvas对主机进行存活测试方式
- 企业内部网段检测有多少主机存活——shell脚本
- 企业内部网段检测有多少主机存活——shell脚本传参(二)
- shell脚本之测试某网段内主机连通性
- shell脚本测试某网段内主机连通性
- 虚拟主机中控制单个站点的FSO和ADODB.steam(Windows2008 测试)
- 检查某个网段存活的主机_shell脚本
- Shell扫描所在网段存活主机
- 主机网卡IP与网关IP不在相同网段对外通讯测试
- MPICH Demo测试__两主机进行通信出现的问题及解决方案
- 单域单站点单主机活动目录和Exchange灾难恢复实践测试(三)Active Directory恢复
- 11款国外免费主机空间推荐_测试可用
- 单域单站点单主机活动目录和Exchange灾难恢复实践测试(二)基本系统安装