Linux下批量ping某個網段的腳本


比如現在需要對192.168.0.0/24網段的ip進行檢查,檢查哪些ip現在被占用,哪些ip沒有被占用,可以通過ping命令來檢查,也可以通過nmap接參數來檢查

ping命令腳本如下:

[root@ZFVM-APP-0-172 shell]# vim ping.sh
#!/bin/bash
. /etc/init.d/functions
for var in {1..254};
do
ip=192.168.0.$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]# bash ping.sh
172.168.0.1                                                [FAILED]
172.168.0.2                                                [FAILED]
172.168.0.3                                                [FAILED]
172.168.0.4                                                [FAILED]
172.168.0.5                                                [FAILED]
.........
.........
192.168.0.249                                              [FAILED]
192.168.0.250                                              [FAILED]
192.168.0.251                                              [FAILED]
192.168.0.252                                              [FAILED]
192.168.0.253                                              [FAILED]
192.168.0.254                                              [FAILED]

用nmap需要先安裝nmap命令

[root@ZFVM-APP-0-172 shell]# yum install -y nmap

[root@ZFVM-APP-0-172 shell]# nmap -v -sP 192.168.0.0/24 |grep down
Nmap scan report for 192.168.0.0 [host down]
Nmap scan report for 192.168.0.2 [host down]
Nmap scan report for 192.168.0.3 [host down]
Nmap scan report for 192.168.0.4 [host down]
Nmap scan report for 192.168.0.5 [host down]
......
......
Nmap scan report for 192.168.0.251 [host down]
Nmap scan report for 192.168.0.252 [host down]
Nmap scan report for 192.168.0.253 [host down]
Nmap scan report for 192.168.0.254 [host down]
Nmap scan report for 192.168.0.255 [host down]

 檢查192.168.0.1網關是否可達的腳本

[root@ZFVM-APP-0-161 shells]# vim ping.sh 
#!/bin/bash
ping www.baidu.com -c 4 -W 5 > /dev/null 2>&1
if [ $? -eq 0 ]
then
  echo "網絡已通"
else
  echo "網絡不可達"
fi

 

執行結果

[root@ZFVM-APP-0-161 shells]# sh ping.sh 
網絡可通

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM