编写SHELL脚本根据输入的IP地址的前三段为网络ID来判断有哪些机子在线哪些不在线

#!/bin/bash
#
read -p "please input the ip address[xx.xx.xx.xx]:" n
p1=`echo $n | awk -F. '{print $1}'`      #取出IP地址前三段
p2=`echo $n | awk -F. '{print $2}'`
p3=`echo $n | awk -F. '{print $3}'`
a=0                                      #当a、b、c都为1时,执行ping判断是否在线
b=0
c=0

d=`echo $n | sed -r 's/[0-9]//g' | sed -r 's/\.//g'`    #判断输入的IP地址是否正确
if [  -z "$d" ];then

   if [ $p1 -gt 0 ] && [ $p1 -lt 255 ];then
   a=1
   else
   echo "you input the ip is wrong!"
   fi

   if [ $p2 -ge 0 ] && [ $p2 -lt 255 ];then
   b=1
   else
   echo "you input the ip is wrong!"
   fi

   if [ $p3 -ge 0 ] && [ $p3 -lt 255 ];then
   c=1
   else
   echo "you input the ip is wrong!"
   fi

 else
 echo "you input  the ip is wrong!"
fi

if [ $a -eq 1 ] && [ $b -eq 1 ] && [ $c -eq 1 ];then
   for i in `seq 1 50`;do
     ping $p1.$p2.$p3.$i -c 1  -W 2 &> /dev/null
     o=$?
     if [ $o -eq 0 ] ;then
        echo "$p1.$p2.$p3.$i is online!"
        else
        echo "$p1.$p2.$p3.$i is down!"
     fi
   done
   
fi

ping

此条目发表在linux分类目录,贴了, , , , 标签。将固定链接加入收藏夹。