Skip to content

Instantly share code, notes, and snippets.

@msmarcal
Created October 26, 2025 12:08
Show Gist options
  • Select an option

  • Save msmarcal/48576eec6e75fa872c6a0358fd85b189 to your computer and use it in GitHub Desktop.

Select an option

Save msmarcal/48576eec6e75fa872c6a0358fd85b189 to your computer and use it in GitHub Desktop.
#!/bin/bash
# A simple script to find the maximum MTU between two hosts using a binary search approach.
# Define variables
host=$1
min_mtu=68
max_mtu=9050
# Check for required argument
if [ -z "$host" ]; then
echo "Usage: $0 <hostname_or_ip>"
exit 1
fi
echo "Checking maximum MTU to $host..."
echo "-----------------------------------------"
while [ $min_mtu -le $max_mtu ]; do
current_mtu=$(( (min_mtu + max_mtu) / 2 ))
packet_size=$(( current_mtu - 28 )) # Subtract 28 bytes for IP and ICMP headers
# Use ping to test the current packet size.
# -c 1: send one packet
# -M do: don't fragment
# -s: packet size in bytes
ping_output=$(ping -c 1 -M do -s $packet_size $host 2>&1)
# Check for error
if echo "$ping_output" | grep -q "error:"; then
max_mtu=$(( current_mtu - 1 ))
else
# If ping is successful, we can increase the size.
echo "Success with MTU: $current_mtu"
min_mtu=$(( current_mtu + 1 ))
max_mtu_found=$current_mtu
fi
done
echo "-----------------------------------------"
echo "The maximum MTU size to $host is: $max_mtu_found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment