每天一个linux命令(54):ping命令

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

linux下的ping和windows下的ping稍有区别,linux下ping不会自动终止,需要按ctrl+c终止或者用参数-c指定要求完成的回应次数。

1.命令格式:

ping[参数][主机名或IP地址]

2.命令功能:

ping命令用于:确定网络和各外部主机的状态;跟踪和隔离硬件和软件问题;测试、评估和管理网络。如果主机正在运行并连在网上,它就对回送信号进行响应。每个回送信号请求包含一个网际协议(IP)和ICMP头,后面紧跟一个tim结构,以及来填写这个信息包的足够的字节。缺省情况是连续发送回送信号请求直到接收到中断信号(Ctrl-C)。

#p#分页标题#e#ping命令每秒发送一个数据报并且为每个接收到的响应打印一行输出。ping命令计算信号往返时间和(信息)包丢失情况的统计信息,并且在完成之后显示一个简要总结。ping命令在程序超时或当接收到SIGINT信号时结束。Host参数或者是一个有效的主机名或者是因特网地址。

3.命令参数:

-d使用Socket的SO_DEBUG功能。

-f极限检测。大量且快速地送网络封包给一台机器,看它的回应。

-n只输出数值。

-q不显示任何传送封包的信息,只显示最后的结果。

-r忽略普通的RoutingTable,直接将数据包送到远端主机上。通常是查看本机的网络接口是否有问题。

-R记录路由过程。

-v详细显示指令的执行过程。

<p>-c数目:在发送指定数目的包后停止。

#p#分页标题#e#-i秒数:设定间隔几秒送一个网络封包给一台机器,预设值是一秒送一次。

-I网络界面:使用指定的网络界面送出数据包。

-l前置载入:设置在送出要求信息之前,先行发出的数据包。

-p范本样式:设置填满数据包的范本样式。

-s字节数:指定发送的数据字节数,预设值是56,加上8字节的ICMP头,一共是64ICMP数据字节。

-t存活数值:设置存活数值TTL的大小。

4.使用实例:

实例1:ping的通的情况

#p#分页标题#e#命令:

ping192.168.120.205

输出:

复制代码

[root@localhost~]#ping192.168.120.205

PING192.168.120.205(192.168.120.205)56(84)bytesofdata.

64bytesfrom192.168.120.205:icmp_seq=1ttl=64time=0.720ms

64bytesfrom192.168.120.205:icmp_seq=2ttl=64time=0.181ms

64bytesfrom192.168.120.205:icmp_seq=3ttl=64time=0.191ms

64bytesfrom192.168.120.205:icmp_seq=4ttl=64time=0.188ms

64bytesfrom192.168.120.205:icmp_seq=5ttl=64time=0.189ms

---192.168.120.205pingstatistics---

5packetstransmitted,5received,0%packetloss,time4000ms

rttmin/avg/max/mdev=0.181/0.293/0.720/0.214ms

[root@localhost~]#

复制代码

说明:

#p#分页标题#e#

实例2:ping不通的情况

命令:

ping192.168.120.202

输出:

复制代码

[root@localhost~]#ping192.168.120.202

PING192.168.120.202(192.168.120.202)56(84)bytesofdata.

From192.168.120.204icmp_seq=1DestinationHostUnreachable

From192.168.120.204icmp_seq=2DestinationHostUnreachable

From192.168.120.204icmp_seq=3DestinationHostUnreachable

From192.168.120.204icmp_seq=4DestinationHostUnreachable

From192.168.120.204icmp_seq=5DestinationHostUnreachable

From192.168.120.204icmp_seq=6DestinationHostUnreachable

---192.168.120.202pingstatistics---

8packetstransmitted,0received,+6errors,100%packetloss,time7005ms

,pipe4

[root@localhost~]#

#p#分页标题#e#复制代码

说明:

实例3:ping网关

命令:

ping-b192.168.120.1

输出:

复制代码

[root@localhost~]#route

KernelIProutingtable

DestinationGatewayGenmaskFlagsMetricRefUseIface

192.168.120.0*255.255.255.0U000eth0#p#分页标题#e#

