-
Requirement: Check whether the IP address is online
In order to prevent the online server from conflicting with the IP address of the online network, write a shell script to list and save the online IP addresses and offline IP addresses of the 20.0.0.0/24 network segment To the document. -
Analysis:
1. There are 254 public IP addresses in the 24 network segment, from 20.0.0.1 to 20.0.0.254, which need to be traversed in a for loop.
2. To see if an IP address is online, use the ping command to test -
step:
#!/bin/bash b="20.0.0." ###将网段赋值于b for a in `seq 1 254` ### 将 1-254个地址赋值于a,做for循环遍历 do ping -c 2 $b$a > /dev/null ###ping这个网段的所有地址,将其信息导入/dev/null文件中(ping中所带的-c 2 是2个包的意思,还有-i 、 -w; 示例:ping -c 2 -i 0.5 -w 3 意思为ping 2个包、间隔0.5秒、时延3秒) if [ $? -eq 0 ];then ####判定ping其中的网址参数是否为0 echo "$b$a is online" >> ip遍历1.txt ###为0 则输出这个网址是online状态的并追加到ip遍历.txt 这个文件中 else ###否则 echo "$b$a is not online" >> ip遍历2.txt ###则输出这个网址是not online状态的并追加到ip遍历.txt 这个文件中 fi done
-
Verification:
View the information in the file ip traversal 1.txt and file ip traversal 2.txt. The data in
ip traversal 1.txt are all online
. The data in ip traversal 2.txt are all not online.
——————————————————————————————
That's it, thanks for watching