请选择 进入手机版 | 继续访问电脑版

牛大大的个人博客

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1612|回复: 0

Linux系统中如何使用ping命令进行网络诊断

[复制链接]

149

主题

201

帖子

643

积分

版主

Rank: 7Rank: 7Rank: 7

积分
643
发表于 2020-4-28 18:49:13 | 显示全部楼层 |阅读模式
linux下的ping和windows下的ping稍有区别,linux下ping不会自动终止,需要按ctrl+c终止或者用参数-c指定要求完成的回应次数。
  1.命令格式:
  ping [参数] [主机名或IP地址]
  2.命令功能:

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

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

  3.命令参数:

  1. -d 使用Socket的SO_DEBUG功能。

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

  3.   -n 只输出数值。

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

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

  6.   -R 记录路由过程。

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

  8.   《p》-c 数目:在发送指定数目的包后停止。

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

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

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

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

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

  14.   -t 存活数值:设置存活数值TTL的大小。
复制代码

  4.使用实例:
  实例1:ping的通的情况
  命令:ping 192.168.120.205
  输出:
  代码如下:

  1. [root@localhost ~]# ping 192.168.120.205

  2.   PING 192.168.120.205 (192.168.120.205) 56(84) bytes of data.

  3.   64 bytes from 192.168.120.205: icmp_seq=1 ttl=64 time=0.720 ms

  4.   64 bytes from 192.168.120.205: icmp_seq=2 ttl=64 time=0.181 ms

  5.   64 bytes from 192.168.120.205: icmp_seq=3 ttl=64 time=0.191 ms

  6.   64 bytes from 192.168.120.205: icmp_seq=4 ttl=64 time=0.188 ms

  7.   64 bytes from 192.168.120.205: icmp_seq=5 ttl=64 time=0.189 ms《/p》 《p》--- 192.168.120.205 ping statistics ---

  8.   5 packets transmitted, 5 received, 0% packet loss, time 4000ms

  9.   rtt min/avg/max/mdev = 0.181/0.293/0.720/0.214 ms

  10.   [root@localhost ~]#
复制代码


实例2:ping不通的情况
  命令:ping 192.168.120.202
  输出:
  代码如下:

  1. [root@localhost ~]# ping 192.168.120.202

  2.   PING 192.168.120.202 (192.168.120.202) 56(84) bytes of data.

  3.   From 192.168.120.204 icmp_seq=1 Destination Host Unreachable

  4.   From 192.168.120.204 icmp_seq=2 Destination Host Unreachable

  5.   From 192.168.120.204 icmp_seq=3 Destination Host Unreachable

  6.   From 192.168.120.204 icmp_seq=4 Destination Host Unreachable

  7.   From 192.168.120.204 icmp_seq=5 Destination Host Unreachable

  8.   From 192.168.120.204 icmp_seq=6 Destination Host Unreachable《/p》 《p》--- 192.168.120.202 ping statistics ---

  9.   8 packets transmitted, 0 received, +6 errors, 100% packet loss, time 7005ms

  10.   , pipe 4

  11.   [root@localhost ~]#
复制代码

  实例3:ping网关
  命令:ping -b 192.168.120.1
  输出:
  代码如下:

  1. [root@localhost ~]# route

  2.   Kernel IP routing table

  3.   Destination Gateway Genmask Flags Metric Ref Use Iface

  4.   192.168.120.0 * 255.255.255.0 U 0 0 0 eth0

  5.   192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0

  6.   10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0

  7.   default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0

  8.   [root@localhost ~]# ping -b 192.168.120.1

  9.   PING 192.168.120.1 (192.168.120.1) 56(84) bytes of data.

  10.   64 bytes from 192.168.120.1: icmp_seq=1 ttl=255 time=2.02 ms

  11.   64 bytes from 192.168.120.1: icmp_seq=2 ttl=255 time=1.83 ms

  12.   64 bytes from 192.168.120.1: icmp_seq=3 ttl=255 time=1.68 ms

  13.   64 bytes from 192.168.120.1: icmp_seq=4 ttl=255 time=1.98 ms

  14.   64 bytes from 192.168.120.1: icmp_seq=5 ttl=255 time=1.88 ms《/p》 《p》--- 192.168.120.1 ping statistics ---

  15.   5 packets transmitted, 5 received, 0% packet loss, time 4000ms

  16.   rtt min/avg/max/mdev = 1.682/1.880/2.020/0.129 ms
复制代码

  实例4:ping指定次数
  命令:ping -c 10 192.168.120.206
  输出:
  代码如下:

  1. [root@localhost ~]# ping -c 10 192.168.120.206

  2.   PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.

  3.   64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.25 ms

  4.   64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.260 ms

  5.   64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.242 ms

  6.   64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.271 ms

  7.   64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.274 ms

  8.   64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.295 ms

  9.   64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.269 ms

  10.   64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.270 ms

  11.   64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.253 ms

  12.   64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.289 ms《/p》 《p》--- 192.168.120.206 ping statistics ---

  13.   10 packets transmitted, 10 received, 0% packet loss, time 9000ms

  14.   rtt min/avg/max/mdev = 0.242/0.367/1.251/0.295 ms

  15.   [root@localhost ~]#
