-->
当前位置:首页 > 运维 > 正文内容

ping命令进阶

Luz1年前 (2022-12-29)运维2212

命令行说明

[root@VM-4-10-centos ~]# ping
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-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
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
             [-l preload] [-m mark] [-M pmtudisc_option]
             [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
             [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
             [-W timeout] destination

功能简述

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


Ping命令在排查网络故障的时候很常用。可以很快到判断出线路是否联通,网卡是否能正常工作等。ping 命令计算信号往返时间和(信息)包丢失情况的统计信息,并且在完成之后显示一个简要总结。ping 命令在程序超时或当接收到 SIGINT 信号时结束。Host 参数或者是一个有效的主机名或者是因特网地址。

参数说明

-d 使用Socket的SO_DEBUG功能。


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


-n 只输出数值。


-q 不显示任何传送封包的信息,只显示最后的结果。[这个以后能加入到脚本中,作为ping结果判断的依据吗?]


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


-R 记录路由过程。:一般可以来测试网络链路在哪个环节有故障。


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


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


-i 秒数:设定间隔几秒送一个网络封包给一台机器,预设值是一秒送一次。


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


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


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


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


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


使用举例

(1)指定次数的ping 某个网站/IP

[root@VM-4-10-centos ~]# ping -c 4  www.baidu.com

PING www.a.shifen.com (112.80.248.76) 56(84) bytes of data.

64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=1 ttl=53 time=9.16 ms

64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=2 ttl=53 time=9.11 ms

64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=3 ttl=53 time=9.13 ms

64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=4 ttl=53 time=9.15 ms


--- www.a.shifen.com ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3003ms

rtt min/avg/max/mdev = 9.116/9.140/9.161/0.017 ms


(2)指定时间间隔,及ping包大小的ping某个网络的情况


[root@VM-4-10-centos ~]# ping -i 3 -c 5 -s 1024 www.baidu.com

PING www.a.shifen.com (180.101.49.14) 1024(1052) bytes of data.

1032 bytes from 180.101.49.14 (180.101.49.14): icmp_seq=1 ttl=49 time=9.23 ms

1032 bytes from 180.101.49.14 (180.101.49.14): icmp_seq=2 ttl=49 time=9.20 ms

1032 bytes from 180.101.49.14 (180.101.49.14): icmp_seq=3 ttl=49 time=9.19 ms

1032 bytes from 180.101.49.14 (180.101.49.14): icmp_seq=4 ttl=49 time=9.22 ms

1032 bytes from 180.101.49.14 (180.101.49.14): icmp_seq=5 ttl=49 time=10.0 ms


--- www.a.shifen.com ping statistics ---

5 packets transmitted, 5 received, 0% packet loss, time 12009ms

rtt min/avg/max/mdev = 9.194/9.381/10.045/0.348 ms


(3)ping网络ping不通的情况

[root@VM-4-10-centos ~]# ping  192.168.1.222

PING 192.168.1.222 (192.168.1.222) 56(84) bytes of data.

^C

--- 192.168.1.222 ping statistics ---

165 packets transmitted, 0 received, 100% packet loss, time 164017ms


持续的ping

# while :;do ping -c 1 172.17.39.251|awk '/ttl=/'|sed "s/^/`date +%Y-%m-%d\|%T` /";sleep 1;done


显示效果如下


引用:


[root@PT_LINUX boot]# while :;do ping -c 1 172.17.39.251|awk '/ttl=/'|sed "s/^/`date +%Y-%m-%d\|%T` /";sleep 1;done


2005-09-20|15:24:40 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.240 ms


2005-09-20|15:24:41 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.235 ms


2005-09-20|15:24:42 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.220 ms


2005-09-20|15:24:43 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.224 ms


2005-09-20|15:24:45 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.211 ms


2005-09-20|15:24:46 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.211 ms


2005-09-20|15:24:47 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.222 ms


2005-09-20|15:24:48 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.221 ms


2005-09-20|15:24:49 64 bytes from 172.17.39.251: icmp_seq=0 ttl=128 time=0.222 ms


这样就加上了时间戳,就可以判断时间点了。

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。