Shell脚本——业务上线前如何去扫描指定网段的所有IP地址呢?

一、为什么要使用这个脚本

  • 此章脚本可以在业务上线前,扫描你设置网段中的所有ip地址,可以查看哪些地址是没有被占用的,避免了资源浪费

二、实现过程

  • 通过for循环,一直循环ping测试ip地址,假设是用的C类网段的公有254个地址,会从1遍历至254,然后跳出循环结束脚本

三、脚本

#!/bin/bash
# 扫描192.168.1.网段中的从1遍历到254个地址
ip=192.168.1.
for((a=1;a<=254;a++))
do
  b=${
    
    ip}${
    
    a}
  ping -c 3 -i 0.2 -w 3  $b  &>> /dev/null
if [ $? -eq 0 ]
then
    echo "host $a is up" &>>up.txt
else
    echo "host $a is down" &>>down.txt
fi
done

猜你喜欢

转载自blog.csdn.net/F2001523/article/details/112258760