Skip to content

Instantly share code, notes, and snippets.

@non7top
Last active January 12, 2017 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non7top/be6dc80d5e82573b3c3341a75674b2da to your computer and use it in GitHub Desktop.
Save non7top/be6dc80d5e82573b3c3341a75674b2da to your computer and use it in GitHub Desktop.
mtu_discover.sh
#!/bin/bash
# original https://idyllictux.wordpress.com/2009/06/19/script-find-maximum-mtu/
# adjusted it to detect pmtu hole
# read https://blog.cloudflare.com/path-mtu-discovery-in-practice/
# https://habrahabr.ru/post/136871/
PKT_SIZE=${2:-1472}
HOSTNAME="$1"
z=$( ping -M do -c 1 -w 3 -s $PKT_SIZE $HOSTNAME 2>&1 )
res=$?
count=$( echo "$z" | grep -c "Frag needed" )
if [ $count -eq 1 ]; then
echo "Got frag needed reply, trying to lower PKT_SIZE from $PKT_SIZE"
while [ $count -eq 1 ]; do
((PKT_SIZE--))
echo -n "$PKT_SIZE:> "
count=$((`ping -M do -c 1 -s $PKT_SIZE $HOSTNAME | grep -c "Frag needed"`))
done
echo
elif [ $res -eq 1 -a $count -eq 0 ]; then
# At this point it's impossible to identify if host not reachable or blackhole
# will try to lower pkt_size
echo "Didn't get any reply. Either the host is not reachable or we faced an MTU blackhole."
echo "trying to lower PKT_SIZE from $PKT_SIZE"
while [ $res -eq 1 ]; do
((PKT_SIZE--))
echo -n "$PKT_SIZE:> "
z=$( ping -M do -c 1 -w 3 -s $PKT_SIZE $HOSTNAME 2>&1 )
res=$?
done
echo
elif [ $res -ne 0 ]; then
echo "Unknown error"
echo "$z"
exit 1
fi
printf "Your Maximum MTU is [ $((PKT_SIZE + 28)) ] \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment