您的位置:首页 > 运维架构 > Shell

Shell扫描所在网段存活主机

2018-07-20 20:49 330 查看
本例以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 脚本