From eb57503871065b74f75115e7f244b7effb98cf8c Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 13 Jul 2024 18:34:30 +0200 Subject: [PATCH] feat: add MTU finder script for optimal network configuration Introduced a new script to determine the optimal Maximum Transmission Unit (MTU) size for a given destination IP. The script includes a function to display usage help, checks for a mandatory destination IP argument, and calculates the optimal MTU using binary search and ping tests. This tool assists in configuring network parameters for efficient data transmission, thereby potentially reducing packet fragmentation and improving network performance. --- mtufinder | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 mtufinder diff --git a/mtufinder b/mtufinder new file mode 100755 index 0000000..791b831 --- /dev/null +++ b/mtufinder @@ -0,0 +1,54 @@ +#!/bin/bash + +# Function to display help text +display_help() { + echo "Usage: $0 [options] " + echo + echo "This script determines the optimal MTU size for a given destination IP." + echo + echo "Options:" + echo " -h, --help Show this help text and exit." + echo + echo "Example:" + echo " $0 192.168.1.1" +} + +# Check if help is requested +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + display_help + exit 0 +fi + +# Check for the mandatory destination IP argument +if [[ -z "$1" ]]; then + echo "Error: Destination IP is required." + echo + display_help + exit 1 +fi + +# Destination IP from command line argument +DESTINATION_IP="$1" + +# Function to find the optimal MTU size +find_optimal_mtu() { + local max_mtu=1500 + local min_mtu=1200 + local optimal_mtu=$max_mtu + + while [[ $min_mtu -le $max_mtu ]]; do + local current_mtu=$(( (min_mtu + max_mtu) / 2 )) + if ping -c 1 -M do -s $((current_mtu - 28)) $DESTINATION_IP >/dev/null 2>&1; then + optimal_mtu=$current_mtu + min_mtu=$((current_mtu + 1)) + else + max_mtu=$((current_mtu - 1)) + fi + done + + echo $optimal_mtu +} + +# Run the function and print the optimal MTU +optimal_mtu=$(find_optimal_mtu) +echo $optimal_mtu