Finding max packet sizes for MTU calculations

Modified on Fri, 17 Jun, 2022 at 11:11 AM

Quick and easy batch file to find the maximum packet sizes across a network/WAN. Download the batchfile or copy/paste the code below into your own file.


Example usage: mtu.bat 8.8.8.8




@echo off
rem This batch will find out maximum MTU between local and remote host.

SetLocal EnableDelayedExpansion
set MTU=1473
set LASTGOOD=0
set LASTBAD=65536
set PACKETSIZE=28
set SERVER=



rem Check presence of target parameter.
if "%~1" EQU "" (
  echo.
  echo Usage: mtu IPv4 ^| hostname
  echo.
  echo On success the result is assigned to %%MAXMTU%% variable
  echo and %%ErrorLevel%% is set to 0. On error the %%MAXMTU%%
  echo variable is set to -1 value and %%ErrorLevel%% is set to
  echo non-zero value.
  goto :error
) else (
  set SERVER=%~1
)

rem Check server reachability.
ping -n 1 -l 0 -f -4 !SERVER! 1>nul
if !ERRORLEVEL! NEQ 0 (
  echo Error: cannot ping !SERVER!. Run "ping -n 1 -l 0 -f -4 !SERVER!" to see details.
  goto :error
)



:seek
rem Start looking for the maximum MTU.
ping -n 1 -l !MTU! -f -4 !SERVER! 1>nul
if !ERRORLEVEL! EQU 0 (
  set /A LASTGOOD=!MTU!
  set /A "MTU=(!MTU! + !LASTBAD!) / 2"
  if !MTU! NEQ !LASTGOOD! goto :seek
) else (
  set /A LASTBAD=!MTU!  
  set /A "MTU=(!MTU! + !LASTGOOD!) / 2"
  if !MTU! NEQ !LASTBAD! goto :seek
)



rem Print the result.
set /A "MAXMTU=!LASTGOOD! + !PACKETSIZE!"
echo Maximum MTU for !SERVER!: !MAXMTU! bytes.

rem Export %MAXMTU% variable.
EndLocal & set MAXMTU=%MAXMTU%
exit /B 0



:error
rem When something unexpected occurs.
EndLocal & set MAXMTU=-1
exit /B 1

Attachments (1)

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article