我已经在 OS X 10.7.3 中配置了 VPN 连接,但是由于我要连接的网络使用公司代理、具有自定义 Maven 存储库服务器和其他特定于网络的设置(例如远程驱动器),我想在 VPN 登录/注销时运行脚本以:
~/.m2/settings.xml以指向他们的 Maven 仓库smb://共享,不过,我不知道如何实现这一点。
如果您已经配置了 VPN,您可以通过命令行连接到它。正如我在这个答案中所解释的,您可以分别创建两个用于登录和注销的 shell 函数,方法是将它们添加到您的~/.bash_profile- 请参阅该vpn-disconnect函数的链接答案。
function vpn-connect {
/usr/bin/env osascript <<-EOF
tell application "System Events"
tell current location of network preferences
set VPN to service "UniVPN" -- your VPN name here
if exists VPN then connect VPN
end tell
end tell
EOF
# insert your commands here
}
Run Code Online (Sandbox Code Playgroud)
只需在EOF标记后包含您需要的自定义命令。
如果你想要一个 GUI 方式来做到这一点,打开Automator.app并创建一个新的Application。然后,从左窗格拖动操作以运行 AppleScript 和 Shell 脚本,并插入如下所示的命令。

然后,您可以将此伪应用程序放入 Dock 以快速启动它。
另一种解决方案是使用LaunchDaemon监视特定目录并在该目录有修改时启动外部脚本。Mac 开发人员库提供了此类脚本的概要(只要该文件的修改时间发生变化,它们的示例就会监视/etc/hostconfig并运行syslog -s -l notice "somebody touched /etc/hostconfig"。)
出于我们的目的,我们注意到每次您登录 VPN 时,目录/Library/Preferences/SystemConfiguration都会被修改。因此,如果您将以下 plist 文件保存在 中/Library/LaunchDaemons/vpn.connectscript.plist,它将监视该目录:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>vpn.connectscript</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/opt/local/bin/vpn_some_script.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
请注意,此 LaunchDaemon 将被调用,而不仅仅是您的 VPN 连接(例如,每次您连接到 wifi 时)。因此,您的脚本/opt/local/bin/vpn_some_script.sh应该检查隧道是否实际连接,并且如果脚本连续运行多次,则脚本不应产生问题。(因此,如果您挂载共享,您可能需要检查它们是否尚未挂载)。
例如,我的脚本/opt/local/bin/vpn_some_script.sh很简单:
#!/bin/bash
# This only changes the routes table if utun0 exists.
# -n checks that `ifconfig utun0` returns something other than "" on STDOUT
# The 2> /dev/null redirects STDERR to null, to silence errors
if [[ -n `ifconfig utun0 2> /dev/null` ]] ; then
route -n add -net 10.0.0.0/8 -interface utun0
# Find the old default gateway
GATEWAY=`route -n get default -ifscope en0 | grep gateway | awk '{ print $2 }'`
# make everything (except blocks described above) go through old default gateway rather than VPN
route -n change default $GATEWAY
fi
Run Code Online (Sandbox Code Playgroud)
如果我连接到 VPN 隧道,它只会通过隧道将路由添加到 10.0.0.0/8(并将默认值更改为转到 192.168.1.1 路由器)。
当您保存 plist 文件时,它将在下次重新启动时自动加载。但是,您也可以手动加载它:
sudo launchctl load -w /Library/LaunchDaemons/vpn.connectscript.plist
Run Code Online (Sandbox Code Playgroud)
请注意,如果您的 bash 脚本需要 root 权限(例如,更改路由表的我的脚本),您需要将其存储在/Library/LaunchDaemons/(或/System/Library/LaunchDaemons) 中。如果您的脚本应以普通用户身份运行,则应将其存储在 ~/Library/LaunchAgents/.
如果您的脚本以 root 身份运行并且不想遭受权限提升攻击,那么被调用的 bash 脚本应该在一个/opt/local/bin/只能由 root 修改的目录中。如果您存储在说~/bin一个普通用户可以更改脚本(或重命名文件/目录并用他们编写的文件替换它)并获得对您系统的完全访问权限。
/etc/ppp/ip-up 对于预连接脚本,以及 /etc/ppp/ip-down连接后脚本。不要忘记添加执行权限位。我使用它们在 PPTP 和 L2TP VPN 连接之前和之后更改和恢复路由表。
在编写了两个名为ip-upand 的脚本ip-down后,在 Terminal.app 中运行以下命令:
chmod +x ip-up ip-down
sudo cp ip-up ip-down /etc/ppp
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18510 次 |
| 最近记录: |