Skip to content

Instantly share code, notes, and snippets.

@e11s
Last active March 6, 2020 10:12
Show Gist options
  • Save e11s/3eb5dfe41d14ad5e51c40b2caddf2424 to your computer and use it in GitHub Desktop.
Save e11s/3eb5dfe41d14ad5e51c40b2caddf2424 to your computer and use it in GitHub Desktop.
Cisco AnyConnect VPN GUI client connection automation (with Keychain)
-- 1. Create a new generic password entry in Keychain Access called "WHATEVER_AnyConnect_VPN" (the name in Keychain access must match that in line 39 below) with your password for the Cisco AnyConnect VPN server.
-- 2. Modify values of vpnUrl and keychainKey variables
-- 3. Open this script in Script Editor (both this and the above are in the Applications->Utilities folder) and "Save as.." an Application (.app) with desired name.
-- 4. Open Security & Privacy System Preferences, go to Privacy, Accessibility.
-- 5. Enable the above .app so it can access Accessibility
-- 6. Copy and paste a nice icon on the generic Applescript icon (I used a copy of the default AnyConnect one)
-- 7. Add the new .app to /Users/[yourshortname]/Applications with a shortcut to your Dock
-- 8. Enjoy the fast connection with no need to enter password and increased security of not having a sensitive password stored as plain text
-- 9. Run script again to close connection
-- AnyConnect now refered to as targetApp
set targetApp to "Cisco AnyConnect Secure Mobility Client"
set vpnUrl to "some.vpnserver.com"
set keychainKey to "WHATEVER_AnyConnect_VPN"
-- Determine if AnyConnect is currently running
tell application "System Events"
set processExists to exists process targetApp
end tell
-- Close connection if running; else start connection and fill in password
if processExists is true then
tell application targetApp
quit
end tell
else
tell application targetApp
activate
end tell
tell application "System Events"
-- Wait for first window to open
repeat until (window 1 of process targetApp exists)
delay 0.2
end repeat
-- Enter vpn server
tell process targetApp
keystroke (vpnUrl as string)
keystroke return
end tell
-- Wait for second window to open and then automatically enter password extracted from your Keychain
set windowName to "Cisco AnyConnect | " & vpnUrl
repeat until (window windowName of process targetApp) exists
delay 0.2
end repeat
tell process targetApp
-- This is where the the password in the Keychain is accessed for use as input rather than being hardcoded as plain text in other versions of this script out in the wild
set PSWD to do shell script "/usr/bin/security find-generic-password -wl " & quoted form of keychainKey
keystroke PSWD as text
keystroke return
end tell
-- Autoclick on "Accept" of AnyConnect Banner window. If you have no welcome banner that needs acceptance, comment out these lines to the first "end tell" below
repeat until (window "Cisco AnyConnect - Banner" of process targetApp exists)
delay 0.2
end repeat
tell process targetApp
keystroke return
end tell
end tell
end if
@meteozond
Copy link

awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment