Linux命令(34):ping

  Linux系统的 ping 命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”、不能打开网页时会说“你先ping网关地址192.168.1.1试试”。

  它通过发送 ICMP ECHO-REQUEST 数据包到网络主机(send ICMP ECHO-REQUEST to network hosts),并显示响应情况,这样我们就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的)。有些服务器为了防止通过ping 探测到,通过防火墙设置了禁止 ping 或者在内核参数中禁止 ping,这样就不能通过 ping 确定该主机是否还处于开启状态。

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

命令格式:

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

命令功能:

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

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

命令参数:

-d 使用Socket的SO_DEBUG功能。

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

-n 只输出数值。

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

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

-R 记录路由过程。

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

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

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

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

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

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

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

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

例1. ping的通的情况

命令:ping 172.16.10.62

    [mt555@localhost Desktop]$ ping 172.16.10.62
    PING 172.16.10.62 (172.16.10.62) 56(84) bytes of data.
    64 bytes from 172.16.10.62: icmp_seq=1 ttl=64 time=0.027 ms
    64 bytes from 172.16.10.62: icmp_seq=2 ttl=64 time=0.063 ms
    64 bytes from 172.16.10.62: icmp_seq=3 ttl=64 time=0.060 ms
    64 bytes from 172.16.10.62: icmp_seq=4 ttl=64 time=0.031 ms
    64 bytes from 172.16.10.62: icmp_seq=7 ttl=64 time=0.032 ms
    64 bytes from 172.16.10.62: icmp_seq=8 ttl=64 time=0.030 ms
    64 bytes from 172.16.10.62: icmp_seq=9 ttl=64 time=0.027 ms
    ^C
    --- 172.16.10.62 ping statistics ---
    9 packets transmitted, 9 received, 0% packet loss, time 8891ms
    rtt min/avg/max/mdev = 0.027/0.036/0.063/0.015 ms
    [mt555@localhost Desktop]$

例2. ping不通的情况

命令:ping 172.16.10.58

    [mt555@localhost Desktop]$ ping 172.16.10.58
    PING 172.16.10.58 (172.16.10.58) 56(84) bytes of data.
    From 172.16.10.62 icmp_seq=2 Destination Host Unreachable
    From 172.16.10.62 icmp_seq=3 Destination Host Unreachable
    From 172.16.10.62 icmp_seq=4 Destination Host Unreachable
    ^C
    --- 172.16.10.58 ping statistics ---
    6 packets transmitted, 0 received, +3 errors, 100% packet loss, time 5847ms
    pipe 3
    [mt555@localhost Desktop]$ 

例3. ping网关

命令:ping -b 172.16.10.254

 [mt555@localhost Desktop]$ route
    Kernel IP routing table
    Destination   Gateway       Genmask         Flags   Metric   RefUse Iface
    172.16.10.0         *       255.255.255.0   U       0         00    eth0
    link-local          *       255.255.0.0     U       1002      00    eth0
    default      172.16.10.254    0.0.0.0       UG      0         00    eth0
    [mt555@localhost Desktop]$ ping -b 172.16.10.254
    PING 172.16.10.254 (172.16.10.254) 56(84) bytes of data.
    64 bytes from 172.16.10.254: icmp_seq=1 ttl=255 time=0.560 ms
    64 bytes from 172.16.10.254: icmp_seq=2 ttl=255 time=0.541 ms
    ^C
    --- 172.16.10.254 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1882ms
    rtt min/avg/max/mdev = 0.541/0.550/0.560/0.025 ms
    [mt555@localhost Desktop]$ 

例4. ping指定次数

命令:ping -c 2 172.16.10.68

    [mt555@localhost Desktop]$ ping -c 2 172.16.10.68
    PING 172.16.10.68 (172.16.10.68) 56(84) bytes of data.
    64 bytes from 172.16.10.68: icmp_seq=1 ttl=64 time=1.18 ms
    64 bytes from 172.16.10.68: icmp_seq=2 ttl=64 time=0.230 ms

    --- 172.16.10.68 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1001ms
    rtt min/avg/max/mdev = 0.230/0.708/1.187/0.479 ms
    [mt555@localhost Desktop]$

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

命令:ping -c 2 -i 0.5 172.16.10.68

    [mt555@localhost Desktop]$ ping -c 2 -i 0.5 172.16.10.68
    PING 172.16.10.68 (172.16.10.68) 56(84) bytes of data.
    64 bytes from 172.16.10.68: icmp_seq=1 ttl=64 time=0.168 ms
    64 bytes from 172.16.10.68: icmp_seq=2 ttl=64 time=0.203 ms

    --- 172.16.10.68 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 500ms
    rtt min/avg/max/mdev = 0.168/0.185/0.203/0.022 ms
    [mt555@localhost Desktop]$ 

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

命令:ping -c 5 www.baidu.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值