我正在尝试通过 Mac 上的命令行登录我的 VPN(Cisco IPSec)
scutil --nc start "myVPN (Cisco IPSec)" --password "mypassword"
运行该脚本时,我只会看到通常的弹出窗口,而无需填写密码(尽管用户名是自动填充的(就像单击 vpn 登录时一样)。是否可以使用此方法填写密码,或者是否有任何其他方式通过命令行连接到 VPN?
答案1
我编写了一个 shell 脚本,用于从钥匙串中获取密码,然后将其粘贴到弹出窗口中。要使此功能正常工作,您必须创建一个包含密码的钥匙串项。
# VPN.sh
# change these variables for your setup
keychainItem=accountWithPassword # this name has to match "Account" for the entry you make in keychain
VPNName="VPN (Cisco IPSec)" # match the name of the VPN service to run
get_pw () {
security 2>&1 >/dev/null find-generic-password -ga $keychainItem \
|ruby -e 'print $1 if STDIN.gets =~ /^password: "(.*)"$/'
}
echo "fetching VPN credentials from keychain account \"$keychainItem\""
echo "Using VPN service: $VPNName"
scutil --nc start "$VPNName"
sleep 2.7
osascript -e "tell application \"System Events\" to keystroke \"$(get_pw)\""
osascript -e "tell application \"System Events\" to keystroke return"
sleep 2
exit
答案2
这可能是检查 VPN 是否已连接的好方法......
\#!/bin/sh
keychainItem="MY KEYCHAIN ACCOUNT"# this name has to match "Account" for the entry you make in keychain
VPNName="MY VPN" # match the name of the VPN service to run
function isnt_connected () {
scutil --nc status "$VPNName" | sed -n 1p | grep -qv Connected
}
get_pw () {
security 2>&1 >/dev/null find-generic-password -ga $keychainItem \
|ruby -e 'print $1 if STDIN.gets =~ /^password: "(.*)"$/'
}
echo "fetching VPN credentials from keychain account \"$keychainItem\""
echo "Using VPN service: $VPNName"
if isnt_connected $VPNName; then
echo "Connecting to VPN..."
scutil --nc start "$VPNName"
sleep 0.5
osascript -e "tell application \"System Events\" to keystroke \"$(get_pw)\""
osascript -e "tell application \"System Events\" to keystroke return"
sleep 2
osascript -e "tell application \"System Events\" to keystroke return"
else
echo "Already Connected to VPN..."
fi