Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to start/stop internet sharing from the command line or maybe an apple script?

The thing is that I move around my laptop between home and work. At one place I get wireless internet (and thus I have to disable internet sharing), and at the other I get internet from an ethernet cable and set up the computer to share the internet to other devices by creating a small wireless network.

But it's getting a bit tedious having to go into System Preferences and start/stop the internet sharing every time I switch locations, so I would like to have a quick command or script to launch and do the switch on demand.

Any hints or ideas?

share|improve this question
    
Mankoff, when I run your suggestion: sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist it seems to run fine and even the sharing preferences gui is updated correctly when refreshed, however the connection is actually not working. If I run the command again it even fails because it thinks its already loaded. Also, if I run the "top" command to see the process running i do not see "InternetSharing" but if I start it in the preferences gui I will see the "InternetSharing" process actually running and the connection works. Any ideas why I am h – MikeZ Feb 1 '11 at 13:55
    
Not sure why you get that behavior. I see "InternetSharing" in top (or easier, ps aux | grep -i internet). – user588 Feb 1 '11 at 13:55
    
I'm running OS X 10.6. If you have a different version maybe that is why... – user588 Feb 1 '11 at 13:55
    
Also, this should probably be a comment to my answer, not an answer in itself. Might want to delete the answer... – user588 Feb 1 '11 at 13:55
    
Yea, certainly this should be a comment to your answer, but for some reason I am not able to comment on there answers unless someone has already made a comment. Maybe because I am a new user. I am running OS X 10.6.4. Another thing I noticed is that when I launch internet sharing in the preferences gui is that the process natd (network address translation daemon) also gets started. I'm wondering if I also need to launch this process, currently investigating how to do this, there seem to be complex arguments needed to start this process. – MikeZ Feb 1 '11 at 13:55

10 Answers 10

up vote 17 down vote accepted

To start Internet Sharing from the CLI:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist

To stop, change the load to unload.

Note that if you have the pref pane open when you run this you will not see the change take effect immediately (the UI won't update), but it does work.

share|improve this answer
    
Awesome! It works, thanks! – Juan A. Navarro Sep 23 '10 at 16:05
    
Strange, after a while, it stopped working. If I run the command and then open the pref pane I see that sharing is enabled, however the connection isn't actually being shared. If I manually stop and start sharing on the pref pane, then it works again without problems. Any clues about what could be wrong? – Juan A. Navarro Jan 5 '11 at 10:30
    
No idea why, but you can try other answers, they might work... – user588 Jan 5 '11 at 16:24
2  
The receipe of Ken works! But... there is a bug (I'm hunting). You have to leave a 30 second delay after the unload to be able to do a load again. One good check command is ps ax | egrep '[ /](PID|boo|nat)'. – daniel Azuelos May 12 '12 at 21:50
1  
try: /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist instead ;) – guerrerocarlos Mar 22 at 15:10

One way of doing this is by GUI scripting—System Preferences doesn't have any Applescript support by default.

tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
delay 1
tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of row 8 of table 1 of scroll area 1 of group 1 of window "Sharing" -- change to row 10 if you are using anything before Snow Leopard
    delay 1
    if (exists sheet 1 of window "Sharing") then
        click button "Start" of sheet 1 of window "Sharing"
    end if
end tell
ignoring application responses
    tell application "System Preferences" to quit
end ignoring
share|improve this answer
3  
Perhaps, but it works and the answer is within the constraints of the OP's question: "Is there a way to start/stop internet sharing from the command line or maybe an apple script?" Downvoting really ought to be saved for the really egregious answers. Thanks – Philip Regan Sep 22 '10 at 16:36
1  
Hence no "official" downvote. :). – user588 Sep 22 '10 at 16:39
2  
"works and is within constraints" makes me think we should have a new site: rubegoldberg.stackexchange.com – user588 Sep 22 '10 at 16:42
2  
Isn't that what thedailywtf.com is for? ;-) – Philip Regan Sep 22 '10 at 16:57
1  
@Juan: Just be forewarned that if Apple changes the layout of Systems Preferences and the Sharing pane with an upgrade, then my script has the real potential break as well because GUI scripting relies on very strict parameters. – Philip Regan Mar 24 '11 at 13:36

