1. Introduction

Ping is a utility available on most operating systems. Commonly, it’s used to check if a network device is reachable.

The name derives from the operating mode, which involves sending (echo request) and receiving (echo reply) of information packets, in a way vaguely similar to sonar. Some authors consider the name to be an acronym for Packet InterNet Groper.

As we’ll see in this tutorial, ping can be used to measure the bandwidth of a network connection.

There are slight implementation differences depending on the operating system. In the examples below, we will use the ping command available on Linux systems.

2. Anatomy of the Ping Command: the ICMP Protocol

2.1. General Features

The ping utility uses the echo request, and echo reply messages within the Internet Control Message Protocol (ICMP), an integral part of any IP network. When a ping command is issued, an echo-request packet is sent to the address specified. When the remote host receives the echo request, it responds with an echo reply packet.

The ping command sends several echo requests. The result of each echo request is displayed, showing whether the request received a successful response, how many bytes were received in response, the Time to Live (TTL), and how long the response took to receive, along with statistics about packet loss and round trip times.

The ping command allows us to:

  • Test our internet connection.
  • Check if a remote machine is online.
  • Analyze if there are network issues, such as dropped packages or high latency.

2.2. The ICMP Protocol

The ICMP protocol is located in the Network Layer of the Open Systems Interconnection (OSI) model, as shown in the following figure:

ICMP in OSI model

The structure of each ICMP packet, with an MTU of 1500 bytes, is as follows:

ICMP packet

For different MTUs, the size of the ICMP payload changes, which can therefore be generally variable.

3. Checking Reachability of the Host

Ping is normally used to check if a remote device is reachable. For example, sending 10 echo requests to the host hosting Baeldung:

> ping -c 10 www.baeldung.com
PING www.baeldung.com (172.67.72.45) 56 (84) bytes of data.
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 1 ttl = 59 time = 13.5 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 2 ttl = 59 time = 14.2 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 3 ttl = 59 time = 13.5 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 4 ttl = 59 time = 15.1 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 5 ttl = 59 time = 14.2 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 6 ttl = 59 time = 13.8 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 7 ttl = 59 time = 14.2 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 8 ttl = 59 time = 14.5 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 9 ttl = 59 time = 19.4 ms
64 bytes from 172.67.72.45 (172.67.72.45): icmp_seq = 10 ttl = 59 time = 16.3 ms

--- www.baeldung.com ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9011ms
rtt min / avg / max / mdev = 13.460 / 14.869 / 19.415 / 1.708 ms

The first line of the output specifies after the remote IP the size of the sent/received packet or MTU, 56 (84), which means 56 bytes of payload and 84 bytes total, considering the 28 bytes of the header.

As we can see from the final statistics, responses to all 10 packets were received, with an average send/reply time or RTT (Round Trip Time) of 14.869 milliseconds. The loss of one or more packets is a symptom of some connection problem.

The size of the payload sent can be specified with the -s argument. The default is 56 bytes.

3.3. Remote Host Distance: Latency Time

If we want to transfer a large file over our modem it might take several seconds, minutes, or even hours. The less data we send, the less time it takes, but there’s a limit. No matter how small the amount of data, for any particular network device there’s always a minimum time that we can never beat. That’s called the latency of the device.

The smaller the size of the packets to be sent or received over a connection, the more important the latency time becomes, as a percentage, which in some cases can exceed the transmission time.

The average RTT of 14,869 ms we discussed in our previous example is nothing more than the average latency time of the connection. Half of this value is the minimum time required to send or receive an information packet, regardless of its size.

With the knowledge of a few readily available data and the average latency time, we can calculate how far away the server hosting our magazine is:

  1. The speed of light in the vacuum is 3E5 km/s.
  2. The speed of light in fiber is roughly 66% of the speed of light in vacuum (3E5 \times 0.66 = 1.98E5 km / s). The speed of the signal in copper is about the same.
  3. The mean time delay for echo request plus echo reply is 14.869 ms. The one-way delay is 7.4345 ms.

The host distance is, therefore:

    \[D = 7.4345 \, \mathrm {ms} \times \frac {1 \, \mathrm {s}} {1000 \, \mathrm {ms}} \times1.98 \mathrm { E} 05 \mathrm {\frac {km} {s}} = 1472 \, \mathrm {km}\]

That means that servers are connected with at most 1472 km of fiber. The actual distance may differ from the results of a GeoIP test if ping results are cached by the ISP, or there is “anycast” routing (non-unique IP addresses).

4. Upload / Download Speed ​​by Ping

From all the elements that we have discussed, and in a similar way to what we have done in the calculation of the distance of the hosts, we are able to calculate the bandwidth of the connection.

Let’s repeat our example by specifying the payload size with the -s option:

> ping -c 10 -s 1472 www.baeldung.com
PING www.baeldung.com (104.26.12.74) 1472 (1500) bytes of data.
From 93.188.101.9 (93.188.101.9) icmp_seq = 1 Frag needed and DF set (mtu = 1492)
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 2 ttl = 59 time = 17.5 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 3 ttl = 59 time = 15.4 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 4 ttl = 59 time = 14.5 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 5 ttl = 59 time = 19.6 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 6 ttl = 59 time = 19.4 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 7 ttl = 59 time = 15.2 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 8 ttl = 59 time = 16.7 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 9 ttl = 59 time = 14.5 ms
1480 bytes from 104.26.12.74 (104.26.12.74): icmp_seq = 10 ttl = 59 time = 13.7 ms

--- www.baeldung.com ping statistics ---
10 packets transmitted, 9 received, +1 errors, 10% packet loss, time 9011ms
rtt min / avg / max / mdev = 13.710 / 16.288 / 19.640 / 2.027 ms

The payload size is 1472 bytes, with a final MTU of 1500 bytes (1472 + 28 header). Hence, the total amount sent / received is 1500 \times 2 = 24 kb.

With this value and the average RTT we can calculate the bandwidth:

    \[24 \, \mathrm {kb} \times \frac {1} {16.288 \, \mathrm {ms}} \times \frac {1000 \, \mathrm {ms}} {\mathrm {s}} \times \frac {\mathrm {Mb}} {1000 \, \mathrm {kb}} = 1.473 \, \frac {\mathrm {Mb}} {\mathrm {s}}\]

5. Conclusion

In this tutorial, we have seen how it is possible to obtain a large amount of information from the ping command. Bandwidth is one of them.

Other more advanced possibilities require a thorough understanding of the ICMP protocol.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.