Skip to content

Instantly share code, notes, and snippets.

@jmarhee
Created July 3, 2024 13:08
Show Gist options
  • Save jmarhee/7e2eebb89f2a8675b3b98845e78dadac to your computer and use it in GitHub Desktop.
Save jmarhee/7e2eebb89f2a8675b3b98845e78dadac to your computer and use it in GitHub Desktop.
#!/bin/bash
# Define header size (IP + ICMP headers)
HEADER_SIZE=28
# Define the starting MTU size (adjust as needed)
START_MTU=1472
# Define the minimum MTU size (optional, adjust as needed)
MIN_MTU=100 # Common minimum Ethernet MTU
# Trap for control-C to allow for graceful exit
trap 'echo "Exiting script..."; exit 1' SIGINT
# Loop through MTU values using seq
for mtu in $(seq $START_MTU $MIN_MTU); do
# Calculate actual MTU considering header size
actual_mtu=$((mtu - HEADER_SIZE))
# Test if ping with "don't fragment" option succeeds (single packet)
echo "Trying MTU: $actual_mtu"
ping -M do -s $actual_mtu -c 1 8.8.8.8
# Check ping exit status (no need for redirection)
if [[ $? -eq 0 ]]; then
echo " --> Success! No fragmentation."
exit 0 # Exit script successfully if a suitable MTU is found
fi
done
# If loop finishes without successful ping, print a message
echo "No MTU found within the range ($START_MTU - $MIN_MTU) that avoids fragmentation."
exit 1 # Exit script with non-zero status to indicate failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment