root:~#ping -c 3 -W 2 baidu.com || echo ok
PING baidu.com (220.181.111.86) 56(84) bytes of data.
64 bytes from 220.181.111.86: icmp_seq=1 ttl=128 time=54.3 ms
64 bytes from 220.181.111.86: icmp_seq=2 ttl=128 time=33.4 ms
--- baidu.com ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2002ms
rtt min/avg/max/mdev = 33.407/43.866/54.325/10.459 ms
PING baidu.com (220.181.111.86) 56(84) bytes of data.
64 bytes from 220.181.111.86: icmp_seq=1 ttl=128 time=54.3 ms
64 bytes from 220.181.111.86: icmp_seq=2 ttl=128 time=33.4 ms
--- baidu.com ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2002ms
rtt min/avg/max/mdev = 33.407/43.866/54.325/10.459 ms
root:~#
可以看到多次ping的话,只要有响应的就认为是成功. 全部失败才认为是失败
据此可以写个一句话脚本, 每隔2秒钟 ping 某IP一次, 如果通了则播放音乐:
watch 'ping -c 1 -W 2 x.x.x.x && smplayer /root/profile/chuntian.mp3'
或者每隔2秒钟 ping 某 IP 3次, 如果全都不通则播放音乐:
watch 'ping -c 3 -W 2 x.x.x.x || smplayer /root/profile/chuntian.mp3'