I took a cue from mankoff's answer and wrapped it up in an AppleScript. I'm using this script from Automator so that I can easily use it as a service and give it a keyboard shortcut.

Toggle Internet Sharing:

register_growl()

try
    if isRunning("InternetSharing") then
        do shell script "launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

        if isRunning("InternetSharing") then
            error "Internet Connection Sharing was Not Disabled"
        else
            my growlnote("Success", "Internet Connection Sharing Disabled")
        end if

    else
        do shell script "launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

        if isRunning("InternetSharing") then
            my growlnote("Success", "Internet Connection Sharing Enabled")
        else
            error "Internet Connection Sharing was Not Enabled"
        end if

    end if

on error errMsg
    my growlnote("Error", errMsg)

end try

on isRunning(processName)
    try
        return 0 < length of (do shell script "ps ax | grep -v grep | grep " & processName)
    on error
        return false
    end try
end isRunning

on register_growl()
    try
        tell application "GrowlHelperApp"
            set the notificationsList to {"Success", "Warning", "Error"}
            register as application "Toggle Internet Connection Sharing" all notifications notificationsList default notifications notificationsList icon of application "Sharing"
        end tell
    end try
end register_growl

on growlnote(growltype, str)
    try
        tell application "GrowlHelperApp"
            notify with name growltype title growltype description str application name "Toggle Internet Connection Sharing"
        end tell
    end try
end growlnote
share|improve this answer
    
Does not really work on Lion for me. It displays the growl success message and toggles the option in the system preferences but the WiFi-symbol doesn't change to the sharing (and other devices can't see that the wifi is shared, so I think it isn't on :( – alopix Nov 30 '11 at 17:12

The really easy way to do it is to combine @Philip's answer with the NetworkLocation application. NL can tell where you are, and automatically run an AppleScript when it senses you've changed locations.

I think it's required software if you have a laptop—otherwise, it's a PITA to always have to manually reset several settings whenever I change locations.

share|improve this answer
    
Sounds like Marco Polo (symonds.id.au/marcopolo) but without the Marco Polo steroids. :) – Ian C. Jan 4 '11 at 4:52
    
@Ian - That page says MP has known issues with Snow Leopard. The Google Group says "the MarcoPolo project isn't dead, per se, but it's being developed very slowly. Don't expect a new version soon." The git repository shows no update since September 2009. Sounds to me like the steroids might be catching up with someone… ;-) – Dori Jan 4 '11 at 5:06
    
Can't say I've had any issues with it on SL save for BlueTooth. I just don't use the BT enable/disable/detect feature and it works rather brilliantly. (But point taken) – Ian C. Jan 4 '11 at 14:43

Slightly different than the other applescript posted (I think in a better way but…). Having options sometimes can help.

 tell application "System Preferences"
   activate
   reveal (pane id "com.apple.preferences.sharing")
 end tell

 tell application "System Events"
   tell process "System Preferences"
     try
       click checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing"

       if checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
         repeat until sheet of window 1 exists
           delay 0.5
         end repeat
       end if

       if (sheet of window 1 exists) then
         click button "Start" of sheet of window 1
       end if

       tell application "System Preferences" to quit
       activate (display dialog "Internet Sharing preferences sucessfully flipped")

     on error     
       activate
       display dialog "something went wrong in automation but you are probably in the right menu…"
       return false
     end try
   end tell
 end tell
share|improve this answer
/usr/bin/defaults write /Library/Preferences/SystemConfiguration/com.apple.nat NAT -dict Enabled -int 0
share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Since the GUI scripting solution posted previously requires international users to adjust the window and button names, I came up with a version that works with any system language. It also works for the other sharing option, and gives localized feedback about the sharing state. I use two different Automator services based on this, one to toggle file sharing and one to toggle internet sharing.

tell application "System Preferences"
    set current pane to pane "com.apple.preferences.sharing"
    set localized_window to the localized name of the current pane
    set localized_app to (localized string "System Preferences")
    set localized_ok to {localized string "OK"} -- File sharing
    set localized_start to {localized string "START"} -- Internet sharing
