我使用过的Linux命令之ping - 测试与目标主机的连通性

我使用过的Linux命令之ping - 测试与目标主机的连通性

本文链接:http://codingstandards.iteye.com/blog/1125766   (转载请注明出处)

用途说明

ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”、不能打开网页时会说“你先ping网关地址192.168.1.1试试”。它通过发送ICMP ECHO_REQUEST数据包到网络主机(send ICMP ECHO_REQUEST to network hosts),并显示响应情况,这样我们就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的)。有些服务器为了防止通过ping探测到,通过防火墙设置了禁止ping或者在内核参数中禁止ping,这样就不能通过ping确定该主机是否还处于开启状态。

man ping 写道
ping uses the ICMP protocol’s mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or
gateway. ECHO_REQUEST datagrams (‘‘pings’’) have an IP and ICMP header, followed by a struct timeval and then
an arbitrary number of ‘‘pad’’ bytes used to fill out the packet.

上面引文中 elicit  引出  http://www.iciba.com/elicit/

ping命令用于:
  * 确定网络和各外部主机的状态。
  * 跟踪和隔离硬件和软件问题。
  * 测试、评估和管理网络。

维基百科上关于ping的说明

ping - 维基百科,自由的百科全书 写道
ping是:一个电脑网络工具,用来测试特定主机能否通过IP到达。ping的运作原理是:向目标主机传出一个ICMPecho要求分组,等待接收echo回应分组。程序会按时间和反应成功的次数,估计失去分组率(丢包率)和分组来回时间(网络时延)(Round-trip delay time)。

1983年12月,Mike Muuss写了这个程序,在IP网络出问题时方便找出其根源。因为这个程序的运作和潜水艇的声纳相似,他便用声纳的声音来为程序取名。David L. Mills曾提出另一个取名:Packet Internet Grouper/Gopher(后者指地鼠)。

网络管理员之间通常把ping用作动词,如“ping一下计算机X,看他是否开着。”
 

根据ping输出的ttl值,可以大体确定目标系统的操作系统类型:

TTL=32 Windows 9x/Me
TTL=64 LINUX
TTL=128 Windows 200x/XP
TTL=255 Unix

常用参数

格式:ping {IP}

格式:ping {HOST}

测试与指定ip地址{IP}或者主机名{HOST}的目标主机的连通性。按Ctrl+C终止,否则会一直执行下去。

友情提示:在Windows的cmd中要想达到同样的效果,要加上-t参数,如 ping -t 192.168.1.103

格式:ping -b {BROADCAST-ADDRESS}

ping广播地址(Allow pinging a broadcast address)。可以大体确定本网络中有哪些主机能访问。

广播地址{BROADCAST-ADDRESS}的形式如:192.168.1.255,192.168.255.255。

格式:ping -c {n} {IP_OR_HOST}

增加-c参数用于指定次数{n},这样在发送了{n}次ICMP数据包后,如果收到了响应或者超时就终止执行。

格式:ping -i {INTERVAL} {IP_OR_HOST}

增加-i参数用于指定发送ICMP数据包的时间间隔,以秒为单位,可以为小数,普通用户最小为0.2秒,只有root用户才能指定比0.2秒小的间隔。如果不指定此参数,则默认的时间间隔是1秒。

man ping 写道
Wait interval seconds between sending each packet. The default is to wait for one second between each
packet normally, or not to wait in flood mode. Only super-user may set interval to values less 0.2 sec-
onds.

格式:ping -f {IP_OR_HOST}

指定flood-ping,只有root用户才能干。flood-ping,顾名思义,就是像洪水一样的发送ICMP数据包。Ping淹没是一种Ping广播风暴,淹没整个目标系统,以至于该系统不能响应合法的通信。

man 写道
-f Flood ping. For every ECHO_REQUEST sent a period ‘‘.’’ is printed, while for ever ECHO_REPLY received a
backspace is printed. This provides a rapid display of how many packets are being dropped. If interval
is not given, it sets interval to zero and outputs packets as fast as they come back or one hundred
times per second, whichever is more. Only the super-user may use this option with zero interval.
 

使用示例

示例一 ping得通的例子

[root@jfht ~]# ping 192.168.1.181
PING 192.168.1.181 (192.168.1.181) 56(84) bytes of data.
64 bytes from 192.168.1.181: icmp_seq=1 ttl=64 time=0.235 ms
64 bytes from 192.168.1.181: icmp_seq=2 ttl=64 time=0.127 ms
64 bytes from 192.168.1.181: icmp_seq=3 ttl=64 time=0.126 ms

--- 192.168.1.181 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.126/0.162/0.235/0.053 ms
[root@jfht ~]#

示例二 ping不通的例子

[root@jfht ~]# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
From 192.168.1.191 icmp_seq=2 Destination Host Unreachable
From 192.168.1.191 icmp_seq=3 Destination Host Unreachable
From 192.168.1.191 icmp_seq=4 Destination Host Unreachable

--- 192.168.1.1 ping statistics ---
7 packets transmitted, 0 received, +3 errors, 100% packet loss, time 5999ms, pipe 3
[root@jfht ~]#

示例三 ping广播地址

[root@jfht ~]# ping -b 192.168.1.255
WARNING: pinging broadcast address
PING 192.168.1.255 (192.168.1.255) 56(84) bytes of data.
64 bytes from 192.168.1.186: icmp_seq=1 ttl=64 time=0.214 ms
64 bytes from 192.168.1.178: icmp_seq=1 ttl=64 time=0.299 ms (DUP!)
64 bytes from 192.168.1.181: icmp_seq=1 ttl=64 time=0.305 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=1 ttl=64 time=0.328 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=2 ttl=64 time=0.156 ms
64 bytes from 192.168.1.181: icmp_seq=2 ttl=64 time=0.162 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=2 ttl=64 time=0.164 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=2 ttl=64 time=0.298 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=3 ttl=64 time=0.153 ms
64 bytes from 192.168.1.181: icmp_seq=3 ttl=64 time=0.158 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=3 ttl=64 time=0.161 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=3 ttl=64 time=0.163 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=4 ttl=64 time=0.158 ms
64 bytes from 192.168.1.181: icmp_seq=4 ttl=64 time=0.168 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=4 ttl=64 time=0.171 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=4 ttl=64 time=0.174 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=5 ttl=64 time=0.154 ms
64 bytes from 192.168.1.181: icmp_seq=5 ttl=64 time=0.159 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=5 ttl=64 time=0.162 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=5 ttl=64 time=0.164 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=6 ttl=64 time=0.145 ms
64 bytes from 192.168.1.181: icmp_seq=6 ttl=64 time=0.151 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=6 ttl=64 time=0.154 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=6 ttl=64 time=0.240 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=7 ttl=64 time=0.146 ms
64 bytes from 192.168.1.181: icmp_seq=7 ttl=64 time=0.152 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=7 ttl=64 time=0.155 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=7 ttl=64 time=0.211 ms (DUP!)
64 bytes from 192.168.1.181: icmp_seq=8 ttl=64 time=0.147 ms
64 bytes from 192.168.1.189: icmp_seq=8 ttl=64 time=0.156 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=8 ttl=64 time=0.159 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=8 ttl=64 time=0.272 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=9 ttl=64 time=0.150 ms
64 bytes from 192.168.1.181: icmp_seq=9 ttl=64 time=0.156 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=9 ttl=64 time=0.159 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=9 ttl=64 time=0.210 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=10 ttl=64 time=0.157 ms
64 bytes from 192.168.1.181: icmp_seq=10 ttl=64 time=0.163 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=10 ttl=64 time=0.165 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=10 ttl=64 time=0.168 ms (DUP!)
64 bytes from 192.168.1.189: icmp_seq=11 ttl=64 time=0.134 ms
64 bytes from 192.168.1.181: icmp_seq=11 ttl=64 time=0.140 ms (DUP!)
64 bytes from 192.168.1.186: icmp_seq=11 ttl=64 time=0.172 ms (DUP!)
64 bytes from 192.168.1.178: icmp_seq=11 ttl=64 time=0.241 ms (DUP!)

