在本期中,本站将为您带来关于如何在Linux中使用ping命令的信息。文章内容丰富,从专业角度分析叙述。看完这篇文章,希望你能有所收获。
1.命令格式:
Ping[参数][主机名或IP地址]
2.命令功能:
ping命令用于确定网络和外部主机的状态;跟踪和隔离硬件和软件问题;测试、评估和管理网络。如果主机正在运行并连接到网络,它将响应回送信号。每个回应请求都包含一个互联网协议(IP)和ICMP报头,后跟一个tim结构,以及足够填充该数据包的字节。默认情况下,环回信号请求持续发送,直到收到中断信号(Ctrl-C)。
ping命令每秒发送一个数据报,并为收到的每个响应打印一行输出。ping命令计算信号往返时间和数据包丢失的统计数据,并在完成后显示一个简短的摘要。当程序超时或收到SIGINT信号时,ping命令结束。主机参数是有效的主机名或互联网地址。
3.命令参数:
-d使用Socket的SO_DEBUG函数。
-f极限检测。向机器快速发送大量网络数据包,查看其响应。
-n仅输出数值。
-q不显示任何发送数据包的消息,只显示最终结果。
-r忽略普通RoutingTable,直接将数据包发送到远程主机。通常是检查这台机器的网络接口是否有问题。
-R记录路由过程。
-v详细显示了指令的执行过程。
P-c号:发送指定数量的数据包后停止。
-i秒:设置几秒钟的时间间隔向机器发送网络数据包,默认值为每秒一次。
-I网络接口:使用指定的网络接口发送数据包。
-l预加载:设置发送请求信息前发送的数据包。
-p模板样式:设置填充包的模板样式。
-s字节:指定发送的数据字节数,默认值为56,加上8字节的ICMP报头,总共是64个ICMP数据字节。
-t生存值:设置生存值TTL的大小。
4.使用实例:
实例1:ping的通的情况
命令:ping192.168.120.205
输出:
代码如下:
[root @ localhost ~]# ping 192 . 168 . 120 . 205
PING 192 . 168 . 120 . 205(192 . 168 . 120 . 205)56(84)字节的数据。
64字节,从192 . 168 . 120 . 205: icmp _ seq=1 TTL=64时间=0.720 ms
64字节,从192 . 168 . 120 . 205: icmp _ seq=2 TTL=64时间=0.181 ms
64字节,从192 . 168 . 120 . 205: icmp _ seq=3 TTL=64时间=0.191 ms
64字节,从192 . 168 . 120 . 205: icmp _ seq=4 TTL=64时间=0.188 ms
64字节,从192 . 168 . 120 . 205: icmp _ seq=5 TTL=64时间=0.189 ms/p
p - 192.168.120.205 ping统计-
发送5个数据包,接收5个数据包,0%数据包丢失,时间4000毫秒
rtt最小值/平均值/最大值/mdev=0.181/0.293/0.720/0.214 ms
[root@localhost ~]#
实例2:ping不通的情况
命令:ping192.168.120.202
输出:
代码如下:
[root @ localhost ~]# ping 192 . 168 . 120 . 202
PING 192 . 168 . 120 . 202(192 . 168 . 120 . 202)56(84)字节的数据。
从192.168.120.204 icmp_seq=1目标主机不可达
从192.168.120.204 icmp_seq=2目标主机不可达
从192.168.120.204 icmp_seq=3目标主机不可达
从192.168.120.204 icmp_seq=4目标主机不可达
从192.168.120.204 icmp_seq=5目的主机U
nreachableFrom 192.168.120.204 icmp_seq=6 Destination Host Unreachable</p> <p>--- 192.168.120.202 ping statistics ---8 packets transmitted, 0 received, +6 errors, 100% packet loss, time 7005ms, pipe 4[root@localhost ~]#
实例3:ping网关
命令:ping-b192.168.120.1
输出:
代码如下:
[root@localhost ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0[root@localhost ~]# ping -b 192.168.120.1PING 192.168.120.1 (192.168.120.1) 56(84) bytes of data.64 bytes from 192.168.120.1: icmp_seq=1 ttl=255 time=2.02 ms64 bytes from 192.168.120.1: icmp_seq=2 ttl=255 time=1.83 ms64 bytes from 192.168.120.1: icmp_seq=3 ttl=255 time=1.68 ms64 bytes from 192.168.120.1: icmp_seq=4 ttl=255 time=1.98 ms64 bytes from 192.168.120.1: icmp_seq=5 ttl=255 time=1.88 ms</p> <p>--- 192.168.120.1 ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 4000msrtt min/avg/max/mdev = 1.682/1.880/2.020/0.129 ms
实例4:ping指定次数
命令:ping-c10192.168.120.206
输出:
代码如下:
[root@localhost ~]# ping -c 10 192.168.120.206PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.25 ms64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.260 ms64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.242 ms64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.271 ms64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.274 ms64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.295 ms64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.269 ms64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.270 ms64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.253 ms64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.289 ms</p> <p>--- 192.168.120.206 ping statistics ---10 packets transmitted, 10 received, 0% packet loss, time 9000msrtt min/avg/max/mdev = 0.242/0.367/1.251/0.295 ms[root@localhost ~]#
实例5:时间间隔和次数限制的ping
命令:ping-c10-i0.5192.168.120.206
输出:
代码如下:
[root@localhost ~]# ping -c 10 -i 0.5 192.168.120.206PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.24 ms64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.235 ms64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.244 ms64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.300 ms64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.255 ms64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.264 ms64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.263 ms64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.331 ms64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.247 ms64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.244 ms</p> <p>--- 192.168.120.206 ping statistics ---10 packets transmitted, 10 received, 0% packet loss, time 4499msrtt min/avg/max/mdev = 0.235/0.362/1.241/0.294 ms[root@localhost ~]# ping -c 10 -i 0.01 192.168.120.206PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.195 ms64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.219 ms64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.204 ms64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=3.56 ms64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=1.93 ms64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.193 ms64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.193 ms64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.202 ms64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.211 ms</p> <p>--- 192.168.120.206 ping statistics ---10 packets transmitted, 10 received, 0% packet loss, time 90msrtt min/avg/max/mdev = 0.193/0.716/3.564/1.080 ms[root@localhost ~]#
实例6:通过域名ping公网上的站点
命令:ping-c5www.58.com
输出:
代码如下:
peida-VirtualBox ~ # ping -c 5 www.58.comPING www.58.com (211.151.111.30) 56(84) bytes of data.64 bytes from 211.151.111.30: icmp_req=1 ttl=49 time=14.7 ms64 bytes from 211.151.111.30: icmp_req=2 ttl=49 time=16.4 ms64 bytes from 211.151.111.30: icmp_req=3 ttl=49 time=15.2 ms64 bytes from 211.151.111.30: icmp_req=4 ttl=49 time=14.6 ms64 bytes from 211.151.111.30: icmp_req=5 ttl=49 time=19.9 ms</p> <p>--- www.58.com ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 20101msrtt min/avg/max/mdev = 14.618/16.192/19.917/1.965 mspeida-VirtualBox ~ #
实例7:多参数使用
命令:ping-i3-s1024-t255192.168.120.206
输出:
代码如下:
[root@localhost ~]# ping -i 3 -s 1024 -t 255 192.168.120.206PING 192.168.120.206 (192.168.120.206) 1024(1052) bytes of data.1032 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.99 ms1032 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.694 ms1032 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.300 ms1032 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.481 ms1032 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.415 ms1032 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.600 ms1032 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.411 ms1032 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.281 ms1032 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.318 ms1032 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.362 ms1032 bytes from 192.168.120.206: icmp_seq=11 ttl=64 time=0.408 ms1032 bytes from 192.168.120.206: icmp_seq=12 ttl=64 time=0.445 ms1032 bytes from 192.168.120.206: icmp_seq=13 ttl=64 time=0.397 ms1032 bytes from 192.168.120.206: icmp_seq=14 ttl=64 time=0.406 ms1032 bytes from 192.168.120.206: icmp_seq=15 ttl=64 time=0.458 ms</p> <p>--- 192.168.120.206 ping statistics ---15 packets transmitted, 15 received, 0% packet loss, time 41999msrtt min/avg/max/mdev = 0.281/0.531/1.993/0.404 ms[root@localhost ~]#
说明:-i3发送周期为3秒-s设置发送包的大小为1024-t设置TTL值为255
上述就是小编为大家分享的如何在Linux中使用ping命令了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。