192.168.0.0192.168.120.1255.255.0.0UG000eth0

10.0.0.0192.168.120.1255.0.0.0UG000eth0

default192.168.120.2400.0.0.0UG000eth0

[root@localhost~]#ping-b192.168.120.1

PING192.168.120.1(192.168.120.1)56(84)bytesofdata.

64bytesfrom192.168.120.1:icmp_seq=1ttl=255time=2.02ms

64bytesfrom192.168.120.1:icmp_seq=2ttl=255time=1.83ms

64bytesfrom192.168.120.1:icmp_seq=3ttl=255time=1.68ms

64bytesfrom192.168.120.1:icmp_seq=4ttl=255time=1.98ms

64bytesfrom192.168.120.1:icmp_seq=5ttl=255time=1.88ms

---192.168.120.1pingstatistics---

5packetstransmitted,5received,0%packetloss,time4000ms

rttmin/avg/max/mdev=1.682/1.880/2.020/0.129ms

复制代码

说明:

实例4:ping指定次数

#p#分页标题#e#命令:

ping-c10192.168.120.206

输出:

复制代码

[root@localhost~]#ping-c10192.168.120.206

PING192.168.120.206(192.168.120.206)56(84)bytesofdata.

64bytesfrom192.168.120.206:icmp_seq=1ttl=64time=1.25ms

64bytesfrom192.168.120.206:icmp_seq=2ttl=64time=0.260ms

64bytesfrom192.168.120.206:icmp_seq=3ttl=64time=0.242ms

64bytesfrom192.168.120.206:icmp_seq=4ttl=64time=0.271ms

64bytesfrom192.168.120.206:icmp_seq=5ttl=64time=0.274ms

64bytesfrom192.168.120.206:icmp_seq=6ttl=64time=0.295ms

64bytesfrom192.168.120.206:icmp_seq=7ttl=64time=0.269ms

64bytesfrom192.168.120.206:icmp_seq=8ttl=64time=0.270ms

64bytesfrom192.168.120.206:icmp_seq=9ttl=64time=0.253ms

64bytesfrom192.168.120.206:icmp_seq=10ttl=64time=0.289ms

---192.168.120.206pingstatistics---

10packetstransmitted,10received,0%packetloss,time9000ms

rttmin/avg/max/mdev=0.242/0.367/1.251/0.295ms

[root@localhost~]##p#分页标题#e#

复制代码

说明:

实例5:时间间隔和次数限制的ping

命令:

ping-c10-i0.5192.168.120.206

输出:

复制代码

[root@localhost~]#ping-c10-i0.5192.168.120.206

PING192.168.120.206(192.168.120.206)56(84)bytesofdata.

64bytesfrom192.168.120.206:icmp_seq=1ttl=64time=1.24ms

64bytesfrom192.168.120.206:icmp_seq=2ttl=64time=0.235ms

64bytesfrom192.168.120.206:icmp_seq=3ttl=64time=0.244ms

64bytesfrom192.168.120.206:icmp_seq=4ttl=64time=0.300ms#p#分页标题#e#

64bytesfrom192.168.120.206:icmp_seq=5ttl=64time=0.255ms

64bytesfrom192.168.120.206:icmp_seq=6ttl=64time=0.264ms

64bytesfrom192.168.120.206:icmp_seq=7ttl=64time=0.263ms

64bytesfrom192.168.120.206:icmp_seq=8ttl=64time=0.331ms

64bytesfrom192.168.120.206:icmp_seq=9ttl=64time=0.247ms

64bytesfrom192.168.120.206:icmp_seq=10ttl=64time=0.244ms

---192.168.120.206pingstatistics---

10packetstransmitted,10received,0%packetloss,time4499ms

rttmin/avg/max/mdev=0.235/0.362/1.241/0.294ms

[root@localhost~]#ping-c10-i0.01192.168.120.206

PING192.168.120.206(192.168.120.206)56(84)bytesofdata.

64bytesfrom192.168.120.206:icmp_seq=1ttl=64time=0.244ms

