How can I toggle internet sharing from the command line? [duplicate]
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?
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.
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
I had far better results (for an already configured sharing setup) on 10.11.6 with the simple variations...
sudo launchctl
start
com.apple.NetworkSharing
and
sudo launchctl
stop
com.apple.NetworkSharing
respectively.
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
Definitive workaround to wifi-sharing constant breaking down in MacosX
I have found a system that finally works and when, less frequently wifi sharing breaks, it manages to recover it automatically in a minute.
The solution is a ~/Library/LaunchAgents/com.me.wifisharingup.plist
daemon with the next contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.juanfal.wifisharingup</string>
<key>ProgramArguments</key>
<array>
<string>/Users/mi/bin/wifisharingup.sh</string>
</array>
<key>Nice</key>
<integer>1</integer>
<key>StartInterval</key>
<integer>60</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/me/Library/Logs/wifisharingup.err</string>
<key>StandardOutPath</key>
<string>/Users/me/Library/Logs/wifisharingup.out</string>
</dict>
</plist>
You can see, each minute it runs the simple script that follows. Be careful making the previous plist be owned by the root and launch it with:
sudo chown root com.me.wifisharingup.plist
sudo launchctl load /Users/me/Library/LaunchAgents/com.me.wifisharingup.plist
The script it launches each minute (don't forget to make it executable) is:
#!/bin/sh
if [[ ! `ipconfig getifaddr en1` ]]; then
/usr/sbin/networksetup -setairportpower en1 off
/usr/sbin/networksetup -setairportpower en1 on
echo `date` >> "/Users/me/Library/Logs/wifisharingup.err"
else
touch "/Users/me/Library/Logs/wifisharingup.out"
fi
I think the simple periodically (each minute) call to ipconfig getifaddr en1
refreshes something in what is the wifi sharing daemon. Whatever it is, any moment the wifi sharing fails, it looses the self assigned IP address, and then, ipconfig getifaddr en1
fails, so my script totally resets wifi, making it rebuild its previous status and recovering the wifi-sharing.
It has been working for days so far inside a MacMini without keyboard, mouse or monitor, but only plugged into the Ethernet and giving my wifi gadgets access to the world.