事情是这样的,自带的Macbook Air 在实习单位入的是办公网,办公网走自动代理(需要配置PAC 文件)。同时回来宿舍或家里需要民用的宽带网络。切换的时候出现了问题。
对于网络配置,Mac OS X 本身提供了“网络位置”这个功能供不同的网络模式需求配置。比如我在宿舍设置了个“Dorm”的网络位置,里面自定义了DNS;又设置了“Libary”这个网络位置在图书馆上China-net 热点。来到公司,接入办公网自然又有“Office” 这个位置,且要高级设置“自动代理配置”填入PAC 文件路径。
问题说明
按理说网络位置分别设置具体参数互不干扰,但因为我在宿舍/家里网络下需要开Shad**sock 客户端(你懂的),一打开SS 就会污染全局的自动代理配置的PAC 文件路径。如此每次重新接入办公网就得重新设置PAC 文件路径——再次走“打开网络偏好设置-高级-代理-自动代理配置填入PAC 文件路径”的流程,多次下来嫌麻烦了。
解决问题
今天下决心解决问题,想到了可以采用Shell 将切换网络位置、填写PAC路径、打开/关闭Shad**sock 客户端APP 的几个操作做成自动化脚本。不断变换英文关键词在Google 搜索,后来借助老外的代码,借助Shell + AppleScript 代码将脚本保存为APP,这样每次需要切换网络的时候打开该APP即可。
解决方案
打开Launchpad 的其它-脚本编辑器,编写AppleScript 代码,Shell 代码请自行用编辑器编写,然后保存的时候保存为“应用程序”:
1)切换网络位置为Dorm(宿舍网络),同时打开Shad**socks 客户端(为科学上网)。以下代码保存为一个“应用程序”。
tell application "System Events" tell network preferences do shell script "scselect 'Dorm'" do shell script "sudo networksetup -setairportpower AirPort on" user name "用户名" password "密码" with administrator privileges do shell script "open /Applications/Shad**socksX.app" user name “用户名" password "密码" with administrator privileges end tell end tell |
上面的代码隐藏了部分内容,比如用户名密码这些需要自定义,网络位置也需要根据实际修改(网络位置需要为英文)。
2.1)切入办公网:关闭Sha***socks 客户端(因为办公网自带科学上网),切换网络位置为Office(办公网),配置自动代理配置PAC文件。以下代码保存为一个“应用程序”。
tell application “Shad**socksX" quit end tell tell application "System Events" tell network preferences do shell script "scselect 'Office'" do shell script "sudo networksetup -setairportpower AirPort on" user name “用户名" password "密码" with administrator privileges do shell script "/Users/name/documents/setpac.sh" user name “用户名" password "密码" with administrator privileges end tell end tell |
2.2)setpac.sh 代码如下,需要放入相对应的位置,autoProxyURL 参数请设置为自己的:
#!/bin/sh #################################################################################################### # # More information: https://macmule.com/2014/12/07/how-to-change-the-automatic-proxy-configuration-url-in-system-preferences-via-a-script/ # # GitRepo: https://github.com/macmule/setAutomaticProxyConfigurationURL # # License: http://macmule.com/license/ # #################################################################################################### # HARDCODED VALUES ARE SET HERE autoProxyURL=“PAC 文件路径" # CHECK TO SEE IF A VALUE WAS PASSED FOR $4, AND IF SO, ASSIGN IT if [ "$4" != "" ] && [ "$autoProxyURL" == "" ]; then autoProxyURL=$4 fi # Detects all network hardware & creates services for all installed network hardware /usr/sbin/networksetup -detectnewhardware IFS=$'/n' #Loops through the list of network services for i in $(networksetup -listallnetworkservices | tail +2 ); do # Get a list of all services beginning 'Ether' 'Air' or 'VPN' or 'Wi-Fi' # If your service names are different to the below, you'll need to change the criteria if [[ "$i" =~ 'Ether' ]] || [[ "$i" =~ 'Air' ]] || [[ "$i" =~ 'VPN' ]] || [[ "$i" =~ 'Wi-Fi' ]] ; then autoProxyURLLocal=`/usr/sbin/networksetup -getautoproxyurl "$i" | head -1 | cut -c 6-` # Echo's the name of any matching services & the autoproxyURL's set echo "$i Proxy set to $autoProxyURLLocal" # If the value returned of $autoProxyURLLocal does not match the value of $autoProxyURL for the interface $i, change it. if [[ $autoProxyURLLocal != $autoProxyURL ]]; then /usr/sbin/networksetup -setautoproxyurl $i $autoProxyURL echo "Set auto proxy for $i to $autoProxyURL" fi fi # Enable auto proxy once set /usr/sbin/networksetup -setautoproxystate "$i" on echo "Turned on auto proxy for $i" done unset IFS echo "Auto proxy present, correct & enabled for all targeted interfaces" |
# HARDCODED VALUES ARE SET HERE
autoProxyURL=“PAC 文件路径"
# CHECK TO SEE IF A VALUE WAS PASSED FOR $4, AND IF SO, ASSIGN IT
if [ "$4" != "" ] && [ "$autoProxyURL" == "" ]; then
autoProxyURL=$4
fi
# Detects all network hardware & creates services for all installed network hardware
/usr/sbin/networksetup -detectnewhardware
IFS=$’/n’
#Loops through the list of network services
for i in $(networksetup -listallnetworkservices | tail +2 );
do
# Get a list of all services beginning ‘Ether’ ‘Air’ or ‘VPN’ or ‘Wi-Fi’
# If your service names are different to the below, you’ll need to change the criteria
if [[ "$i" =~ ‘Ether’ ]] || [[ "$i" =~ ‘Air’ ]] || [[ "$i" =~ ‘VPN’ ]] || [[ "$i" =~ ‘Wi-Fi’ ]] ; then
autoProxyURLLocal=`/usr/sbin/networksetup -getautoproxyurl "$i" | head -1 | cut -c 6-`
# Echo’s the name of any matching services & the autoproxyURL’s set
echo "$i Proxy set to $autoProxyURLLocal"
# If the value returned of $autoProxyURLLocal does not match the value of $autoProxyURL for the interface $i, change it.
if [[ $autoProxyURLLocal != $autoProxyURL ]]; then
/usr/sbin/networksetup -setautoproxyurl $i $autoProxyURL
echo "Set auto proxy for $i to $autoProxyURL"
fi
fi
# Enable auto proxy once set
/usr/sbin/networksetup -setautoproxystate "$i" on
echo "Turned on auto proxy for $i"
done
unset IFS
echo "Auto proxy present, correct & enabled for all targeted interfaces"
以上涉及到的代码备份一份到了Gist 那里,地址见此。代码就不解释了哈,有需要请自行研究。
这样,每次需要切换网络位置的时候就自动走流程,省却了自己一步步操作,真正“一键”完成!
参考资料:
How To: Change the “Automatic Proxy Configuration” url in System Preferences via a Script
http://www.cyberciti.biz/faq/mac-osx-applescript-run-shell-script/
原创文章,作者:sunnyman218,如若转载,请注明出处:https://blog.ytso.com/244192.html