64bytesfrom192.168.120.206:icmp_seq=2ttl=64time=0.195ms

64bytesfrom192.168.120.206:icmp_seq=3ttl=64time=0.219ms

64bytesfrom192.168.120.206:icmp_seq=4ttl=64time=0.204ms

64bytesfrom192.168.120.206:icmp_seq=5ttl=64time=3.56ms

64bytesfrom192.168.120.206:icmp_seq=6ttl=64time=1.93ms

64bytesfrom192.168.120.206:icmp_seq=7ttl=64time=0.193ms

64bytesfrom192.168.120.206:icmp_seq=8ttl=64time=0.193ms

64bytesfrom192.168.120.206:icmp_seq=9ttl=64time=0.202ms

64bytesfrom192.168.120.206:icmp_seq=10ttl=64time=0.211ms

---192.168.120.206pingstatistics---

10packetstransmitted,10received,0%packetloss,time90ms

rttmin/avg/max/mdev=0.193/0.716/3.564/1.080ms

[root@localhost~]#

复制代码#p#分页标题#e#

说明:

实例6:通过域名ping公网上的站点

命令:

ping-c5www.58.com

输出:

复制代码

peida-VirtualBox~#ping-c5www.58.com

PINGwww.58.com(211.151.111.30)56(84)bytesofdata.

64bytesfrom211.151.111.30:icmp_req=1ttl=49time=14.7ms

64bytesfrom211.151.111.30:icmp_req=2ttl=49time=16.4ms

64bytesfrom211.151.111.30:icmp_req=3ttl=49time=15.2ms

64bytesfrom211.151.111.30:icmp_req=4ttl=49time=14.6ms

64bytesfrom211.151.111.30:icmp_req=5ttl=49time=19.9ms#p#分页标题#e#

---www.58.compingstatistics---

5packetstransmitted,5received,0%packetloss,time20101ms

rttmin/avg/max/mdev=14.618/16.192/19.917/1.965ms

peida-VirtualBox~#

复制代码

说明:

实例7:多参数使用

命令:

ping-i3-s1024-t255192.168.120.206

输出:

复制代码

[root@localhost~]##p#分页标题#e#ping-i3-s1024-t255192.168.120.206

PING192.168.120.206(192.168.120.206)1024(1052)bytesofdata.

1032bytesfrom192.168.120.206:icmp_seq=1ttl=64time=1.99ms

1032bytesfrom192.168.120.206:icmp_seq=2ttl=64time=0.694ms

1032bytesfrom192.168.120.206:icmp_seq=3ttl=64time=0.300ms

1032bytesfrom192.168.120.206:icmp_seq=4ttl=64time=0.481ms

1032bytesfrom192.168.120.206:icmp_seq=5ttl=64time=0.415ms

1032bytesfrom192.168.120.206:icmp_seq=6ttl=64time=0.600ms

1032bytesfrom192.168.120.206:icmp_seq=7ttl=64time=0.411ms

1032bytesfrom192.168.120.206:icmp_seq=8ttl=64time=0.281ms

1032bytesfrom192.168.120.206:icmp_seq=9ttl=64time=0.318ms

1032bytesfrom192.168.120.206:icmp_seq=10ttl=64time=0.362ms

1032bytesfrom192.168.120.206:icmp_seq=11ttl=64time=0.408ms

1032bytesfrom192.168.120.206:icmp_seq=12ttl=64time=0.445ms

1032bytesfrom192.168.120.206:icmp_seq=13ttl=64time=0.397ms

1032bytesfrom192.168.120.206:icmp_seq=14ttl=64time=0.406ms

1032bytesfrom192.168.120.206:icmp_seq=15ttl=64time=0.458ms

---192.168.120.206pingstatistics---

15packetstransmitted,15received,0%packetloss,time41999ms

rttmin/avg/max/mdev=0.281/0.531/1.993/0.404ms

[root@localhost~]#

复制代码

说明:

-i3发送周期为3秒-s设置发送包的大小#p#分页标题#e#为1024-t设置TTL值为255

转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0304/213.html?1457194120

相关推荐