linux中间隔10ping一次脚本,Linux下批量ping某个网段ip的脚本

好比如今须要对172.16.50.0/24网段的ip进行检查,检查哪些ip如今被占用,哪些ip没有被占用,能够经过ping命令来检查,脚本以下:shell

[root@uatdns01 opt]# vim /opt/ping.sh

#!/bin/bash

. /etc/init.d/functions

for var in {1..254};

do

ip=172.16.50.$var

ping -c2 $ip >/dev/null 2>&1

if [ $? = 0 ];then

action "$ip" /bin/true

else

action "$ip" /bin/false

fi

done

[root@uatdns01 opt]# chmod 755 /opt/ping.sh

[root@uatdns01 opt]# sh /opt/ping.sh

172.16.50.1 [ OK ]

172.16.50.2 [FAILED]

172.16.50.3 [FAILED]

172.16.50.4 [FAILED]

172.16.50.5 [FAILED]

.........

.........

172.16.50.99 [ OK ]

172.16.50.100 [ OK ]

172.16.50.101 [ OK ]

172.16.50.102 [ OK ]

172.16.50.103 [ OK ]

172.16.50.104 [ OK ]

172.16.50.105 [ OK ]

172.16.50.106 [ OK ]

172.16.50.107 [ OK ]

172.16.50.108 [ OK ]

172.16.50.109 [ OK ]

172.16.50.110 [ OK ]

172.16.50.111 [FAILED]

172.16.50.112 [ OK ]

获取172.16.60.0/24网段可用ip的shell脚本

[root@ansible-server ~]# cat ip_ping.sh

#!/bin/bash

ip=1

while [ $ip != "254" ]; do

ping 172.16.60.$ip -c 2 | grep -q "ttl=" && echo "172.16.60.$ip yes" || echo "172.16.60.$ip no"

ip=`expr "$ip" "+" "1"`

done

执行:

[root@ansible-server ~]# sh ip_ping.sh

172.16.60.1 yes

172.16.60.2 no

172.16.60.3 no

.............

172.16.60.24 yes

172.16.60.25 yes

172.16.60.26 no

.............

若是只打印可用ip, 则脚本修改以下:

[root@ansible-server ~]# cat ip_ping.sh

#!/bin/bash

ip=1

while [ $ip != "254" ]; do

ping 172.16.60.$ip -c 2 | grep -q "ttl=" && echo "172.16.60.$ip yes" || echo "172.16.60.$ip no" >/dev/null 2>&1

ip=`expr "$ip" "+" "1"`

done

执行:

[root@ansible-server ~]# sh ip_ping.sh

172.16.60.1 yes

172.16.60.21 yes

172.16.60.22 yes

172.16.60.23 yes

172.16.60.24 yes

172.16.60.25 yes

172.16.60.31 yes

172.16.60.32 yes

172.16.60.33 yes

172.16.60.34 yes

172.16.60.35 yes

172.16.60.36 yes

172.16.60.37 yes

172.16.60.38 yes

172.16.60.39 yes

获取172.16.50.0/24, 172.16.51.0/24, 172.16.60.0/24 三个网段的可用ip

[root@ansible-server ~]# cat ip_ping.sh

#!/bin/bash

ip=1

while [ $ip != "254" ]; do

ping 172.16.50.$ip -c 2 | grep -q "ttl=" && echo "172.16.50.$ip yes" || echo "172.16.50.$ip no" >/dev/null 2>&1

ip=`expr "$ip" "+" "1"`

ping 172.16.51.$ip -c 2 | grep -q "ttl=" && echo "172.16.51.$ip yes" || echo "172.16.51.$ip no" >/dev/null 2>&1

ip=`expr "$ip" "+" "1"`

ping 172.16.60.$ip -c 2 | grep -q "ttl=" && echo "172.16.60.$ip yes" || echo "172.16.60.$ip no" >/dev/null 2>&1

ip=`expr "$ip" "+" "1"`

done

执行:

[root@ansible-server ~]# sh ip_ping.sh

172.16.50.1 yes

172.16.51.11 yes

172.16.50.16 yes

172.16.50.19 yes

172.16.51.20 yes

172.16.60.21 yes

172.16.50.22 yes

172.16.60.24 yes

172.16.50.25 yes

172.16.50.31 yes

172.16.60.33 yes

172.16.51.35 yes

172.16.60.36 yes

172.16.60.39 yes

172.16.51.41 yes

172.16.51.44 yes

172.16.50.52 yes

172.16.51.53 yes

172.16.50.55 yes

172.16.50.58 yes

172.16.51.65 yes

..................

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值