Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Set MTU value for any interface via AppleScript Network
Sometimes you have to change the Maximum Transmission Unit (MTU) on a network interface in Mac OS X to fix or debug a connection problem. You can dive into the command line using ifconfig:
ifconfig en0 mtu 1458
Or you can change it in the advanced settings of the Ethernet interface, but for other interfaces, you have to use ifconfig. Well, not anymore. Using the following script, you can easily set one or more interfaces to a new MTU value (on both Tiger (10.4) and Leopard (10.5), and possibly on older versions of Mac OS X):
(*
    Set MTU © RickoKid 2008
    Version 0.1

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)

set resultList to {}
set curMTU to ""
set OSVer to (do shell script "sw_vers -productVersion")
set tigerSupport to false
set legacySupport to false

if OSVer contains "10.4" or OSVer contains "10.3" or OSVer contains "10.2" or OSVer contains "10.1" or OSVer contains "10.1" then
  if OSVer contains "10.4" then
    set tigerSupport to true
  else
    display dialog "This script has only been tested on Mac OS X 10.4 and above.  You may encounter errors and/or crashes in earlier versions of Mac OS X" buttons {"OK"} default button "OK"
    set legacySupport to true
  end if
  
  set interfaceList to {}
  
  repeat with theInterface in words of (do shell script "ifconfig -lu")
    if theInterface contains "lo0" then
      set interfaceList to interfaceList & {"lo0 Loopback"}
    else if theInterface contains "en1" then
      set interfaceList to interfaceList & {"en1 (AirPort?)"}
    else if theInterface contains "en" then
      set interfaceList to interfaceList & {theInterface & " (Ethernet?)"}
    else if theInterface contains "fw" then
      set interfaceList to interfaceList & {theInterface & " (Firewire?)"}
    else
      set interfaceList to interfaceList & {theInterface}
    end if
  end repeat
else
  -- set interfaceList to paragraphs of (do shell script "networksetup -listallhardwareports | grep -e 'Hardware Port:' | sed 's/^.*: //'")
  tell application "System Events" to set interfaceList to name of every interface of network preferences
end if

set chosenInterface to choose from list interfaceList with prompt 
  "Please select the network interface(s) you would like to set the MTU for (or just click OK to select all interfaces):" with title 
  "Select Network Interface" with multiple selections allowed and empty selection allowed
if chosenInterface is not false then -- if something is chosen, but not if cancel is clicked.
  if chosenInterface is {} then set chosenInterface to interfaceList
  if (count of chosenInterface) is not 1 then
    set AppleScript's text item delimiters to return
    set newMTU to text returned of (display dialog "Please enter the new MTU value for these interfaces:" & return & return & chosenInterface with title "Set MTU" default answer curMTU)
  end if
  
  repeat with theInterface in chosenInterface
    if legacySupport or tigerSupport then
      set netid to word 1 of theInterface
    else
      set netid to (do shell script "networksetup -listallhardwareports | grep -A1 'Hardware Port: " & theInterface & "$'| tail -n1| sed 's/^.*: //'")
    end if
    if (count of chosenInterface) is 1 then
      set curMTU to last word of (do shell script "ifconfig " & netid & " |grep mtu")
      set newMTU to text returned of (display dialog "Please enter the new MTU value for " & theInterface & ":" with title "Set MTU" default answer curMTU)
    end if
    
    try
      do shell script "ifconfig " & netid & " mtu " & newMTU with administrator privileges and password
      --set theIP to the result
      set resultList to resultList & {"" & theInterface & ":" & tab & "MTU set to " & newMTU & "."}
    on error errorText
      set resultList to resultList & {"" & theInterface & ":" & tab & "Failed to set MTU."}
      --  display dialog ".  Make sure you are connected to the network via " & theInterface & "." with title "DHCP Lookup failure" buttons {"OK"} cancel button "OK" default button "OK"
    end try
    --  display dialog "" & theInterface & " DHCP server: " & theIP buttons {"OK"} default button "OK" with icon 1
  end repeat
  set AppleScript's text item delimiters to return
  if legacySupport then
    display dialog (resultList as text) with title "Set MTU" buttons {"OK"} default button "OK"
  else
    display alert "Set MTU" as informational message (resultList as text) buttons {"OK"} default button "OK"
  end if
end if
(You can find the original version of this script in this post on my site.) Due to the differences in systems older than Leopard however, the script can't lookup the friendly interface name (Built-in Ethernet, Airport, Firewire, and so on), but lists the interface's UNIX name (en0, en1, fw0, etc). It does make a wild guess at which interface it is though based on the usual assignments.

[robg adds: I haven't tested this one.]
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[13,142 views]  

Set MTU value for any interface via AppleScript | 3 comments | Create New Account
Click here to return to the 'Set MTU value for any interface via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set MTU value for any interface via AppleScript
Authored by: aquarajustin on May 23, '08 04:16:40AM
I appreciate the work you put into this script, but I don't think I've ever had to manually modify my MTU to troubleshoot a connection problem (and I am a Systems/Network Administrator with many o' Mac).

[ Reply to This | # ]
Set MTU value for any interface via AppleScript
Authored by: piper on May 27, '08 06:45:13AM

I wish you had posted this just a few months ago! I admin a network that, until recently, used a Sonicwall router that exhibited a very strange bug: When Mac clients connected to it, they would have to use a non-standard MTU setting. Sonicwall gave me a startup script that worked, but it was annoying. This would have been much easier. Oh well. I ended up replacing the sonicwall, but I'll keep this in my back pocket now.

~P



[ Reply to This | # ]
Set MTU value for any interface via AppleScript
Authored by: RickoKid on Jun 08, '08 11:04:23AM
It seems in Leopard (as at 10.5.3 anyway) that you can change the MTU for Airport interfaces in the Advanced->Ethernet tab of System Preferences as well as Ethernet interfaces.

I've updated the script though to incorporate a way to test for your maxmum MTU, taken in part from this hint.

Get the updated script at my blog: http://www.torrfamily.org/tumbleseed/products/set-mtu/.

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/

[ Reply to This | # ]