Ping stands for Packet Internet Gopher. Ping basically is the simplest tool to verify network connectivity. We can verify connectivity between any two devices within a private or public network.

In simple words, a simple network can be made up of two or more devices connected together using any media and properly configured as well. For example if we connect two computers using Ethernet cable and configured the IP settings properly then we can call it a network. Here ping is the first thing to do to verify the connectivity between two machines within this small network. If network is perfect then both machines A & B will be able to send and receive ping packets between each other.

ICMP Protocol:

Ping uses ICMP protocol. ICMP stands for Internet Control Message Protocol. Ping basically sends ICMP echo request packet to the destination device and wait for ICMP echo reply from the destination. At the end of its output it shows a statistical summary which contains packets sent and received, packet loss and minimum, maximum and average round-trip time values. The time taken in between sending ICMP echo request and receiving ICMP echo reply is called round-trip time. Ping is not only used for troubleshooting to test connectivity but also it is used to check the response time which determines network latency. First thing ping does is to resolve the destination domain name into IP address which also test whether dns services are working properly or not. Below is the output of ping from windows machine which by default sends 4 packets and stop.

ping google.com
Pinging google.com [216.58.192.14] with 32 bytes of dat
Reply from 216.58.192.14: bytes=32 time=306ms TTL=49
Reply from 216.58.192.14: bytes=32 time=303ms TTL=49
Reply from 216.58.192.14: bytes=32 time=302ms TTL=49
Reply from 216.58.192.14: bytes=32 time=304ms TTL=49
Ping statistics for 216.58.192.14:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)
Approximate round trip times in milli-seconds:
    Minimum = 302ms, Maximum = 306ms, Average = 303ms

Here is the output of ping from Linux machine which by default continue pinging until ctrl+c is pressed to cancel.

ping google.com
PING google.com (216.58.220.14) 56(84) bytes of data.
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=1 ttl=52 time=26.0 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=2 ttl=52 time=25.9 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=3 ttl=52 time=25.8 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=4 ttl=52 time=25.8 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=5 ttl=52 time=25.8 ms
^C
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 25.823/25.891/26.049/0.195 ms

Common ping Examples:

Here are few examples of ping with different switches. In Linux we use -c to limit the number of packets to be sent and received. In below example after 5 packets ping will stop.

ping -c 5 google.com
PING google.com (216.58.220.14) 56(84) bytes of data.
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=1 ttl=52 time=25.9 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=2 ttl=52 time=26.4 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=3 ttl=52 time=25.8 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=4 ttl=52 time=25.8 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=5 ttl=52 time=25.8 ms
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 25.817/25.986/26.417/0.283 ms

To set the interval between ping packets, switch -i is used. In below example 4 ping packet will be sent after the interval of 2 seconds each.

ping -i 2 -c 4 google.com
PING google.com (216.58.220.14) 56(84) bytes of data.
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=1 ttl=52 time=29.4 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=2 ttl=52 time=29.6 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=3 ttl=52 time=29.3 ms
64 bytes from bom05s05-in-f14.1e100.net (216.58.220.14): icmp_seq=4 ttl=52 time=29.3 ms
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 6007ms
rtt min/avg/max/mdev = 29.302/29.420/29.644/0.251 ms

Option -n is used for only numeric output without doing any name resolution for finding host name like bom05s05-in-f14.1e100.net from our previous examples.

ping -nc4 google.com
PING google.com (172.217.26.174) 56(84) bytes of data.
64 bytes from 172.217.26.174: icmp_seq=1 ttl=47 time=24.7 ms
64 bytes from 172.217.26.174: icmp_seq=2 ttl=47 time=24.8 ms
64 bytes from 172.217.26.174: icmp_seq=3 ttl=47 time=24.6 ms
64 bytes from 172.217.26.174: icmp_seq=4 ttl=47 time=24.5 ms
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 24.536/24.673/24.820/0.105 ms

If it is only summary required then -q is used for quiet operation which only displays the first line and summary.

ping -qc 4 google.com
PING google.com (216.58.199.142) 56(84) bytes of data.
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 26.226/26.402/26.677/0.168 ms

For more help -h switch is used with ping.