复制代码

实例5:时间间隔和次数限制的ping
  命令:ping -c 10 -i 0.5 192.168.120.206
  输出:
  代码如下:

  1. [root@localhost ~]# ping -c 10 -i 0.5 192.168.120.206

  2.   PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.

  3.   64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.24 ms

  4.   64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.235 ms

  5.   64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.244 ms

  6.   64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.300 ms

  7.   64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.255 ms

  8.   64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.264 ms

  9.   64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.263 ms

  10.   64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.331 ms

  11.   64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.247 ms

  12.   64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.244 ms《/p》 《p》--- 192.168.120.206 ping statistics ---

  13.   10 packets transmitted, 10 received, 0% packet loss, time 4499ms

  14.   rtt min/avg/max/mdev = 0.235/0.362/1.241/0.294 ms

  15.   [root@localhost ~]# ping -c 10 -i 0.01 192.168.120.206

  16.   PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.

  17.   64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=0.244 ms

  18.   64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.195 ms

  19.   64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.219 ms

  20.   64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.204 ms

  21.   64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=3.56 ms

  22.   64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=1.93 ms

  23.   64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.193 ms

  24.   64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.193 ms

  25.   64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.202 ms

  26.   64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.211 ms《/p》 《p》--- 192.168.120.206 ping statistics ---

  27.   10 packets transmitted, 10 received, 0% packet loss, time 90ms

  28.   rtt min/avg/max/mdev = 0.193/0.716/3.564/1.080 ms

  29.   [root@localhost ~]#
复制代码

  实例6:通过域名ping公网上的站点
  命令:ping -c 5 www.58.com
  输出:
  代码如下:

  1. peida-VirtualBox ~ # ping -c 5 www.58.com

  2.   PING www.58.com (211.151.111.30) 56(84) bytes of data.

  3.   64 bytes from 211.151.111.30: icmp_req=1 ttl=49 time=14.7 ms

  4.   64 bytes from 211.151.111.30: icmp_req=2 ttl=49 time=16.4 ms

  5.   64 bytes from 211.151.111.30: icmp_req=3 ttl=49 time=15.2 ms

  6.   64 bytes from 211.151.111.30: icmp_req=4 ttl=49 time=14.6 ms

  7.   64 bytes from 211.151.111.30: icmp_req=5 ttl=49 time=19.9 ms《/p》 《p》--- www.58.com ping statistics ---

  8.   5 packets transmitted, 5 received, 0% packet loss, time 20101ms

  9.   rtt min/avg/max/mdev = 14.618/16.192/19.917/1.965 ms

  10.   peida-VirtualBox ~ #
复制代码

  实例7:多参数使用
  命令:ping -i 3 -s 1024 -t 255 192.168.120.206
  输出:
  代码如下:

  1. [root@localhost ~]# ping -i 3 -s 1024 -t 255 192.168.120.206

  2.   PING 192.168.120.206 (192.168.120.206) 1024(1052) bytes of data.

  3.   1032 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.99 ms

  4.   1032 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.694 ms

  5.   1032 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.300 ms

  6.   1032 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.481 ms

  7.   1032 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.415 ms

  8.   1032 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.600 ms

  9.   1032 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.411 ms

  10.   1032 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.281 ms

  11.   1032 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.318 ms

  12.   1032 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.362 ms

  13.   1032 bytes from 192.168.120.206: icmp_seq=11 ttl=64 time=0.408 ms

  14.   1032 bytes from 192.168.120.206: icmp_seq=12 ttl=64 time=0.445 ms

  15.   1032 bytes from 192.168.120.206: icmp_seq=13 ttl=64 time=0.397 ms

  16.   1032 bytes from 192.168.120.206: icmp_seq=14 ttl=64 time=0.406 ms

  17.   1032 bytes from 192.168.120.206: icmp_seq=15 ttl=64 time=0.458 ms《/p》 《p》--- 192.168.120.206 ping statistics ---

  18.   15 packets transmitted, 15 received, 0% packet loss, time 41999ms

  19.   rtt min/avg/max/mdev = 0.281/0.531/1.993/0.404 ms

  20.   [root@localhost ~]#
复制代码

  说明:-i 3 发送周期为 3秒 -s 设置发送包的大小为1024 -t 设置TTL值为 255
  上面就是Linux下ping命令的具体用法的介绍了,如果你的网络出了故障,又想知道是哪里出了问题,可以使用ping命令进行检测。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|牛大大的个人博客 ( 苏ICP备17043571号-2 )

GMT+8, 2021-6-15 12:50 , Processed in 0.192767 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表