《通过脚本查看哪些ip被占用》shell笔记


改脚本查看哪些ip被占用。

#!/bin/bash

for i in {1..10}   //赋予i变量1-10

do   //干什么

ping -c1 -w1 192.168.7.$i &> /dev/null   //ping 192.168.7.网段 每个ip1次 显示1行全输出到无底洞

if [ $? -eq 0 ]; //返回值是否为0

then   //如果返回值为0则做下面的输出

echo station$i is up!

else   //否则

echo station$i is down!  //输出这步

fi   done 

赋予该脚本可执行权限: chmod +x ipadd.sh  

执行该脚本:

[root@localhost shellscripts]# ipadd.sh

station1 is up!

station2 is down!

station3 is up!

station4 is down!

station5 is down!

station6 is down!

station7 is down!

station8 is down!

station9 is down!

station10 is down!

===========================

[root@localhost shellscripts]# ping 192.168.7.3

PING 192.168.7.3 (192.168.7.3) 56(84) bytes of data. 64 bytes from 192.168.7.3:

icmp_seq=1 ttl=64 time=0.043 ms 64 bytes from 192.168.7.3:

icmp_seq=2 ttl=64 time=0.040 ms ^C --- 192.168.7.3

ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1791ms rtt min/avg/max/mdev = 0.040/0.041/0.043/0.006 ms

[root@localhost shellscripts]# echo $?   //ping通返回值为0

0 ==============================

[root@localhost shellscripts]# ping 192.168.7.4

PING 192.168.7.4 (192.168.7.4) 56(84) bytes of data.

From 192.168.7.3 icmp_seq=1 Destination Host Unreachable From 192.168.7.3

icmp_seq=2 Destination Host Unreachable From 192.168.7.3

icmp_seq=3 Destination Host Unreachable ^C --- 192.168.7.4

ping statistics --- 5 packets transmitted, 0 received, +3 errors, 100% packet loss, time 4084ms pipe 3

[root@localhost shellscripts]# echo $? 1     //ping不通返回值不为0

 

相关内容