Ket*_*ema 55 vpn command-line osx-lion macos
我的 mac 上有两个 VPN 配置,当我 ssh 进入我的机器时,我希望能够从控制台启动它们。
我找到了networksetup允许我配置连接的命令,但据我所知,实际上并没有启动。
使用狮子。
enc*_*ded 61
至少从 Lion 1 开始,您还可以使用 scutil 命令。
例如,如果我有一个名为“Foo”的 VPN 服务,我可以通过以下方式连接:
$ scutil --nc start Foo
Run Code Online (Sandbox Code Playgroud)
我可以选择使用相同名称的标志指定用户、密码和机密:
$ scutil --nc start Foo --user bar --password baz --secret quux
Run Code Online (Sandbox Code Playgroud)
可以通过以下方式断开服务:
$ scutil --nc stop Foo
Run Code Online (Sandbox Code Playgroud)
有关更详细的帮助,您可以查看手册页,或运行:
$ scutil --nc help
Run Code Online (Sandbox Code Playgroud)
添加快速脚本以轮询直到建立连接(响应 Eric B.
#!/bin/bash
# Call with <script> "<VPN Connection Name>"
set -e
#set -x
vpn="$1"
function isnt_connected () {
scutil --nc status "$vpn" | sed -n 1p | grep -qv Connected
}
function poll_until_connected () {
let loops=0 || true
let max_loops=200 # 200 * 0.1 is 20 seconds. Bash doesn't support floats
while isnt_connected "$vpn"; do
sleep 0.1 # can't use a variable here, bash doesn't have floats
let loops=$loops+1
[ $loops -gt $max_loops ] && break
done
[ $loops -le $max_loops ]
}
scutil --nc start "$vpn"
if poll_until_connected "$vpn"; then
echo "Connected to $vpn!"
exit 0
else
echo "I'm too impatient!"
scutil --nc stop "$vpn"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
脚注:
slh*_*hck 55
对于较新的 macOS 版本,可以使用一个非常简单的命令,如下面的答案所示,例如这个(给它一个 +1!)。
所有你需要的是:
networksetup -connectpppoeservice "UniVPN"
Run Code Online (Sandbox Code Playgroud)
唯一的问题是您无法使用此命令断开连接。
您还可以使用 AppleScript 连接到您选择的 VPN 服务。我们将使用 shell 函数,一旦它们被加载,就可以从命令行获得它们。
将以下功能添加到您的~/.bash_profile或~/.profile(无论您使用什么)。
您只需要更改 VPN 连接本身的名称,因为它出现在“网络”首选项下。我在这里使用了我的大学 VPN。

如果你想为不同的函数做这件事,你也可以更改函数的名称。可能可以使用参数来缩短它,但它以这种方式工作得很好。我在 Snow Leopard 上进行了测试(但 Leopard 和 Lion 也应该可以使用)。
添加函数后,重新加载终端并分别使用vpn-connect和调用它们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
repeat while (current configuration of VPN is not connected)
delay 1
end repeat
end tell
end tell
EOF
}
function vpn-disconnect {
/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 disconnect VPN
end tell
end tell
return
EOF
}
Run Code Online (Sandbox Code Playgroud)
小智 29
尚未在 Lion 下对此进行测试,但我在 Mountain Lion 下使用以下命令没有任何问题:
networksetup -connectpppoeservice UniVPN
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73012 次 |
| 最近记录: |