Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

6 Answers 6

up vote 16 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

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

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

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

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

share|improve this answer
5  
Please don't just simply link to another answer. Actually answer the question and provide links to support it. If you don't have an answer that directly answers the question, then don't answer it. Otherwise it just looks like you are fishing for reputation. –  Philip Regan Sep 22 '10 at 13:53
    
Why would you duplicate content when there’s a perfect question and answer. On the contrary, I’d −1 anyone who copies and paste a question like that. I don’t know who would be stupid enough to “fish” for reputation. If you believe that I actually care about reputation you need to find better things to do. –  Martín Marconcini Sep 22 '10 at 13:57
4  
Why? "Linking is not a bad thing as long as you actually provide some context to your answer. If all you are going to do is post a link in your answer, you aren't really answering, you are just providing a detour for the user. At the very least you could explain where the link goes and what it is going to show the user (maybe even quote some of important stuff in the answer so that the OP doesn't have to click the link to possibly answer their question)." From meta.stackexchange.com/questions/7515/why-is-linking-bad –  Philip Regan Sep 22 '10 at 14:02
    
"most people who say that linking is bad on the web have absolutely no clue how the web works. The only thing bad about linking is the fact that you have no guarantee that the thing you are linking to will be open tomorrow or the next day or 7 years down the road. That's the only problem. “ From meta.stackexchange.com/questions/7515/why-is-linking-bad (note: the provided link is the url shortened link of another stack exchange website, Stack Overflow no less, the father of Stack Exchange. It’s not a google + I’m feeling lucky link. If SO link fails in the future there’s no hope. –  Martín Marconcini Sep 22 '10 at 15:30
4  
Links die over time so just answer the question and the answer lives on longer. Simply answering the question makes it easier on the OP. I'm not saying that linking is bad, but just that simply posting a link isn't what is considered a sufficient answer in SE. From the same thread: "One problem with links is that they can die/expire, the pages on the target site get reorganised or deleted, and the link is no longer valid." and "posting only a link as an answer requires the person reading the answer to perform an additional action (click on the link) in order to see your answer" –  Philip Regan Sep 22 '10 at 16:33

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.