Bash ping
- Send Request to Network Hosts
Using the ping
Command
The ping
command is used to send ICMP ECHO_REQUEST to network hosts.
It's a useful tool for checking network connectivity and diagnosing network issues.
Basic Usage
To ping a host, use ping hostname
:
Example
ping google.com
Pinging google.com [142.250.74.110] with 32 bytes of data:
Reply from 142.250.74.110: bytes=32 time=79ms TTL=57
Reply from 142.250.74.110: bytes=32 time=52ms TTL=57
Reply from 142.250.74.110: bytes=32 time=48ms TTL=57
Reply from 142.250.74.110: bytes=32 time=38ms TTL=57
Ping statistics for 142.250.74.110:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 38ms, Maximum = 79ms, Average = 54ms
Options
The ping
command has options to change how it works:
-c
- Send a specific number of ping requests-i
- Wait a specific number of seconds between sending each packet-t
- Set the IP Time to Live (TTL)-q
- Quiet output, only show summary-s
- Specify the number of data bytes to be sent
Send a Specific Number of Ping Requests
The -c
option allows you to send a specific number of ping requests.
This is useful for limiting the number of packets sent.
Example: Send a Specific Number of Ping Requests
ping -c 4 google.com
PING google.com (172.217.14.206): 56 data bytes
64 bytes from 172.217.14.206: icmp_seq=0 ttl=118 time=14.5 ms
64 bytes from 172.217.14.206: icmp_seq=1 ttl=118 time=14.2 ms
64 bytes from 172.217.14.206: icmp_seq=2 ttl=118 time=14.3 ms
64 bytes from 172.217.14.206: icmp_seq=3 ttl=118 time=14.4 ms
--- google.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 14.2/14.3/14.5/0.1 ms
Understanding Ping Results
The output of the ping
command provides several key pieces of information:
- Bytes: The size of the ICMP packet sent
- Time: The round-trip time it took for the packet to reach the host and return, measured in milliseconds
- TTL (Time to Live): The remaining lifespan of the packet, which decreases by one for each hop
- Packet Loss: The percentage of packets that were lost during transmission
- Round-Trip Time Statistics: Includes minimum, average, maximum, and standard deviation of the round-trip times
In the example above:
- Each packet is 64 bytes
- The round-trip times vary slightly, indicating network latency
- The TTL value is 118, showing the packet's remaining lifespan
- There is 0.0% packet loss, indicating a stable connection
- The round-trip time statistics provide insights into network performance
Wait a Specific Number of Seconds Between Sending Each Packet
The -i
option allows you to wait a specific number of seconds between sending each packet.
This can be useful for reducing network traffic when using ping
.
Example: Wait a Specific Number of Seconds Between Sending Each Packet
ping -i 2 google.com
PING google.com (172.217.14.206): 56 data bytes
64 bytes from 172.217.14.206: icmp_seq=0 ttl=118 time=14.5 ms
64 bytes from 172.217.14.206: icmp_seq=1 ttl=118 time=14.2 ms
--- google.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 14.2/14.3/14.5/0.1 ms
Set the IP Time to Live (TTL)
The -t
option allows you to set the IP Time to Live (TTL).
This determines the maximum number of hops a packet can take before being discarded.
Example: Set the IP Time to Live (TTL)
ping -t 64 google.com
PING google.com (172.217.14.206): 56 data bytes
64 bytes from 172.217.14.206: icmp_seq=0 ttl=64 time=14.5 ms
--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 14.5/14.5/14.5/0.0 ms
Quiet Output
The -q
option enables quiet output, only showing the summary of the ping results.
This is useful for scripts or when you only need the final statistics.
Example: Quiet Output
ping -c 4 -q google.com
--- google.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 14.2/14.3/14.5/0.1 ms
Specify Data Bytes
The -s
option allows you to specify the number of data bytes to be sent.
This can be useful for testing network performance with different packet sizes.
Example: Specify Data Bytes
ping -s 128 google.com
PING google.com (172.217.14.206): 128 data bytes
136 bytes from 172.217.14.206: icmp_seq=0 ttl=118 time=14.5 ms
--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 14.5/14.5/14.5/0.0 ms