[ 登录注册 ]

云计算

获取ping不通的IP地址

2016-08-25 21:31:51 admin 返回上一页

#!/bin/ksh
#..............................................................................
# Description    : ping ip address
# Author         : jiangshouzhuang
# Date           : 2012-06-28
# Version        : 1.5
#.............................................................................

#统计IP地址的初始次数
count=0

#定义临时文件
t_tmpfile="/tmp/ping.tmp"

function pingBuTong
{
    echo "============Get IPs Begin(OMSuite)==============="
    echo "The IP Address you need:"
   
    #忽略掉1和255
    sequence=`seq ${StartNum} ${EndNum}`
    
    for myseq in ${sequence}
    do 
        ipaddress="${IPSectsegment}.${myseq}"
       
        ping -c 1 ${ipaddress} > ${t_tmpfile}
        t_judge_ip=`grep "100% packet loss" ${t_tmpfile}`
        if [[ ! -z ${t_judge_ip} ]]
        then   
            #输出IP地址             
            echo ${ipaddress}           
            ((count++))
            if [[ ${count} -eq ${IPCounts} ]]
            then
                break
            fi
        fi
    done
   
    echo "============Get IPs End(OMSuite)==============="
   
    return 0
}

function usage
{
    echo "Usage: pingbutong -s [num1] -e [num2] -w [IP] -c [counts]"
    echo "     Note: [num2] is bigger [num1], [num1] and [num2] is between 2 and 254 number."
    echo "           [IP] is that 10.137.17 or 10.138.20 and so on."
    echo "           [counts] is that how many IP you want, default 5."
    echo "     For example: pingbutong -s 100 -e 200 -w 10.137.17 -c 5, same as pingbutong -s 100 -e 200 -w 10.137.17"
}

function main
{
    #check num
    checkStartNum=`echo $StartNum | egrep "^[0-9]{1,}$"`
    checkEndNum=`echo $EndNum | egrep "^[0-9]{1,}$"`
    checkIPSec=`echo ${IPSectsegment} | egrep "[1-9]{1,}.[0-9]{1,}.[0-9]{1,}"`   
    IPCounts=${IPCounts:-5}
    if [ -z "$checkStartNum" ] || [ -z "$checkEndNum" ] || [ -z "$checkIPSec" ]
    then
        usage
        return 1
    fi
   
    if [ -n "$StartNum" ] && [ -n "$EndNum" ] && [ -n "$IPSectsegment" ]
    then
        if [ ${StartNum} -gt ${EndNum} ]
        then
            usage
            return 1
        fi
       
        pingBuTong
       
    elif [ -z "$StartNum" ] || [ -z "$EndNum" ] || [ -n "$IPSectsegment" ]
    then
        usage
        return 1
    fi
           
}

while getopts :s:e:w:c: OPTION
do
    case $OPTION in
   s)
    StartNum=$OPTARG
    ;;
   e)
    EndNum=$OPTARG
    ;; 
    w)
      IPSectsegment=$OPTARG   
      ;;
    c)
      IPCounts=$OPTARG   
      ;;
     
   \?)
    echo "Command option is invalid!"
    ;;
   esac
done

main


文章来源:http://www.bozhiyue.com/cloud/2016/0825/432552.html
返回上一页    返回分类 上一篇:   下一篇:
相关