Shell扫描所在网段存活主机

本例以c类地址为例,如果是A类或者B类地址,将脚本稍作修改即可

ipaddr=`/usr/sbin/ip a | grep $1 | grep inet | awk '{print $2}'`
netmask=`echo ${ipaddr} | awk -F "/" '{print $2}'`
if [ $netmask -eq 24 ];then
    subnets=`echo ${ipaddr} | awk -F "." '{print $1"."$2"."$3"."}'`
    acount=0
    for i in `seq 1 25`
    do
        /usr/bin/ping ${subnets}${i} -c 2 > /dev/null
        if [ $? -eq 0 ];then
            echo "${subnets}$i is online"
            let count+=1
        else
            echo "${subnets}$i is not online"
        fi
    done
fi      
echo "There are $count hosts onlie"

执行脚本时需要加上参数(网卡的连接名,如ens33)

猜你喜欢

转载自blog.51cto.com/13434336/2147879