1. 程式人生 > >shell 批量 ping 多個IP地址

shell 批量 ping 多個IP地址

ping多個IP地址檢視連線狀態

1,系統環境

$ sw_vers 
ProductName:	Mac OS X
ProductVersion:	10.13.6
BuildVersion:	17G65

2,shell 指令碼

#!/bin/bash
#批量ping IP地址
#ip_record.text文字一行一個IP地址
#獲取ip 是否可以ping通


ipAll=$(cat /root/ip_record.text)

for ip in $ipAll
do 
echo $ip
value=
$(ping -c 1 $ip |grep packets |awk '{print $7}' |awk -F % '{print $1}') num=${value%.*} time=$(ping -c 1 $ip |grep time |awk '{print $7}') echo $time if [ $num == 100 ];then echo $num fail else echo $num ok fi done

輸出結果

192.168.1.1
time=30.663
0 ok
192.168.2.1
time=23.794
0 ok

參考:

  1. shell 編寫指令碼批量ping ip
  2. shell 中 [-eq] [-ne] [-gt] [-lt] [ge] [le]
  3. Shell指令碼語法-- if/then/elif/else/fi
  4. Linux awk 命令
  5. shell ——for in 迴圈