How to use the command 'ping' (with examples)
- Osx
- December 25, 2023
Ping is a command-line utility that sends Internet Control Message Protocol (ICMP) Echo Request messages to network hosts. It is commonly used to check whether a specific host is reachable or to measure the round-trip time for packets sent from the local host to a remote host.
Use case 1: Ping the specified host
Code:
ping "hostname"
Motivation: This use case is useful when you want to check whether a specific host is reachable from your machine. By pinging the hostname, you can determine if there is a network connection between your machine and the remote host.
Explanation:
ping
: The command used to send ICMP ECHO_REQUEST packets to network hosts."hostname"
: The name or IP address of the host you want to ping.
Example output:
PING hostname (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.948 ms
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.001 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.058 ms
--- hostname ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.948/2.002/2.058/0.053 ms
Use case 2: Ping a host a specific number of times
Code:
ping -c count "host"
Motivation: This use case is helpful when you want to send a specific number of ping requests to a host. It allows you to determine the reachability and responsiveness of the host within a given count.
Explanation:
ping
: The command used to send ICMP ECHO_REQUEST packets to network hosts.-c count
: Specifies the number of packets to send to the host."host"
: The name or IP address of the host you want to ping.
Example output:
PING host (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.922 ms
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.988 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.989 ms
--- host ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.922/1.966/1.989/0.036 ms
Use case 3: Ping host, specifying the interval in seconds between requests
Code:
ping -i seconds "host"
Motivation: This use case is useful when you want to specify a custom interval between ping requests. It allows you to control the frequency of ping requests to a host.
Explanation:
ping
: The command used to send ICMP ECHO_REQUEST packets to network hosts.-i seconds
: Specifies the interval in seconds between sending each packet."host"
: The name or IP address of the host you want to ping.
Example output:
PING host (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.945 ms
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.001 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.060 ms
--- host ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.945/2.002/2.060/0.049 ms
Use case 4: Ping host without trying to lookup symbolic names for addresses
Code:
ping -n "host"
Motivation: This use case is helpful when you want to disable the hostname resolution feature of the ping command. By using this option, you can directly ping the specified host without any delays caused by resolving symbolic names.
Explanation:
ping
: The command used to send ICMP ECHO_REQUEST packets to network hosts.-n
: Disables the lookup of symbolic names for addresses."host"
: The name or IP address of the host you want to ping.
Example output:
PING host (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.936 ms
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.999 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.062 ms
--- host ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.936/1.999/2.062/0.055 ms
Use case 5: Ping host and ring the bell when a packet is received
Code:
ping -a "host"
Motivation: This use case is beneficial when you want to be notified audibly whenever a ping packet is received by the host. It can be helpful in situations where you need to actively monitor the responsiveness of a host.
Explanation:
ping
: The command used to send ICMP ECHO_REQUEST packets to network hosts.-a
: Rings the bell (audible beep) when a packet is received."host"
: The name or IP address of the host you want to ping.
Example output:
PING host (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.947 ms
^G64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.005 ms
^G64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.057 ms
--- host ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.947/2.003/2.057/0.049 ms
Use case 6: Ping host and prints the time a packet was received (Apple addition)
Code:
ping --apple-time "host"
Motivation: This use case is specific to Apple’s implementation of the ping command. It allows you to view the time when each ping packet is received by the host. This can be useful for analyzing the responsiveness of a host over time.
Explanation:
ping
: The command used to send ICMP ECHO_REQUEST packets to network hosts.--apple-time
: Prints the time a packet was received."host"
: The name or IP address of the host you want to ping.
Example output:
PING host (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.956 ms (Tue Jun 15 10:22:30 2021)
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.010 ms (Tue Jun 15 10:22:31 2021)
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.050 ms (Tue Jun 15 10:22:32 2021)
--- host ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.956/2.005/2.050/0.044 ms
Conclusion:
The ping
command is a versatile tool for network diagnostics and troubleshooting. With its various options, you can easily check the reachability, responsiveness, and round-trip time of network hosts. Whether you need to ping a host once, multiple times, or with custom settings, the ping
command provides the necessary functionality to meet your requirements.