Linux ping 详细介绍(win也适用)

21 篇文章 7 订阅

多数情况下,能否访问某台电脑或服务器,很自然的使用ping xxx.xxx.xxx.xxx,这是最简单的ping形式,同时看到ping几个包之后就停止(windows)了,linux 会一直ping下去,直到按ctrl+c或其他组合键。在windows平台或linux平台,ping是非常常用的网络命令;ping命令通过ICMP(Internet控制消息协议)工作;ping可以用来测试本机与目标主机是否联通、联通速度、稳定性。此联通是形容词,连接是通的。不是“联通公司”

一、ping用法:
  ping命令运行在命令提示符终端,用法为:“ping 参数 目标主机”。其中参数为零到多个,目标主机可以是IP或者域名。

ping [options] [ hop ...] destination_host

具体参数形式

ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]      
     [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
     [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
     [-w deadline] [-W timeout] [hop1 ...] destination

二、ping参数详解:
ping详细参数,如有错误,请帮忙指正:

参数详解
-aAudible ping.
-A自适应ping,根据ping包往返时间确定ping的速度;
-b允许ping一个广播地址;
-B不允许ping改变包头的源地址;
-c countping指定次数后停止ping;
-d使用Socket的SO_DEBUG功能;
-F flow_label为ping回显请求分配一个20位的“flow label”,如果未设置,内核会为ping随机分配;
-f极限检测,快速连续ping一台主机,ping的速度达到100次每秒;
-i interval设定间隔几秒发送一个ping包,默认一秒ping一次;
-I interface指定网卡接口、或指定的本机地址送出数据包;
-l preload设置在送出要求信息之前,先行发出的数据包;
-L抑制组播报文回送,只适用于ping的目标为一个组播地址
-n不要将ip地址转换成主机名;
-p pattern指定填充ping数据包的十六进制内容,在诊断与数据有关的网络错误时这个选项就非常有用,如:“-p ff”;
-q不显示任何传送封包的信息,只显示最后的结果
-Q tos设置Qos(Quality of Service),它是ICMP数据报相关位;可以是十进制或十六进制数,详见rfc1349和rfc2474文档;
-R记录ping的路由过程(IPv4 only); 注意:由于IP头的限制,最多只能记录9个路由,其他会被忽略;
-r忽略正常的路由表,直接将数据包送到远端主机上,通常是查看本机的网络接口是否有问题;如果主机不直接连接的网络上,则返回一个错误。
-S sndbufSet socket sndbuf. If not specified, it is selected to buffer not more than one packet.
-s packetsize指定每次ping发送的数据字节数,默认为“56字节”+“28字节”的ICMP头,一共是84字节; 包头+内容不能大于65535,所以最大值为65507(linux:65507, windows:65500);
-t ttl设置TTL(Time To Live)为指定的值。该字段指定IP包被路由器丢弃之前允许通过的最大网段数;
-T timestamp_option设置IP timestamp选项,可以是下面的任何一个:   ‘tsonly’ (only timestamps)   ‘tsandaddr’ (timestamps and addresses)   ‘tsprespec host1 [host2 [host3]]’ (timestamp prespecified hops).
-M hint设置MTU(最大传输单元)分片策略。 可设置为:   ‘do’:禁止分片,即使包被丢弃;   ‘want’:当包过大时分片;   ‘dont’:不设置分片标志(DF flag);
-m mark设置mark;
-v使ping处于verbose方式,它要ping命令除了打印ECHO-RESPONSE数据包之外,还打印其它所有返回的ICMP数据包;
-UPrint full user-to-user latency (the old behaviour). Normally ping prints network round trip time, which can be different f.e. due to DNS failures.
-W timeout以毫秒为单位设置ping的超时时间;
-w deadlinedeadline;

参考内容:http://ss64.com/bash/ping.html

三、输出格式:

gensee@localhost ~ % ping -c 2 www.baidu.com
PING www.a.shifen.com (112.80.248.76): 56 data bytes
64 bytes from 112.80.248.76: icmp_seq=0 ttl=56 time=13.495 ms
64 bytes from 112.80.248.76: icmp_seq=1 ttl=56 time=13.247 ms

— www.a.shifen.com ping statistics —
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 13.247/13.371/13.495/0.124 ms

下图为简单的ping产生的响应内容,图中返回内容具体的含义如下:
在这里插入图片描述

  • ① ping目标主机的域名和IP(ping会自动将域名转换为IP)
  • ② 不带包头的包大小和带包头的包大小(参考“-s”参数)
  • ③ icmp_seq:ping序列,从1开始; ttl:剩余的ttl;time: 响应时间,数值越小,联通速度越快;
  • ④ 发出去的包数,返回的包数,丢包率,耗费时间;
  • ⑤ 最小/最大/平均响应时间和本机硬件耗费时间;

四、简单例子:
1、每隔0.6秒ping一次,一共ping 5次:

gensee@localhost ~  ping -c 5 -i 0.6 qq.com
PING qq.com (14.17.32.211) 56(84) bytes of data.
64 bytes from 14.17.32.211: icmp_seq=1 ttl=54 time=16.5 ms
64 bytes from 14.17.32.211: icmp_seq=2 ttl=54 time=17.0 ms
64 bytes from 14.17.32.211: icmp_seq=3 ttl=54 time=16.6 ms
64 bytes from 14.17.32.211: icmp_seq=4 ttl=54 time=18.3 ms
64 bytes from 14.17.32.211: icmp_seq=5 ttl=54 time=19.9 ms
--- qq.com ping statistics ---5 packets transmitted, 5 received, 0% packet loss,
time 2405ms rtt min /avg/max/mdev = 16.520 /17.692/19.919/1.293 ms

2、完全无法ping通的情形:

gensee@localhost ~ % ping -c 3 9.9.9.9
PING 9.9.9.9 (9.9.9.9): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1

--- 9.9.9.9 ping statistics ---
3 packets transmitted, 0 packets received, 100.0% packet loss

3、每隔5秒ping一次

gensee@localhost ~ % ping -I 5 www.baidu.com
PING www.a.shifen.com (112.80.248.76): 56 data bytes
64 bytes from 112.80.248.76: icmp_seq=0 ttl=56 time=14.237 ms
64 bytes from 112.80.248.76: icmp_seq=1 ttl=56 time=14.071 ms
64 bytes from 112.80.248.76: icmp_seq=2 ttl=56 time=14.122 ms
64 bytes from 112.80.248.76: icmp_seq=3 ttl=56 time=13.450 ms
64 bytes from 112.80.248.76: icmp_seq=4 ttl=56 time=13.227 ms

五、常见错误:
1、错误一:

ping: packet size too large: 70000
WARNING: packet size 65508 is too large. Maximum is 65507``
PING 10.0.0.52 (10.0.0.52) 65508(65536) bytes of data.
ping: local error: Message too long, mtu=1500
ping: local error: Message too long, mtu=1500

解决:
  将ping的“-s”参数设置一个更小的值,windows最大值为“65500”,linux最大值为“65507”。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值