shell 批量 ping 多个IP地址

版权声明:本文为博主原创文章,可以自由转载。 https://blog.csdn.net/u010953692/article/details/84778325

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 循环

猜你喜欢

转载自blog.csdn.net/u010953692/article/details/84778325
今日推荐