计算机网络学习笔记(2)——Mac ping命令


PING (Packet Internet Groper), 因特网包探索器,用于测试网络连接量的程序 [1] 。Ping是工作在TCP/IP网络体系结构中 应用层的一个服务命令, 主要是向特定的目的主机发送 ICMP(Internet Control Message Protocol 因特网报文控制协议) Echo 请求报文,测试目的站是否可达及了解其有关状态 。

mac的ping命令语法
ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize] [-h sweepincrsize] 
    [-i wait] [-l preload] [-M mask | time] [-m ttl] [-P policy] [-p pattern] 
    [-S src_addr] [-s packetsize] [-t timeout] [-W waittime] [-z tos] host 
ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload] [-M mask | time] [-m ttl] 
    [-P policy] [-p pattern] [-S src_addr] [-s packetsize] [-T ttl] 
    [-t timeout] [-W waittime] [-z tos] mcast-group

参数说明:

OptionDescription
-AAudible. Output a bell (ASCII 0x07) character when no packet is received before the next packet is transmitted. To cater for round-trip times that are longer than the interval between transmissions, further missing packets cause a bell only if the maximum number of unreceived packets has increased.
-aAudible. Include a bell (ASCII 0x07) character in the output when any packet is received. This option is ignored if other format options are present.
-c countStop after sending (and receiving) count ECHO_RESPONSE packets. If this option is not specified, ping will operate until interrupted. If this option is specified in conjunction with ping sweeps, each sweep will consist of count packets.
-DSet the Don't Fragment bit.
-dSet the SO_DEBUG option on the socket being used.
-fFlood ping. Outputs packets as fast as they come back or one hundred times per second, whichever is more. For every ECHO_REQUEST sent a period ‘.’ is printed, while for every ECHO_REPLY received a backspace is printed. This provides a rapid display of how many packets are being dropped. Only the super-user may use this option. This can be very hard on a network and should be used with caution.
-G sweepmaxsizeSpecify the maximum size of ICMP payload when sending sweeping pings. This option is required for ping sweeps.
-g sweepminsizeSpecify the size of ICMP payload to start with when sending sweeping pings. The default value is 0.
-h sweepincrsizeSpecify the number of bytes to increment the size of ICMP payload after each sweep when sending sweeping pings. The default value is 1.
-I ifaceSource multicast packets with the given interface address. This flag only applies if the ping destination is a multicast address.
-i waitWait wait seconds between sending each packet. The default is to wait for one second between each packet. The wait time may be fractional, but only the super-user may specify values less than 1 second. This option is incompatible with the -f option.
-LSuppress loopback of multicast packets. This flag only applies if the ping destination is a multicast address.
-l preloadIf preload is specified, ping sends that many packets as fast as possible before falling into its normal mode of behavior. Only the super-user may use this option.
-M mask | timeUse ICMP_MASKREQ or ICMP_TSTAMP instead of ICMP_ECHO. For mask, print the netmask of the remote machine. Set the net.inet.icmp.maskrepl MIB variable to enable ICMP_MASKREPLY. For time, print the origination, reception and transmission time-stamps.
-m ttlSet the IP Time To Live for outgoing packets. If not specified, the kernel uses the value of the net.inet.ip.ttl MIB variable.
-nNumeric output only. No attempt will be made to lookup symbolic names for host addresses.
-oExit successfully after receiving one reply packet.
-P policypolicy specifies IPsec policy for the ping session.
-p patternYou may specify up to 16 ‘pad’ bytes to fill out the packet you send. This is useful for diagnosing data-dependent problems in a network. For example, ‘-p ff’ will cause the sent packet to be filled with all ones.
-QSomewhat quiet output. Don’t display ICMP error messages that are in response to our query messages Originally, the -v flag was required to display such errors, but -v displays all ICMP error messages. On a busy machine, this output can be overbearing. Without the -Q flag, ping prints out any ICMP error messages caused by its own ECHO_REQUEST messages.
-qQuiet output. Nothing is displayed except the summary lines at startup time and when finished.
-RRecord route. Includes the RECORD_ROUTE option in the ECHO_REQUEST packet and displays the route buffer on returned packets. Note that the IP header is only large enough for nine such routes; the traceroute(8) command is usually better at determining the route packets take to a particular destination. If more routes come back than should, such as due to an illegal spoofed packet, ping will print the route list and then truncate it at the correct spot. Many hosts ignore or discard the RECORD_ROUTE option.
-rBypass the normal routing tables and send directly to a host on an attached network. If the host is not on a directly-attached network, an error is returned. This option can be used to ping a local host through an interface that has no route through it (e.g., after the interface was dropped by routed(8)).
-S src_addrUse the following IP address as the source address in outgoing packets. On hosts with more than one IP address, this option can be used to force the source address to be something other than the IP address of the interface the probe packet is sent on. If the IP address is not one of this machine’s interface addresses, an error is returned and nothing is sent.
-s packetsizeSpecify the number of data bytes to be sent. The default is 56, which translates into 64 ICMP data bytes when combined with the 8 bytes of ICMP header data. Only the super-user may specify values more than default. This option cannot be used with ping sweeps.
-T ttlSet the IP Time To Live for multicasted packets. This flag only applies if the ping destination is a multicast address.
-t timeoutSpecify a timeout, in seconds, before ping exits regardless of how many packets have been received.
-vVerbose output. ICMP packets other than ECHO_RESPONSE that are received are listed.
-W waittimeTime in milliseconds to wait for a reply for each packet sent. If a reply arrives later, the packet is not printed as replied, but considered as replied when calculating statistics.
-z tosUse the specified type of service.

