- Ping works by sending ICMP echo packet request to the target destination and listening or ICMP echo reply packets.
- Breaking down ping,
- Ping constructs an ICMP echo request packet (has the destination IP and other details)
- The packet is sent to the destination IP
- The target machine upon receiving the request, sends back an echo response packet (with source and destination IP swapped)
- When the originating machine receives a response, it calculates the total-round-trip-time, this tells us about the communication latency with that particular target machine
- More such packets are sent to come up with network statistics like average round-trip-trip, packet loss percentage, etc.
Using Ping
# Explicity mention IP
ping 192.168.1.1
# Ping using hostname
ping www.google.com
# hostname resolution is done by cache lookup followed by DNS lookup
Ping can leverage the same DNS resolution libraries that HTTP clients use to find IPs of target systems.
ping https://github.com
# will raise an error
Ping works on the network layer and thus cannot resolve HTTP addresses (they work on the application layer).
Routers have an 'ICMP block ping' option that lets us block a response to a ping for security reasons.