# -c n 限制发送的分组数量 # icmp_seq icmp 序号 # ttl time to live 剩余路由器转发的次数,次数为 0 时还没有到达目标主机,消息将被丢弃 # time RTT(Round Trip Time) 分组的往返时间 guo@DESKTOP-4L69AND:/mnt/e/learning-dir/shell-learning$ ping baidu.com -c 5 PING baidu.com (220.181.38.148) 56(84) bytes of data. 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=52 time=35.3 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=52 time=35.4 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=52 time=35.4 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=52 time=35.8 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=5 ttl=52 time=35.8 ms
--- baidu.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 5053ms rtt min/avg/max/mdev = 35.322/35.588/35.876/0.331 ms
ping 命令执行顺利,目标主机可达,退出状态为 0;否则非 0,目标主机不可达。
测试局域网下所有可达的主机:
1 2 3 4 5 6 7 8 9 10
#!/bin/bash
for ip in 192.168.31.{1..255} do echo"testing $ip" ping $ip -c 1 &> /dev/null ; if [ $? -eq 0 ];then echo$ip is alive fi done