使用ping进行故障隔离时,应首先在本地主机上运行它,以验证本地网络接口是否已启动并正在运行。然后,应该ping越来越远的主机和网关。计算往返时间和丢包统计信息。如果接收到重复的数据包,尽管这些数据包的往返时间用于计算往返时间统计信息,但它们不会包含在丢包计算中。发送(和接收)指定数量的数据包后,会显示一个简短摘要,其中显示了已发送和接收的数据包数量,以及往返时间的最小值,平均值,最大值和标准偏差。

该程序旨在用于网络测试,测量和管理。由于它可能会施加在网络上,因此在正常操作或自动脚本中使用ping是不明智的。

实例

要测试到某计算机如 百度Web 服务器的连通性,可以使用 ping www.baidu.com 命令,也可直接使用 IP 地址。

>ping www.baidu.com                                                         64 ↵
PING www.a.shifen.com (14.215.177.39): 56 data bytes
64 bytes from 14.215.177.39: icmp_seq=0 ttl=54 time=30.345 ms
64 bytes from 14.215.177.39: icmp_seq=1 ttl=54 time=42.513 ms
64 bytes from 14.215.177.39: icmp_seq=2 ttl=54 time=31.481 ms
64 bytes from 14.215.177.39: icmp_seq=3 ttl=54 time=36.710 ms
64 bytes from 14.215.177.39: icmp_seq=4 ttl=54 time=31.891 ms
64 bytes from 14.215.177.39: icmp_seq=5 ttl=54 time=48.125 ms
64 bytes from 14.215.177.39: icmp_seq=6 ttl=54 time=31.085 ms
64 bytes from 14.215.177.39: icmp_seq=7 ttl=54 time=30.300 ms
^C
--- www.a.shifen.com ping statistics ---
38 packets transmitted, 38 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 30.096/35.533/70.009/6.930 ms

//需要手动终止Ctrl+C

指定接收包的次数

>ping -c 5 www.baidu.com 
PING www.a.shifen.com (14.215.177.39): 56 data bytes
64 bytes from 14.215.177.39: icmp_seq=0 ttl=54 time=34.659 ms
64 bytes from 14.215.177.39: icmp_seq=1 ttl=54 time=42.064 ms
64 bytes from 14.215.177.39: icmp_seq=2 ttl=54 time=37.253 ms
64 bytes from 14.215.177.39: icmp_seq=3 ttl=54 time=31.259 ms
64 bytes from 14.215.177.39: icmp_seq=4 ttl=54 time=33.665 ms

--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 31.259/35.780/42.064/3.683 ms

多参数使用

>ping -i 3 -s 1024 -t 255 www.baidu.com                                     64 ↵
PING www.a.shifen.com (14.215.177.39): 1024 data bytes
1032 bytes from 14.215.177.39: icmp_seq=0 ttl=54 time=36.187 ms
1032 bytes from 14.215.177.39: icmp_seq=1 ttl=54 time=30.574 ms
1032 bytes from 14.215.177.39: icmp_seq=2 ttl=54 time=37.126 ms
1032 bytes from 14.215.177.39: icmp_seq=3 ttl=54 time=30.388 ms
1032 bytes from 14.215.177.39: icmp_seq=4 ttl=54 time=37.841 ms
1032 bytes from 14.215.177.39: icmp_seq=5 ttl=54 time=37.441 ms
1032 bytes from 14.215.177.39: icmp_seq=6 ttl=54 time=37.702 ms
1032 bytes from 14.215.177.39: icmp_seq=7 ttl=54 time=37.362 ms
^C
--- www.a.shifen.com ping statistics ---
8 packets transmitted, 8 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 30.388/35.578/37.841/2.980 ms
TTL详细信息

