您的位置:首页 > 运维架构 > Linux

linux 用于测试指定段的主机的连通性的脚本

2011-07-24 13:45 204 查看
题目如下:



#!/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

测试结果:



这个脚本主要运用到了循环语句,条件判断,如IP地址的取值范围,变量的赋值等进行判断,使用if语句和while语句结合起来达到效果,还有排序等知识点,在
以后的网络中的运用也是很广泛的本文出自 “Ro の博客” 博客,请务必保留此出处http://luoweiro.blog.51cto.com/2186161/621983
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: