从命令行使用 anyconnect 连接

Kel*_*ert 45 networking vpn cisco-vpn-client

我正在尝试从 Linux 命令行使用 Cisco anyconnect 3.1 连接到服务器。我可以连接,但我必须一次提交一个参数。我想从将在另一台服务器上运行的脚本进行连接。我可以这样做吗?就像是

vpn connect server_add group_name user_name passwd
Run Code Online (Sandbox Code Playgroud)

Asc*_*ius 53

假设/opt/cisco/anyconnect/bin/vpnagentd正在运行,因为它应该自动运行:

连接

printf 'USERNAME\nPASSWORD\ny' | /opt/cisco/anyconnect/bin/vpn -s connect HOST
Run Code Online (Sandbox Code Playgroud)

替换USERNAMEPASSWORDHOST。将\ny在年底是接受登录旗帜-这是针对我的主人。

请注意单引号'而不是双引号"- 这是因为双引号告诉 Bash 将字符串中的某些字符(例如感叹号)解释为 Bash 历史命令。如果密码包含感叹号,双引号将使此命令失败并显示“未找到事件”错误。单引号字符串传递感叹号而不解释它们。

断开连接

/opt/cisco/anyconnect/bin/vpn disconnect
Run Code Online (Sandbox Code Playgroud)

这是使用 AnyConnect v3.1.05160 测试的。

  • 如果您的客户端由于证书验证错误“证书来自不受信任的来源”而无法连接,而您仍然想连接,则在上述方法中传递一个“y”参数,以便连接命令变为:“printf”y \n用户名\n密码\ny" | /opt/cisco/anyconnect/bin/vpn -s connect HOST`。请注意,仅在您绝对信任您的连接的情况下才这样做;否则可能会有一个中间人坐在那里窥探你。 (5认同)
  • 效果很好(虽然我的版本需要一个 `GROUPNAME\nUSERNAME\nPASSWORDy`。如果你想将你的密码与命令(可能是 shell 脚本或点文件键绑定)分开,你可以这样做:`cat ~/ .anyconnect_credentials | /opt/cisco/anyconnect/bin/vpn -s 连接主机` (2认同)

小智 11

我在尝试从 Mac OS X 终端使用 Cisco AnyConnect 时遇到了同样的困难。要让 Cisco vpn 命令从标准输入获取其输入,您必须指定 -s 选项,该选项将 Cisco vpn 命令置于交互模式。然后您可以提供您在交互模式下给出的响应。

您需要给出的响应取决于 VPN 服务器管理员如何配置服务器。对我来说,vpn 交互式提示是

Group: 
Username: 
Password: 

Blah, blah, blah, ...
accept? :
Run Code Online (Sandbox Code Playgroud)

所以我运行的命令是

$ /opt/cisco/anyconnect/bin/vpn -s connect vpn.example.com <<"EOF"
0
username
password
y
exit
EOF
Run Code Online (Sandbox Code Playgroud)

(围绕EOF的引号是为了防止以下输入中的命令/参数扩展/替换。)

最后的exit是退出Cisco vpn交互模式。


Dan*_*Dan 6

您可以将连接信息放在单独的文件中,例如

anyconnect.txt:

connect [HOST]
[GROUP or 0 or 1]
[USER]
[PASSWORD]
y
exit
Run Code Online (Sandbox Code Playgroud)

然后执行以下操作:

/opt/cisco/anyconnect/bin/vpn -s < anyconnect.txt
Run Code Online (Sandbox Code Playgroud)


小智 5

我喜欢简化命令行,因此我在名为 gotowork 的 shell 脚本中使用上述方法。如上所述,我需要提供组、我的用户名以及由私人 PIN 码和 RSA SecurID 密码组成的密钥。我不必回答上面的“接受?” 问题。除 RSA 密码外的所有内容都在脚本中,因此命令行是

$ gotowork <RSA passcode>
Run Code Online (Sandbox Code Playgroud)

我必须以 root 身份运行它。假设 PIN 为 1234。脚本要点:

# put the interactive answers into a text file
echo -e "0\nusername\n1234$1\n" > /tmp/answers.txt
# find the path to the anyconnect executables
ciscopath="$(dirname $(find /opt/cisco -depth -name vpnagentd))"
# make sure the anyconnect daemon is running
[ $(pidof vpnagentd) ] || $ciscopath/vpnagentd
# connect
$ciscopath/vpn -s < /tmp/answers.txt connect remote.mycompany.com
Run Code Online (Sandbox Code Playgroud)

使用anyconnect 3.1.05170。在 Debian 6、LinuxMint 17 上测试