Advanced ping Options and Practical Applications
Now that you understand the basics of ping and how to interpret its output, let's explore some advanced options and practical scenarios where ping is useful for network diagnostics.
Let's first create a simple shell script that will help us monitor a host continuously and log the results:
nano ~/project/monitor_host.sh
Type the following script into the editor:
#!/bin/bash
## Simple network monitoring script
HOST=$1
INTERVAL=$2
LOG_FILE=~/project/ping_log.txt
echo "Starting monitoring of $HOST at $(date)" > $LOG_FILE
while true; do
ping -c 1 $HOST | grep "time=" >> $LOG_FILE
sleep $INTERVAL
echo "---" >> $LOG_FILE
done
Save the file by pressing Ctrl+O
, then Enter
, and exit with Ctrl+X
.
Make the script executable:
chmod +x ~/project/monitor_host.sh
Now, let's run the script for a few seconds to monitor Google's DNS server:
~/project/monitor_host.sh 8.8.8.8 2 &
sleep 10
kill $!
This will:
- Start the monitoring script in the background
- Wait for 10 seconds
- Stop the script
Let's examine the log:
cat ~/project/ping_log.txt
You should see several ping results with timestamps.
Now, let's explore some more advanced ping options:
- Setting the TTL (Time To Live):
ping -c 4 -t 64 8.8.8.8
The -t
option sets the TTL value, which defines how many network hops a packet can traverse before being discarded.
- Flood ping (requires sudo):
sudo ping -c 10 -f 8.8.8.8
The -f
option sends packets as fast as possible. This is useful for stress testing but should be used with caution.
- Audible ping:
ping -c 4 -a 8.8.8.8
The -a
option produces an audible bell sound when a host responds, which is useful when you're troubleshooting and not watching the screen.
- Timing with deadline:
ping -c 4 -w 2 8.8.8.8
The -w
option sets a deadline in seconds after which ping will stop, regardless of how many packets have been sent.
Let's explore a practical troubleshooting scenario. We'll create a simple visualization of the network path to a destination using traceroute (which works similar to ping but shows the route packets take):
traceroute 8.8.8.8
This command shows each hop (router) that packets traverse to reach the destination. You'll see something like:
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 _gateway (10.0.2.2) 0.113 ms 0.087 ms 0.083 ms
2 * * *
3 * * *
4 8.8.8.8 14.080 ms 13.849 ms 14.399 ms
The asterisks (*) represent timeouts or routers that don't respond to traceroute requests.
Now, let's create a simple table to document our ping results for different hosts:
echo -e "Host\tMin Time\tAvg Time\tMax Time\tPacket Loss" > ~/project/ping_results.txt
Let's add Google's DNS to our table:
result=$(ping -c 5 8.8.8.8 | tail -1)
min=$(echo $result | awk -F/ '{print $4}')
avg=$(echo $result | awk -F/ '{print $5}')
max=$(echo $result | awk -F/ '{print $6}')
loss=$(ping -c 5 8.8.8.8 | grep -o "[0-9]*%" | head -1)
echo -e "8.8.8.8\t$min ms\t\t$avg ms\t\t$max ms\t\t$loss" >> ~/project/ping_results.txt
And add Cloudflare's DNS:
result=$(ping -c 5 1.1.1.1 | tail -1)
min=$(echo $result | awk -F/ '{print $4}')
avg=$(echo $result | awk -F/ '{print $5}')
max=$(echo $result | awk -F/ '{print $6}')
loss=$(ping -c 5 1.1.1.1 | grep -o "[0-9]*%" | head -1)
echo -e "1.1.1.1\t$min ms\t\t$avg ms\t\t$max ms\t\t$loss" >> ~/project/ping_results.txt
Let's view our results table:
cat ~/project/ping_results.txt
You should see a formatted table with ping statistics for both DNS servers, which you can use to compare their performance.