end tell
delay 0.3
tell application "System Events"
tell process "System Preferences"
    click checkbox 1 of row 8 of table 1 of scroll area 1 of group 1 of window localized_window
    delay 0.2
    select row 8 of table 1 of scroll area 1 of group 1 of window localized_window
    -- change row numbers to the service you want toggled
    if (exists sheet 1 of window localized_window) then
        try
            click button (localized_ok as string) of sheet 1 of window localized_window
        on error
            click button (localized_start as string) of sheet 1 of window localized_window
        end try
    end if
    set sharing_state to the value of item 1 of static text of group 1 of window localized_window
end tell

tell application "System Preferences" to quit
display notification sharing_state with title localized_app
--  display notification exists since OS 10.9, for older systems use: 
--  display dialog sharing_state buttons {localized_ok} default button 1 with title localized_app giving up after 1.5
end tell
share|improve this answer

Before/after any configuration the interface or interfaces involved in sharing need to be turned off/on with:

networksetup -setairportpower en1 off

then on. (networksetup -setairportpower en1 on)

My wifi was listed as en1, to lookup yours use networksetup -listnetworkserviceorder. More to the point, when starting internet sharing you will need to add more detailed NAT functionality then just setting the enabled bit.

NAT is turned on/off with bit: -int 0 = off -int 1 = on

The only thing that is written in /Library/Preferences/SystemConfiguration/com.apple.nat.plist with

defaults write /Library/Preferences/SystemConfiguration/com.apple.nat\
    NAT -dict Enabled -int 0

is

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>NAT</key>
        <dict>
                </dict>
                <key>Enabled</key>
                <integer>1</integer>
        </dict>
</dict>
</plist>

You will need to change your plist file to look like the following:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>NAT</key>
        <dict>
                <key>AirPort</key>
                <dict>
                        <key>40BitEncrypt</key>
                        <integer>1</integer>
                        <key>Channel</key>
                        <integer>0</integer>
                        <key>Enabled</key>
                        <integer>0</integer>
                        <key>NetworkName</key>
                        <string>FancyHostNome</string>
                        <key>NetworkPassword</key>
                        <data>
                        </data>
                </dict>
                <key>Enabled</key>
                <integer>1</integer>
                <key>NatPortMapDisabled</key>
                <false/>
                <key>PrimaryInterface</key>
                <dict>
                        <key>Device</key>
                        <string>en4</string>
                        <key>Enabled</key>
                        <integer>0</integer>
                        <key>HardwareKey</key>
                        <string></string>
                        <key>PrimaryUserReadable</key>
                        <string>InfiniBand</string>
                </dict>
                <key>PrimaryService</key>
                <string>XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</string>
                <key>SharingDevices</key>
                <array>
                        <string>en1</string>
                </array>
        </dict>
</dict>
</plist>

As you can see my plist NAT file may need a little customization to fit your needs.

share|improve this answer

I use the following script to toggle internet sharing on an early 2011 MacBook Pro / 10.11.3 El Capitan, so with newer hardware you might get away with shorter delays. It adopts ideas from several scripts posted for this purpose (localisation, turning wifi on/off using networksetup, close System Preferences when done).

tell application "System Preferences"
    activate
    reveal (pane id "com.apple.preferences.sharing")
    set localized_window to the localized name of the current pane
end tell

tell application "System Events" to tell process "System Preferences"
    delay 1
    repeat with r in rows of table 1 of scroll area 1 of group 1 of window localized_window
        if (value of static text of r as text) starts with "Internet" then
            set sharingBool to value of checkbox of r as boolean
            select r
            if sharingBool is true
              do shell script "/usr/sbin/networksetup -setairportpower en1 off"
            else
              do shell script "/usr/sbin/networksetup -setairportpower en1 on"
            end if
            click checkbox of r
        end if
    end repeat
    delay 1

    if (exists sheet 1 of window localized_window) then
      click button "Start" of sheet 1 of window localized_window
    end if

end tell

ignoring application responses
  tell application "System Preferences" to quit
end ignoring
share|improve this answer

You might want to take a look at this (possibly offtopic for StackOverflow) question and its answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.