ping 命令是您可以使用的最有用的网络诊断(network diagnostic)工具之一。它对于在本地网络和更广泛的 Internet 上查找问题很有用。让我们看看 ping 命令的作用以及如何使用它。
ping 某物是什么意思?(Mean)
在网络上“ping”某物意味着您将互联网数据包发送到目标计算机或其他网络设备,请求响应。然后,该设备会向您发送一个数据包。
当数据包返回时(如果它返回的话),您可以了解关于您自己和目的地之间的网络的各种有趣的事情。
最常见的是,我们只是想知道回复需要多长时间。因此,当有人提到(例如)网站的“ping”时,通常以毫秒为单位表示,通常数字越小越好。
什么是互联网数据包?
为了更好地理解 ping,您需要对作为“ping”发送到目标计算机或网络设备的数据包有所了解。
数据包是现代互联网(modern internet)的基本单位。当您向某人发送数据(例如照片)时,它会被分解成小块。每个数据包都标有源地址和目标地址,然后发送到互联网。这些数据包通过许多其他计算机,例如 Web 服务器和 Internet 路由器。数据包不断传递,直到它们到达目标系统。
您可以使用 Ping 做什么
ping 命令有两个主要用途:
- 检查您与远程计算机的连接是否正常工作。
- 检查该连接的健康程度。
即使您的 ping 到达目的地并收到回复,ping 响应也会告诉您返回数据包需要多长时间以及丢失了多少数据包。您可以使用 ping 命令来诊断连接是否太慢或不可靠。
您可以 ping 哪些类型的东西?
- 理论上,您可以用IP 地址(IP address)ping 任何东西。
- 您可以 ping 局域网上的设备以确保它们连接正确。
- 您还可以 ping 网站以查看是否可以访问它们。
如何在 Windows 上使用 Ping
使用 ping 很容易。您从命令提示符(Command Prompt)或PowerShell运行它,但我们在此示例中使用命令提示符:(Command Prompt)
- 打开开始菜单(Start Menu),搜索命令提示符(Command Prompt),然后选择它。
- 键入Ping,然后输入设备的IP 地址(IP address)或您要 ping的站点的 URL 。(URL of a site)
我们使用Google.com作为下面的示例。
一旦启动,ping 将发送四个数据包。
显示“请求超时”的任何数据包的消息意味着您的计算机没有收到来自目标的回复。如果某些数据包丢失,则您的计算机和目标之间的某些路由路径存在问题。
适用于Windows的有用Ping 命令修饰符(Ping Command Modifiers)
有很多命令开关可以与 ping 命令一起使用。开关是自定义 ping 命令功能的额外选项。如果您想查看完整的选项列表,您只需在命令提示符下键入ping /help 并按Enter即可。您将看到整个列表以及语法和用法。
以下(Below)是对大多数用户有用的一些命令开关的列表:
- /t:持续 ping 一个目标,不管你想要多长时间。按Ctrl + Break暂停该过程并查看当前统计信息。要退出,请按Ctrl + C.
- /a:解析 IP 地址的主机名。如果您有一个 IP 地址并且想知道与您正在 ping 的特定服务器关联的 Web 地址,这将非常有用。
- /n X:将“X”替换为您要发送的 ping 数。默认值为四个。但是,如果您想更好地分析丢失数据包的数量,请发送更多 ping 以更准确地了解平均丢失了多少数据包。
- /w X:将“X”替换为在声明超时之前要等待的毫秒数。默认情况下,此值为 4000 毫秒。如果您认为连接可能正常,但 ping 等待回复的时间不够长,您可以增加 X 以查看您是否正确。
- /l X:将“X”替换为以字节为单位的值以增加每个 ping 的大小。默认情况下,此值为 32,但您可以将其增加到 65527。这对于查看是否是导致网络问题的数据包大小很有用。32 字节的 ping 可以使用默认设置,但更大的值会导致数据包丢失。
如何在 macOS 上使用 Ping
对于Mac用户,您必须使用Terminal。macOS 的早期版本包括Network Utility,这是一个图形实用程序,您可以使用它来运行 ping 等网络命令。
但是,最新版本已弃用该实用程序以支持Terminal。它使用起来并不直观,但它仍然相当简单。首先(First),通过打开Spotlight(Command +空格键(Spacebar))并输入终端来打开(Terminal)终端(terminal)。
要启动 ping,只需键入 Windows 中所示的命令:ping后跟网站的 IP 地址或URL,即ping www.google.com。
您会注意到在Windows和Mac上使用 ping 的主要区别在于,在 macOS 上它会一直运行,直到您手动停止它。要停止 ping 设备或URL,请按Ctrl + C。这将使您回到主要提示。
要查看可用于 macOS 上 ping 的所有参数的列表,您可以键入man ping并获取帮助页面。有很多开关,所以花点时间玩不同的选项。
要退出手册页,您只需按键盘上的q键即可。这将退出手册页并带您回到提示符。
ping 命令非常通用,通常是找出问题出在网络上的最快方法。我们强烈建议您查看最佳 Windows 命令行网络命令(The Best Windows Command Line Network Commands)以了解基本的网络诊断命令。
How to Use the Ping Command to Troubleshoot Network Issues
The ping command is one of the most helpful network diagnostic tools at your disposal. It’s useful for finding problems both on your local network and on the wider internet. Let’s look at what the ping command does and how to use it.
What Does It Mean to Ping Something?
To “ping” something on a network means that you send an internet packet to a destination computer or other network device, asking for a response. That device then sends a packet back to you.
When the packet comes back (if it makes it back, that is) you can learn all sorts of interesting things about the network between yourself and the destination.
Most commonly, we simply want to know how long the reply takes. So when someone mentions the “ping” of (for example) a website, it’s usually expressed in milliseconds, with a lower number being better in general.
What Is an Internet Packet?
To better understand ping, you need to know a little about the packet you’re sending to the target computer or network device as a “ping.”
Packets are the fundamental units of the modern internet. When you send someone data, like a photo, it gets broken up into tiny pieces. Each packet is marked with a source and destination address and then sent off into the internet. These packets pass through many other computers, such as web servers and internet routers. Packets keep getting passed along until they reach the target system.
What You Can Use Ping For
The ping command has two primary uses:
- To check whether your connection to a remote computer works at all.
- To check how healthy that connection is.
Even if your ping reaches its destination and you get a reply, the ping response will tell you how long a packet took to return and how many packets have been lost. You can use the ping command to diagnose if the connection is too slow or unreliable.
What sorts of things can you ping?
- In theory, you can ping anything with an IP address.
- You can ping devices on your LAN to make sure they’re connected properly.
- You can also ping websites to see if you can reach them.
How to Use Ping on Windows
Using ping is easy. You run it from the Command Prompt or PowerShell, but we’re using the Command Prompt in this example:
- Open the Start Menu, search for the Command Prompt, and select it.
- Type Ping and then enter either the IP address of the device or the URL of a site you’d like to ping.
We’ve used Google.com as an example below.
Once initiated, ping will send four packets of data.
A message for any packets that says “request timed out” implies that your computer did not receive a reply from the target. If some packets are lost, some of the routing paths between your computer and the target have an issue.
Useful Ping Command Modifiers for Windows
There are quite a few command switches you can use with the ping command. Switches are extra options to customize how the ping command functions. If you want to see a complete list of options, all you have to do is type ping /help at the Command Prompt and press Enter. You’ll see the whole list as well as the syntax and usage.
Below is a list of a few command switches that are of use to most users:
- /t: Continuously ping a target for however long you want. Press Ctrl + Break to pause the process and look at the current statistics. To quit, press Ctrl + C.
- /a: Resolve the hostname of an IP address. This is useful if you have an IP address and would like to know the web address associated with the specific server you’re pinging.
- /n X: Replace “X” with the number of pings you want to send. The default is four. But if you want to better analyze the number of lost packets, send more pings to get a more accurate idea of how many packets are getting lost on average.
- /w X: Replace “X” with the number of milliseconds you want to wait before declaring a timeout. By default, this value is 4000ms. If you think that a connection may be working but that ping isn’t waiting long enough for a reply, you can increase X to see if you’re right.
- /l X: Replace “X” with a value in bytes to increase the size of each ping. By default this value is 32, but you can increase it to 65527. This is useful to see if it’s packet sizes that are causing issues on your network. A 32-byte ping would work with the default setting, but something larger would result in lost packets.
How to Use Ping on macOS
For Mac users, you have to user Terminal. Previous versions of macOS included Network Utility, which was a graphical utility that you could use to run network commands like ping.
However, the latest versions have deprecated that utility in favor of Terminal. It’s not as intuitive to use, but it’s still fairly straight-forward. First, open Terminal by opening Spotlight (Command + Spacebar) and typing in terminal.
To start a ping, just type the command as was shown in Windows: ping followed by the IP address or URL of the website, i.e. ping www.google.com.
The main difference you’ll notice between using ping on Windows and Mac is that on macOS it just keeps going until you manually stop it. To stop pinging the device or URL, press Ctrl + C. That will bring you back to the main prompt.
To see a list of all of the parameters you can use for ping on macOS, you can type man ping and get the help pages. There are a lot of switches, so take your time playing around with the different options.
To get out of the man page, you have to simply press the q key on your keyboard. This will quit the man page and take you back to the prompt.
The ping command is very versatile and is often the fastest way to figure out where the problem is on a network. We strongly recommend that you check out The Best Windows Command Line Network Commands for essential network diagnostic commands.