shell脚本-------------企业上线新项目 检查上线的IP是否被占用

项目需求:

当企业项目完成后需上线,在上线前都会进行检测,为了不让上线的服务器,不与线网的IP地址相冲突,我们就用shell脚本,把192.168.100.0/24网段在线的IP地址和不在线的IP地址列出来并保存到文档中。

步骤分析

  1. 24网段公有254个IP地址,从192.168.100.1到192.168.1.254,需要用for循环进行遍历。
  2. 查看某个IP地址是否在线,主要用ping命令来进行测试。

脚本实现:

#!/bin/bash
ips="20.0.0."
for i in `seq 1 254`
do
ping -c 3 -i 0.2 -W 3 $ips$i &> /dev/null
if [ $? -eq 0 ]
then
        echo "$ips$i is up" >> /opt/ipup.txt
else
        echo "$ips$i is done" >> /opt/ipdown.txt
fi
done

注释:
-c:发送包的数量
-i:发送ping包间隔
-W:ping的延时时间

猜你喜欢

转载自blog.csdn.net/weixin_48190875/article/details/108244401