IP数据包的TTL值表示该数据包在丢弃之前可以通过的IP路由器的最大数量。在当前的实践中,您可以期Internet中的每个路由器将TTL字段减一。
TCP / IP规范建议将IP数据包的TTL字段设置为64,但是许多系统使用较小的值(4.3BSD使用30,4.2BSD使用15)。
此字段的最大可能值为255,并且大多数UNIX系统将ICMP ECHO_REQUEST数据包的TTL字段设置为255。这就是为什么您会发现可以ping某些主机,但不能通过telnet到达它们的原因。

在正常操作中,ping从收到的数据包中打印ttl值。远程系统收到ping数据包时,可以在响应中使用TTL字段执行以下三项操作之一:

  1. 不改变它;这就是BSD系统在4.3BSD-Tahoe发行之前所做的事情。在这种情况下,接收到的数据包中的TTL值将为255减去往返路径中路由器的数量。
  2. 设置为255;这就是当前的BSD系统所做的。在这种情况下,接收到的数据包中的TTL值将为255减去从远程系统到ping主机的路径中的路由器数量。
  3. 将其设置为其他值。一些机器对ICMP数据包使用的值与用于TCP数据包的值相同,例如30或60。其他机器可能完全使用通配值。
小秘笈

当你的网络出现故障不能访问某计算机如 14.215.177.39 (百度的 IP 地址之一 ) 时,我们一般可采用由近及远的连通性测试来确定问题所在。现假设你的 IP 是 192.168.1.89,你旁边计算机的 IP 是 192.168.1.64,网关的 IP 是 192.168.1.1 ,那么过程如下:

  • ping 127.0.0.1 ,测试自己计算机的状态,如果 OK,那么说明本机网络软件硬件工作正常,否则,问题在本机,检查本机 TCP/IP 配置即网卡状态等
  • ping 192.168.1.64 ,测试到旁边计算机的连通性,如果OK,那么说明本子网内部工作正常,否则,问题在本机网络出口到交换机之间,检查本机网卡到交换机的连线等
  • ping 192.168.1.1,测试到网关的连通性,如果 OK,那么说明本子网出口工作正常,否则,问题在网关,这是你无能为力的事情,报告给网管
  • ping 14.215.177.39,测试到百度的连通性,如果 OK,那就 OK,否则,问题在网关以外,这也是你无能为力的事情,报告给网管或者李彦宏?
Bugs
  • 许多主机和网关会忽略RECORD_ROUTE选项。
  • IP标头的最大长度太小,无法使RECORD_ROUTE之类的选项完全有用。但是,对此没有太多的事情可以做。
    通常不建议进行频繁ping操作,并且仅应在非常受控的条件下对广播地址进行泛洪ping操作。
  • -v选项在繁忙的主机上最好不常用。
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!感谢提问。下面是有关 K210 学习笔记五中串口通信的一些内容: 在 K210 开发板上,串口是一种常见的通信接口,用于将数据从一个设备传输到另一个设备。串口通信通常使用 UART (Universal Asynchronous Receiver/Transmitter) 协议。 要在 K210 上进行串口通信,首先需要使用两个引脚:一个用于接收数据(RX),一个用于发送数据(TX)。这些引脚可以通过 GPIO 端口与外部设备连接。 在 K210 的开发环境中,可以使用 MaixPy 或者 Kendryte Standalone SDK 进行串口编程。 在 MaixPy 中,可以使用 `uart` 模块来进行串口通信。例如,要初始化一个串口对象并设置波特率为 115200,可以使用以下代码: ```python from machine import UART uart = UART(UART.UART1, 115200) ``` 然后,可以使用 `uart.write()` 函数发送数据,使用 `uart.read()` 函数接收数据。例如: ```python uart.write("Hello, world!\n") data = uart.read(10) ``` 在 Kendryte Standalone SDK 中,可以使用 `uart.h` 头文件中的函数来进行串口通信。例如,要初始化一个串口对象并设置波特率为 115200,可以使用以下代码: ```c #include "uart.h" uart_init(UART_DEVICE_1, 115200); ``` 然后,可以使用 `uart_send_data()` 函数发送数据,使用 `uart_receive_data()` 函数接收数据。例如: ```c uart_send_data(UART_DEVICE_1, "Hello, world!\n", 14); char buffer[10]; uart_receive_data(UART_DEVICE_1, buffer, 10); ``` 以上是关于 K210 学习笔记五中串口通信的简要介绍。如果你有更具体的问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值