1. 程式人生 > >Shell掃描所在網段存活主機

Shell掃描所在網段存活主機

mas 需要 所在 sbin 網卡 ets shel toolbar dev

本例以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)

Shell掃描所在網段存活主機