Skip to content

Instantly share code, notes, and snippets.

@kitchen
Last active October 30, 2018 19:10
Show Gist options
  • Save kitchen/1c82b3ab3fd8ef5df606d2f7333bbf04 to your computer and use it in GitHub Desktop.
Save kitchen/1c82b3ab3fd8ef5df606d2f7333bbf04 to your computer and use it in GitHub Desktop.
Determine your MTU on MacOS empirically using ping
#!/bin/bash
start=${1:-1200}
finish=${2:-1600}
echo "determining MTU by bisecting between $start and $finish bytes per packet."
trap "exit 0" SIGINT SIGTERM
function do_ping {
size=$1
ping -q -t 4 -c 10 -o -D -s $size 8.8.8.8 > /dev/null 2> /dev/null
ret=$?
return $ret
}
echo "checking that $start works ..."
if ! do_ping $start ; then
echo "lower bound $start doesn't seem to work, try a lower one?"
exit 2
fi
echo "checking that $finish doesn't work ..."
if do_ping $finish ; then
echo "upper bound $finish seems to work, try a higher one? (or just use it?)"
exit 2
fi
while true; do
try=$[(start + finish) / 2]
if [[ $try -eq $start || $try -eq $finish ]]; then
echo "seems that $try is your best bet, try that!"
exit 0
fi
echo -n "trying $try ... "
if do_ping $try; then
echo "yep!"
start=$try
else
echo "nope :("
finish=$try
fi
done
@kitchen
Copy link
Author

kitchen commented Oct 30, 2018

Note that the ping command line options used here are for MacOS. If you're using something else you'll need to adjust them. PRs welcome ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment