Advertisement
crossboy007

Maximum MTU

Mar 18th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.54 KB | None | 0 0
  1. #! /bin/sh
  2. #edit by stev-o - find the MTU available for current network
  3.  
  4. #Usage: $0 <exthost>
  5. #
  6. #$0 = Me
  7. #<exthost> = custom host used for test, different from built-in ones, needed if they go down or close: this is optional
  8. #
  9. #P.S.:due to a limitation of busybox "ping" command, make sure the hosts passed reject fragmented packet, otherwise the test will be compromized...
  10.  
  11. export PATH=$PATH:"/usr/sbin:/bin:/usr/bin:/sbin"
  12.  
  13. MTU="32"
  14. STEP="750"
  15. STROKES_LIMIT="30"
  16. PACKETS_HEADER="28"
  17. HOST_1="8.8.8.8"  #Hosts MUSTN'T reply packets greater than effective MTU so not all hosts are GOOD for test...
  18. HOST_2="8.8.4.4"
  19. HOST_3="208.67.222.222"
  20. HOST_EXT="$1"
  21.  
  22. probe() {  #ping host with one icmp-echo packet of variable size: the output is passed through various shell filters
  23.     if [ "$HOST" != "" ]; then  #avoid processing NULL, if exthost is not given
  24.     echo "Sending $MTU bytes to $HOST"
  25.     ping -s $MTU -c "1" $HOST > /dev/null 2>&1
  26.     RESULT=$?
  27.     #recursive output message
  28.         if [ "$RESULT" = "0" ]; then
  29.         echo "----> Contiguous"
  30.         else
  31.         echo "----> Fragmented"
  32.         fi
  33.     echo
  34.     fi
  35. }
  36.  
  37. answer() {
  38.     echo
  39.     echo "It's reasonable to say that $MTU_LASTGOOD bytes is the largest contiguous packet size ($MTU includes $PACKETS_HEADER ICMP/IP Headers)"
  40.     echo
  41.     echo "MTU should be set to $MTU"
  42.     echo
  43. }
  44.  
  45. #Let's start testing, with a small echo-packet, if the host is at least reachable
  46. for HOST in "$HOST_EXT" "$HOST_1" "$HOST_2" "$HOST_3"
  47. do
  48.     probe
  49.     if [ "$RESULT" = "0" ]; then  #If the 1st host fails, try the others
  50.     HOSTGOOD="1"
  51.     break
  52.     else
  53.     HOSTGOOD="0"
  54.     fi
  55. done
  56.  
  57. #No valid hosts founded: exit...
  58. if [ "$HOSTGOOD" != "1" ]; then
  59. echo "No reachable hosts"
  60. exit 1
  61. fi
  62.  
  63. #The host is pingable, so let's go on with larger packets....    
  64. MTU="$STEP"
  65. STROKES="0"
  66. while [ "$STROKES" -lt "$STROKES_LIMIT" ]
  67. do
  68.     STEP=`expr "$STEP" / 2 + "$STEP" % 2`
  69.     probe
  70.         if [ "$RESULT" = "0" ]; then
  71.             if [ "$MTU" = "$MTU_LASTGOOD" ]; then
  72.             break
  73.             else
  74.             MTU_LASTGOOD="$MTU"
  75.             MTU=`expr "$MTU" + "$STEP"`
  76.             fi
  77.         else
  78.         MTU=`expr "$MTU" - "$STEP"`
  79.         fi
  80.     STROKES=`expr "$STROKES" + 1`  #limit the max loop retries in case of successive host failures
  81. done
  82.  
  83. #Maximum retries value reached: exit...
  84. if [ "$STROKES" = "$STROKES_LIMIT" ]; then
  85. echo
  86. echo "Test limit exceeded"
  87. exit 2
  88. fi
  89.  
  90. #Add ICMP default header to the found value
  91. MTU=`expr "$MTU" + "$PACKETS_HEADER"`
  92. answer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement