Skip to content

Instantly share code, notes, and snippets.

@yrammos
Last active April 8, 2022 14:55
Show Gist options
  • Save yrammos/abe5cca8c4a744a380cd7e553e7da8b8 to your computer and use it in GitHub Desktop.
Save yrammos/abe5cca8c4a744a380cd7e553e7da8b8 to your computer and use it in GitHub Desktop.
Automatic login to Cisco AnyConnect VPN using password from the macOS Keychain.
set vpn to do shell script "/opt/cisco/anyconnect/bin/vpn status | grep 'Connected' && echo 'connected' || echo 'disconnected'"
if (vpn is not "disconnected") then
do shell script "/opt/cisco/anyconnect/bin/vpn disconnect"
display dialog "VPN disconnected!" with icon caution buttons {"OK"} default button "OK"
tell application "Cisco AnyConnect Secure Mobility Client" to quit
else if (vpn is "disconnected") then
set pwd to do shell script "/usr/bin/security find-internet-password -wl your.keychain.item.name"
set output to do shell script "echo '
" & pwd & "'| /opt/cisco/anyconnect/bin/vpn -s connect your.vpn.server"
-- Do not indent the line above!
if (output contains "state: Connected") then
display dialog "VPN connected!" with icon 1 buttons {"OK"} default button "OK"
tell application "Cisco AnyConnect Secure Mobility Client" to run
else
do shell script "ps -ef | grep 'cisco' | awk '{print $2};' | xargs kill -9 > /dev/null 2>&1"
say "Error connecting to VPN. Running instances have been killed."
display dialog "Error connecting to VPN." with icon stop buttons {"OK"} default button "OK"
end if
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment