第一个问题:如何缩短ping的次数,减少输出内容,以及自动结束ping?
[root@server1 shells]# ping -c1 www.baidu.com //通过-c设置次数,减少输出内容以及减少ping的时间
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=37.1 ms
--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 37.196/37.196/37.196/0.000 ms
第二个问题:什么结果显示网络通的?
通的时候:
[root@server1 shells]# ping -c1 www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=45.8 ms
--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 45.804/45.804/45.804/0.000 ms
[root@server1 shells]# echo $?
0
不通的时候:
[root@server1 shells]# ping -c1 192.168.13.134
PING 192.168.13.134 (192.168.13.134) 56(84) bytes of data.
From 192.168.13.129 icmp_seq=1 Destination Host Unreachable
--- 192.168.13.134 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
[root@server1 shells]# echo $?
1
所以我们可以通过上一次程序返回值来判断
下面直接放程序:
#!/bin/bash //调用解释器
read -p "input the id or dns" CONTEXT //使用交互模式,提示用户输入要ping的主机或域名
ping -c1 $CONTEXT &> /dev/null //将ping后要在屏幕显示的覆盖到空文件中
a=$? //定义上一个程序运行结果为a
if [ $a -eq 0 ] /通过if判断,如果运行结果为0,则网络通畅。否则不通畅
then
echo "ok"
else
echo "no"
本文介绍如何利用Ping命令的-c参数精简Ping操作,减少输出内容,并自动结束Ping,以此快速检测网络连通性。同时提供了一个简单的Shell脚本示例,用于判断目标地址是否可达。

807

被折叠的 条评论
为什么被折叠?



