Skip to content

Instantly share code, notes, and snippets.

@ganeshrvel
Created June 5, 2024 13:00
Show Gist options
  • Save ganeshrvel/49b330fa3d50dfa6a5f2b05f31cd9e25 to your computer and use it in GitHub Desktop.
Save ganeshrvel/49b330fa3d50dfa6a5f2b05f31cd9e25 to your computer and use it in GitHub Desktop.
Find the optimal network MTU
#!/bin/bash
# Destination to ping
destination="1.1.1.1"
max_mtu=1500
min_mtu=1400
best_mtu=$min_mtu
best_time=999999
# Function to ping with a specific MTU and return the average ping time
ping_mtu() {
local mtu=$1
local ping_time=$(ping -D -s $(($mtu - 28)) -c 4 $destination | grep 'avg' | awk -F '/' '{print $5}')
echo $ping_time
}
# Loop through MTU values and find the best one
for (( mtu=$max_mtu; mtu>=$min_mtu; mtu-- ))
do
echo "Testing MTU: $mtu"
avg_time=$(ping_mtu $mtu)
if [ -z "$avg_time" ]; then
echo "MTU $mtu: Packet fragmentation occurred or no response"
continue
fi
echo "MTU $mtu: Average ping time = $avg_time ms"
if (( $(echo "$avg_time < $best_time" | bc -l) )); then
best_mtu=$mtu
best_time=$avg_time
fi
done
echo "Optimal MTU: $best_mtu with average ping time: $best_time ms"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment