有没有一种方法可以从命令行或苹果脚本启动/停止Internet共享?
问题是我在家里和公司之间随身携带笔记本电脑。一方面,我获得了无线互联网(因此必须禁用Internet共享),另一方面,我从一条以太网电缆获得了互联网,并通过创建一个小型无线网络,将计算机设置为与其他设备共享互联网。
但是每次进入位置时都必须进入“系统偏好设置”并启动/停止互联网共享变得有些乏味,所以我希望有一个快速的命令或脚本来启动并按需进行切换。
有任何提示或想法吗?
有没有一种方法可以从命令行或苹果脚本启动/停止Internet共享?
问题是我在家里和公司之间随身携带笔记本电脑。一方面,我获得了无线互联网(因此必须禁用Internet共享),另一方面,我从一条以太网电缆获得了互联网,并通过创建一个小型无线网络,将计算机设置为与其他设备共享互联网。
但是每次进入位置时都必须进入“系统偏好设置”并启动/停止互联网共享变得有些乏味,所以我希望有一个快速的命令或脚本来启动并按需进行切换。
有任何提示或想法吗?
Answers:
要从CLI启动Internet共享:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist
要停止,请将负载更改为unload。
请注意,如果您在运行该对话框时打开了“首选项”窗格,则该更改不会立即生效(UI不会更新),但是它确实起作用。
unload
做load
一次。一个很好的检查命令是ps ax | egrep '[ /](PID|boo|nat)'
。
/System/Library/LaunchDaemons/com.apple.InternetSharing.plist: No such file or directory
一种方法是使用GUI脚本-默认情况下,系统偏好设置不支持任何Applescript。
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
我从mankoff的答案中得到了提示,并将其包装在AppleScript中。我正在使用Automator中的此脚本,以便可以轻松地将其用作服务并为其提供键盘快捷键。
切换互联网共享:
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
我发现一个系统终于可以工作了,并且当wifi共享中断的频率降低时,它可以在一分钟内自动恢复。
解决方案是一个~/Library/LaunchAgents/com.me.wifisharingup.plist
带有以下内容的守护程序:
<?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>
您可以看到,它每分钟运行一次下面的简单脚本。请小心,使先前的plist由根拥有,并使用以下命令启动它:
sudo chown root com.me.wifisharingup.plist
sudo launchctl load /Users/me/Library/LaunchAgents/com.me.wifisharingup.plist
每分钟启动的脚本(别忘了使其可执行):
#!/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
我认为简单的定期(每分钟)调用会ipconfig getifaddr en1
刷新什么是wifi共享守护程序。不管是什么,任何时候wifi共享失败,它都会失去自己分配的IP地址,然后ipconfig getifaddr en1
失败,所以我的脚本完全重置了wifi,使其重建了以前的状态并恢复了wifi共享。
到目前为止,它在没有键盘,鼠标或显示器的MacMini内已经工作了几天,但仅插入以太网并允许我的wifi小工具访问世界。
真正简单的方法是将@Philip的答案与NetworkLocation应用程序结合起来。NL可以告诉您您的位置,并在感觉到您已更改位置时自动运行AppleScript。
我认为如果您有一台笔记本电脑,这是必需的软件-否则,每当我更改位置时,始终必须手动重置多个设置是PITA。
与发布的其他applescript稍有不同(我认为更好,但是…)。有选择有时会有所帮助。
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
由于以前发布的GUI脚本解决方案要求国际用户调整窗口和按钮名称,因此我想出了一个可与任何系统语言一起使用的版本。它还适用于其他共享选项,并提供有关共享状态的本地化反馈。我基于此使用两种不同的Automator服务,一种用于切换文件共享,另一种用于切换Internet共享。
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
在进行任何配置之前/之后,需要使用以下命令关闭/打开共享所涉及的一个或多个接口:
networksetup -setairportpower en1 off
然后。(networksetup -setairportpower en1 on
)
我的wifi被列为en1
,供您查找networksetup -listnetworkserviceorder
。更重要的是,开始共享Internet时,您需要添加更详细的NAT功能,然后只需设置启用的位即可。
NAT通过以下位打开/关闭: -int 0
=关闭
-int 1
=打开
唯一写的东西 /Library/Preferences/SystemConfiguration/com.apple.nat.plist
与
defaults write /Library/Preferences/SystemConfiguration/com.apple.nat\
NAT -dict Enabled -int 0
是
<!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>
您将需要将plist文件更改为如下所示:
<!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>
如您所见,我的plist NAT文件可能需要一些自定义才能满足您的需求。
我使用以下脚本在2011年初的MacBook Pro / 10.11.3 El Capitan上切换Internet共享,因此,使用较新的硬件,您可能会获得更短的延迟。它采用了为此目的而发布的多个脚本中的想法(本地化,使用来打开/关闭wifi networksetup
,System Preferences
完成时关闭)。
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
您可能想看一下这个问题(可能是Stack Overflow的非主题)及其答案:如何使用AppleScript启动/停止Internet共享。
do shell script "/bin/launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges
/usr/bin/defaults write /Library/Preferences/SystemConfiguration/com.apple.nat NAT -dict Enabled -int 0