并发检测主机ip存活脚本

#!/bin/bash
###################
# Check Active Host#######
###################
function CheckInput(){
if [ $# -ne 1 ] ; then
  return 1
else
  return 0
fi
}

function CheckIp(){
/bin/ipcalc -s -c $1 &>/dev/null
return $?
}

function PingIp(){
ping -c 2 -W 2 $1 &>/dev/null
if [ $? -eq 0 ] ; then
  echo $1 >> $TMP_ACTIVE
else
  echo $1 >> $TMP_DOWN
fi
}

function PingAll(){
PrefixIP=$(echo $1|awk -F '.' '{print $1"."$2"."$3"."}')
for ip in {1..254}
do
  IP="${PrefixIP}${ip}"
  PingIp $IP &
done
}

function SelectPing(){
ENDIP=$(echo $1|awk -F '.' '{print $NF}')
if [ $ENDIP -eq 0 -o $ENDIP -eq 255 ] ; then
  PingAll $*
else
  PingIp $*
fi
}

function SortIp(){
while :
do
PING_NUM=$(ps -ef|grep ping|grep -v 'grep'|wc -l)
if [ $PING_NUM -eq 0 ] ; then
`cat $TMP_ACTIVE|sort -t '.' -k 4 -n > /tmp/active.host`
`cat $TMP_DOWN|sort -t '.' -k 4 -n > /tmp/down.host`
rm -f $TMP_ACTIVE
rm -f $TMP_DOWN
break
fi
done
}

function Show(){
ACTIVEIP=$(cat /tmp/active.host)
DOWNIP=$(cat /tmp/down.host)
if [ -s "/tmp/active.host" ] ; then
  echo "Active Host:"
  echo $ACTIVEIP|tr " " "\n"
else
  echo "Down Host:"
  echo $DOWNIP|tr " " "\n"
fi
}
function main(){
CheckInput $*
if [ $? -ne 0 ] ; then
  echo -e $"USAGE: $0 {ipaddress}\nExample:\n$0 192.168.1.10  will check 192.168.1.10 only\n$0 192.168.1.0 will check 192.168.1.1 - 192.168.1.254 "
else
  CheckIp $*
  if [ $? -ne 0 ] ; then
    echo "Please input correct ip address . such as 192.168.1.10 or 192.168.1.0"
  else
    TMP_ACTIVE=$(mktemp)
    TMP_DOWN=$(mktemp)
    >/tmp/active.host
    >/tmp/down.host
    SelectPing $*
    SortIp
    Show
    rm -f /tmp/active.host
    rm -f /tmp/down.host
  fi
fi
}

main $*

 

转载于:https://www.cnblogs.com/yangliheng/p/6379466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值