--- 192.168.1.255 ping statistics ---
11 packets transmitted, 11 received, +33 duplicates, 0% packet loss, time 10000ms
rtt min/avg/max/mdev = 0.134/0.181/0.328/0.049 ms
[root@jfht ~]#

示例四 ping指定次数

[root@jfht ~]# ping -c 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
From 192.168.1.191 icmp_seq=1 Destination Host Unreachable

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

[root@jfht ~]# ping -c 1 192.168.1.181
PING 192.168.1.181 (192.168.1.181) 56(84) bytes of data.
64 bytes from 192.168.1.181: icmp_seq=1 ttl=64 time=1.06 ms

--- 192.168.1.181 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.061/1.061/1.061/0.000 ms
[root@jfht ~]#

[root@jfht ~]# ping -c 4 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
From 192.168.1.191 icmp_seq=1 Destination Host Unreachable
From 192.168.1.191 icmp_seq=2 Destination Host Unreachable
From 192.168.1.191 icmp_seq=3 Destination Host Unreachable
From 192.168.1.191 icmp_seq=4 Destination Host Unreachable

--- 192.168.1.1 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 2999ms, pipe 4
[root@jfht ~]#

示例五 ping不可达的网络地址

[root@jfht ~]# ping -c 4 192.168.100.123
PING 192.168.100.123 (192.168.100.123) 56(84) bytes of data.

--- 192.168.100.123 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3008ms

为了达到网络不可达的效果,我们增加一条拒绝路由。
[root@jfht ~]# route add -net 192.168.100.0 netmask 255.255.255.0 reject
[root@jfht ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
211.103.28.0    *               255.255.255.224 U     0      0        0 eth0
192.168.100.0   -               255.255.255.0   !     0      -        0 -
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
10.0.0.0        -               255.0.0.0       !     0      -        0 -
default         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
[root@jfht ~]# ping -c 4 192.168.100.123                                
connect: Network is unreachable
[root@jfht ~]#

示例六 flood-ping测试

ping -f 指定 flood-ping 选项。 -f 标志“倾倒”或输出信息包,在它们回来时或每秒 100 次,选择较快一个。每一次发送 ECHO_REQUEST,都打印一个句号,而每接收到一个 ECHO_REPLY 信号,就打印一个退格。这就提供了一种对多少信息包被丢弃的信息的快速显示。仅仅 root 用户可以使用这个选项。

[root@jfht ~]# ping -f 192.168.8.1
PING 192.168.8.1 (192.168.8.1) 56(84) bytes of data.
.................................................................................................................................................................................................................................................................................................................................................................................................................................................
--- 192.168.8.1 ping statistics ---
433 packets transmitted, 0 received, 100% packet loss, time 4672ms

[root@jfht ~]# ping -f 192.168.1.8
PING 192.168.1.8 (192.168.1.8) 56(84) bytes of data.
...................................................................................................................................................................................................................................................................................................E.........................................................................................................................................................................................................................................................................................................
--- 192.168.1.8 ping statistics ---
589 packets transmitted, 0 received, +3 errors, 100% packet loss, time 5995ms
, pipe 3
[root@jfht ~]#
[root@jfht ~]# ping -f 192.168.1.181
PING 192.168.1.181 (192.168.1.181) 56(84) bytes of data.
.
--- 192.168.1.181 ping statistics ---
18959 packets transmitted, 18958 received, 0% packet loss, time 2986ms
rtt min/avg/max/mdev = 0.093/0.103/1.084/0.017 ms, pipe 2, ipg/ewma 0.157/0.100 ms
[root@jfht ~]#

示例七 发送时间间隔测试

[root@jfht ~]# ping -c 10 -i 0.5 www.g.cn
PING www.g.cn (203.208.46.145) 56(84) bytes of data.
64 bytes from 203.208.46.145: icmp_seq=1 ttl=50 time=48.6 ms
64 bytes from 203.208.46.145: icmp_seq=2 ttl=50 time=29.7 ms
64 bytes from 203.208.46.145: icmp_seq=3 ttl=50 time=61.9 ms
64 bytes from 203.208.46.145: icmp_seq=4 ttl=50 time=33.1 ms
64 bytes from 203.208.46.145: icmp_seq=5 ttl=50 time=44.3 ms
64 bytes from 203.208.46.145: icmp_seq=6 ttl=50 time=37.0 ms
64 bytes from 203.208.46.145: icmp_seq=7 ttl=50 time=37.5 ms
64 bytes from 203.208.46.145: icmp_seq=8 ttl=50 time=48.0 ms
64 bytes from 203.208.46.145: icmp_seq=9 ttl=50 time=37.0 ms
64 bytes from 203.208.46.145: icmp_seq=10 ttl=50 time=45.3 ms

--- www.g.cn ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 4502ms
rtt min/avg/max/mdev = 29.762/42.303/61.908/8.885 ms
[root@jfht ~]# ping -c 10 -i 0.01 www.g.cn
PING www.g.cn (203.208.46.144) 56(84) bytes of data.
64 bytes from 203.208.46.144: icmp_seq=2 ttl=50 time=30.5 ms
64 bytes from 203.208.46.144: icmp_seq=1 ttl=50 time=45.1 ms
64 bytes from 203.208.46.144: icmp_seq=3 ttl=50 time=30.9 ms
64 bytes from 203.208.46.144: icmp_seq=4 ttl=50 time=42.6 ms
64 bytes from 203.208.46.144: icmp_seq=5 ttl=50 time=42.7 ms
64 bytes from 203.208.46.144: icmp_seq=7 ttl=50 time=30.1 ms
64 bytes from 203.208.46.144: icmp_seq=6 ttl=50 time=41.2 ms
64 bytes from 203.208.46.144: icmp_seq=8 ttl=50 time=34.3 ms
64 bytes from 203.208.46.144: icmp_seq=9 ttl=50 time=45.4 ms
64 bytes from 203.208.46.144: icmp_seq=10 ttl=50 time=41.3 ms

--- www.g.cn ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 158ms
rtt min/avg/max/mdev = 30.163/38.468/45.439/5.926 ms, pipe 5
[root@jfht ~]# su - mike
[mike@jfht ~]$ ping -c 10 -i 0.01 www.g.cn
PING www.g.cn (203.208.46.148) 56(84) bytes of data.
ping: cannot flood; minimal interval, allowed for user, is 200ms
[mike@jfht ~]$ exit
logout

[root@jfht ~]#

示例八 一个探测局域网中哪些机器能ping通的脚本

下面展示了一个脚本,用来探测局域网中哪些主机能ping通。来自鸟哥的私房菜,稍作了修改。

[root@jfht ~]# cat >ping.sh
#!/bin/bash
for siteip in $(seq 1 254)
do
        site="192.168.1.${siteip}"
        ping -c1 -W1 ${site} &> /dev/null
        if [ "$?" == "0" ]; then
                echo "$site is UP"
        else
                : #echo "$site is DOWN"
        fi
done


[root@jfht ~]# chmod +x ping.sh
[root@jfht ~]# ./ping.sh
192.168.1.178 is UP
192.168.1.181 is UP
192.168.1.186 is UP
192.168.1.189 is UP
192.168.1.191 is UP
192.168.1.193 is UP
192.168.1.194 is UP
[root@jfht ~]#

示例九 ping一下公网上的主机

在不能上网的时候,可以通过ping公网主机来确定能否上网。可以确定域名是否能解析,远程主机是否可访问等。

[root@jfht ~]# ping -c 4 en.wikipedia.org
PING text.pmtpa.wikimedia.org (208.80.152.2) 56(84) bytes of data.
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=1 ttl=43 time=258 ms
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=2 ttl=43 time=265 ms
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=3 ttl=43 time=266 ms
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=4 ttl=43 time=264 ms

--- text.pmtpa.wikimedia.org ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2998ms
rtt min/avg/max/mdev = 258.492/263.998/266.927/3.315 ms
[root@jfht ~]# ping -c 4 zh.wikipedia.org
PING text.pmtpa.wikimedia.org (208.80.152.2) 56(84) bytes of data.
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=1 ttl=43 time=258 ms
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=2 ttl=43 time=258 ms
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=3 ttl=43 time=259 ms
64 bytes from rr.pmtpa.wikimedia.org (208.80.152.2): icmp_seq=4 ttl=43 time=257 ms

--- text.pmtpa.wikimedia.org ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 257.679/258.435/259.326/0.590 ms
[root@jfht ~]# ping -c 4 www.google.com
PING www.l.google.com (74.125.71.147) 56(84) bytes of data.
64 bytes from hx-in-f147.1e100.net (74.125.71.147): icmp_seq=1 ttl=48 time=74.6 ms
64 bytes from hx-in-f147.1e100.net (74.125.71.147): icmp_seq=2 ttl=48 time=74.4 ms
64 bytes from hx-in-f147.1e100.net (74.125.71.147): icmp_seq=3 ttl=48 time=74.3 ms
64 bytes from hx-in-f147.1e100.net (74.125.71.147): icmp_seq=4 ttl=48 time=77.4 ms

--- www.l.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 74.358/75.236/77.467/1.335 ms
[root@jfht ~]# ping -c 4 www.google.com.hk
PING www-hk.l.google.com (74.125.71.99) 56(84) bytes of data.
64 bytes from hx-in-f99.1e100.net (74.125.71.99): icmp_seq=1 ttl=48 time=74.5 ms
64 bytes from hx-in-f99.1e100.net (74.125.71.99): icmp_seq=2 ttl=48 time=74.3 ms
64 bytes from hx-in-f99.1e100.net (74.125.71.99): icmp_seq=3 ttl=48 time=74.7 ms
64 bytes from hx-in-f99.1e100.net (74.125.71.99): icmp_seq=4 ttl=48 time=74.9 ms

--- www-hk.l.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 74.366/74.657/74.987/0.411 ms
[root@jfht ~]# ping -c 4 www.g.cn
PING www.g.cn (203.208.46.144) 56(84) bytes of data.
64 bytes from 203.208.46.144: icmp_seq=1 ttl=50 time=109 ms
64 bytes from 203.208.46.144: icmp_seq=2 ttl=50 time=110 ms
64 bytes from 203.208.46.144: icmp_seq=3 ttl=50 time=95.5 ms
64 bytes from 203.208.46.144: icmp_seq=4 ttl=50 time=114 ms

--- www.g.cn ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 95.566/107.616/114.506/7.195 ms
[root@jfht ~]#

问题思考

相关资料

【1】中国IT实验室 Linux和Windows下ping命令详解
http://linux.chinaitlab.com/command/829332.html
【2】网络小筑 根据TTL值判断操作系统类型(转)
http://webtrados.llh4.com/post/375.html

【3】Blinux linux ping 测试本机和目标主机连通性
http://codingstandards.iteye.com/admin/blogs/new

【4】维基百科,自由的百科全书 ping
http://zh.wikipedia.org/wiki/Ping
【5】ping的原作者Mike的个人网站 The Story of the PING Program
http://ftp.arl.mil/~mike/ping.html

【6】xuelanghu.cublog.cn linux中c语言实现的ping程序
http://blogold.chinaunix.net/u2/62117/showart_1756522.html

【7】鸟哥的Linux私房菜 兩部主機兩點溝通: ping
http://linux.vbird.org/linux_server/0140networkcommand.php#ping

返回 我使用过的Linux命令系列总目录

猜你喜欢

转载自codingstandards.iteye.com/blog/1125766