ping -h
Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
            [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
            [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
            [-w deadline] [-W timeout] [hop1 ...] destination

 

Related concept:   How to list partition labels in Linux

fping:

Manual page of utility fping explains it beautifully in simple words as fping is a program like ping which uses the Internet Control Message Protocol (ICMP) echo request to determine if a target host is responding.  fping differs from ping in that you can specify any number of targets on the command line, or specify a file containing the lists of targets to ping.  Instead of sending to one target until it times out or replies, fping will send out a ping packet and move on to the next target in a round-robin fashion.  In the default mode, if a target replies, it is noted and removed from the list of targets to check; if a target does not respond within a certain time limit and/or retry limit it is designated as unreachable.

fping google.com yahoo.com facebook.com
google.com is alive
yahoo.com is alive
facebook.com is alive
#fping google.com yahoo.com facebook.com nogoogle.com
google.com is alive
yahoo.com is alive
facebook.com is alive
nogoogle.com is unreachable

We can make file and add IPs or hostnames in the file to feed fping command.

$cat ips.txt
google.com
facebook.com
yahoo.com
nogoogle.com

#fping -f ips.txt
google.com is alive
yahoo.com is alive
facebook.com is alive
nogoogle.com is unreachable

With fping we use switch -f for reading file containing destination hosts. This option can only be used with root user. For standard user follow the command below.

$fping < ips.txt
google.com is alive
yahoo.com is alive
facebook.com is alive
nogoogle.com is unreachable

We can also provide range of IP addresses like below.

$fping 192.168.10.{249,250,251,252}
192.168.10.249 is alive
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.250
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.250
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.250
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.250
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.251
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.251
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.251
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.251
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.252
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.252
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.252
ICMP Host Unreachable from 192.168.10.249 for ICMP Echo sent to 192.168.10.252
192.168.10.250 is unreachable
192.168.10.251 is unreachable
192.168.10.252 is unreachable

hping:

hping is another sister command of ping to test the network. In some distributions it is known as hping3 and not installed by default. hping3 is a network tool able to send custom TCP/IP packets and to display target replies like ping program does with ICMP replies.

Related concept:   FREE Docker online training from linuxnix.com

It also uses the same switches like ping for basic functionalities like -h for help and -c for count etc. Here are few basic examples for hping3.

hping3 localhost -c 3
HPING localhost (lo 127.0.0.1): NO FLAGS are set, 40 headers + 0 data bytes
len=40 ip=127.0.0.1 ttl=64 DF id=10586 sport=0 flags=RA seq=0 win=0 rtt=3.5 ms
len=40 ip=127.0.0.1 ttl=64 DF id=10657 sport=0 flags=RA seq=1 win=0 rtt=2.3 ms
len=40 ip=127.0.0.1 ttl=64 DF id=10668 sport=0 flags=RA seq=2 win=0 rtt=2.4 ms
--- localhost hping statistic ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 2.3/2.7/3.5 ms

For sending TCP packet to specific port like 443 -p switch is used. Below it will send 3 packets to port 443 of localhost.

hping3 localhost -c 3 -p 443
HPING localhost (lo 127.0.0.1): NO FLAGS are set, 40 headers + 0 data bytes
len=40 ip=127.0.0.1 ttl=64 DF id=10925 sport=443 flags=RA seq=0 win=0 rtt=1.2 ms
len=40 ip=127.0.0.1 ttl=64 DF id=11032 sport=443 flags=RA seq=1 win=0 rtt=1.2 ms
len=40 ip=127.0.0.1 ttl=64 DF id=11164 sport=443 flags=RA seq=2 win=0 rtt=1.2 ms
--- localhost hping statistic ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 1.2/1.2/1.2 ms

For more verbosity we can use -V option which shows more details to help analyzing network.

hping3 localhost -c 3 -p 443 -V
using lo, addr: 127.0.0.1, MTU: 65536
HPING localhost (lo 127.0.0.1): NO FLAGS are set, 40 headers + 0 data bytes
len=40 ip=127.0.0.1 ttl=64 DF id=15202 tos=0 iplen=40
sport=443 flags=RA seq=0 win=0 rtt=1.1 ms
seq=0 ack=2084985995 sum=3ce urp=0
len=40 ip=127.0.0.1 ttl=64 DF id=15246 tos=0 iplen=40
sport=443 flags=RA seq=1 win=0 rtt=1.3 ms
seq=0 ack=1926997853 sum=9b8c urp=0
len=40 ip=127.0.0.1 ttl=64 DF id=15314 tos=0 iplen=40
sport=443 flags=RA seq=2 win=0 rtt=4.6 ms
seq=0 ack=1033548582 sum=12b6 urp=0
--- localhost hping statistic ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 1.1/2.3/4.6 ms