How to use the command 'ping' (with examples)
The ping
command is used to send ICMP ECHO_REQUEST packets to network hosts. It is commonly used to check the connectivity and response time of a remote host. By sending ICMP ECHO_REQUEST packets, it allows us to see if a host is reachable and measure the round-trip time for the packets to reach the host and come back.
Use case 1: Ping host
Code:
ping host
Motivation: The motivation for using this example is to check if a specific host is reachable on the network. It can be useful when troubleshooting network connectivity issues or monitoring the status of a remote host.
Explanation: In this use case, we simply use the ping
command followed by the hostname or IP address of the host we want to ping. The command sends ICMP ECHO_REQUEST packets to the specified host and waits for a response.
Example output:
PING host (192.168.0.1) 56(84) bytes of data.
64 bytes from host (192.168.0.1): icmp_seq=1 ttl=64 time=1.20 ms
64 bytes from host (192.168.0.1): icmp_seq=2 ttl=64 time=0.96 ms
64 bytes from host (192.168.0.1): icmp_seq=3 ttl=64 time=1.02 ms
...
Use case 2: Ping a host only a specific number of times
Code:
ping -c count host
Motivation: The motivation for using this example is to limit the number of pings sent to a host. It can be useful when you only need to check the reachability of a host for a specific number of times.
Explanation: In this use case, we use the -c
option followed by a number to specify the count of pings to be sent to the host. After sending the specified number of pings, the command will stop and display the statistics.
Example output:
PING host (192.168.0.1) 56(84) bytes of data.
64 bytes from host (192.168.0.1): icmp_seq=1 ttl=64 time=1.20 ms
64 bytes from host (192.168.0.1): icmp_seq=2 ttl=64 time=0.96 ms
64 bytes from host (192.168.0.1): icmp_seq=3 ttl=64 time=1.02 ms
...
--- host ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 0.960/1.052/1.203/0.112 ms
Use case 3: Ping host, specifying the interval in seconds between requests
Code:
ping -i seconds host
Motivation: The motivation for using this example is to specify the interval in seconds between each ping request. It can be useful when you want to customize the timing between each request.
Explanation: In this use case, we use the -i
option followed by the number of seconds to specify the interval between each ping request. The default interval is 1 second.
Example output:
PING host (192.168.0.1) 56(84) bytes of data.
64 bytes from host (192.168.0.1): icmp_seq=1 ttl=64 time=1.20 ms
64 bytes from host (192.168.0.1): icmp_seq=2 ttl=64 time=0.96 ms
64 bytes from host (192.168.0.1): icmp_seq=3 ttl=64 time=1.02 ms
...
Use case 4: Ping host without trying to lookup symbolic names for addresses
Code:
ping -n host
Motivation: The motivation for using this example is to disable the hostname lookup feature when pinging a host. It can be useful when you want to speed up the ping process and only focus on checking the reachability of a host.
Explanation: In this use case, we use the -n
option to disable the symbolic name lookup. By doing so, the ping
command will not try to resolve the IP address to a hostname.
Example output:
PING host (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=1.20 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=0.96 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=1.02 ms
...
Use case 5: Ping host and ring the bell when a packet is received
Code:
ping -a host
Motivation: The motivation for using this example is to receive an audible notification when a ping response is received. It can be useful when you want to be alerted immediately upon receiving a response.
Explanation: In this use case, we use the -a
option to ring the terminal bell when a ICMP ECHO_REPLY packet is received. However, this feature depends on the terminal support.
Example output:
PING host (192.168.0.1) 56(84) bytes of data.
64 bytes from host (192.168.0.1): icmp_seq=1 ttl=64 time=1.20 ms
Use case 6: Also display a message if no response was received
Code:
ping -O host
Motivation: The motivation for using this example is to display a message if no response is received from the host. It can be useful when you want to be notified if a host is not responding.
Explanation: In this use case, we use the -O
option to display a message if no response is received from the host. The message “no answer from host” will be displayed in case the host is not responding.
Example output:
PING host (192.168.0.1) 56(84) bytes of data.
64 bytes from host (192.168.0.1): icmp_seq=1 ttl=64 time=1.20 ms
no answer from host