Skip to content

Instantly share code, notes, and snippets.

@communikein
Forked from halocaridina/anyconnect.scpt
Last active March 4, 2021 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save communikein/62b253cea6cb31344cd4e88066ea4289 to your computer and use it in GitHub Desktop.
Save communikein/62b253cea6cb31344cd4e88066ea4289 to your computer and use it in GitHub Desktop.
-- 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. 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.
-- 3. Open Security & Privacy System Preferences, go to Privacy, Accessibility.
-- 4. Enable the above .app so it can access Accessibility
-- 5. Copy and paste a nice icon on the generic Applescript icon (I used a copy of the default AnyConnect one)
-- 6. Add the new .app to /Users/[yourshortname]/Applications with a shortcut to your Dock
-- 7. Enjoy the fast connection with no need to enter password and increased security of not having a sensitive password stored as plain text
-- 8. Run script again to close connection
-- AnyConnect now refered to as targetApp
set targetApp to "Cisco AnyConnect Secure Mobility Client"
-- 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. Do nothing.
repeat until (window 1 of process targetApp exists)
delay 0.1
end repeat
--You may need to uncomment below if your OpenConnect implementation requires a keystroke to accept the default VPN
--tell process targetApp
-- keystroke return
--end tell
-- Wait for second window to open and then automatically enter password extracted from your Keychain
repeat until (window "Cisco AnyConnect Login" of process targetApp exists)
delay 0.1
end repeat
-- Wait for the "Cisco AnyConnect Login" window to completely load
repeat until (button "Log in" of group 2 of group 3 of UI element of scroll area 1 of group 1 of group 1 of window "Cisco AnyConnect Login" of process "Cisco AnyConnect Secure Mobility Client" exists)
delay 0.1
end repeat
-- 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
tell process targetApp
set inString to "ENTER_YOUR_KEYCHAIN_PASSWORD_NAME" -- NOT the password itself
set username to "ENTER_YOUR_USERNAME"
set PSWD to do shell script "/usr/bin/security find-generic-password -wl " & quoted form of inString
keystroke username as text
keystroke tab
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.1
end repeat
tell process targetApp
keystroke return
end tell
end tell
end if
@communikein
Copy link
Author

Instead of waiting for 4 seconds before entering password and username in the login prompt, the script now waits until the prompt page is actually loaded, and enters the credentials as soon as it is possible.
This way the script will not give an error if the current internet connection speed is slow (more than 4 seconds to load the login prompt) and if the connection is fast, will result in a quicker login (down to 0.1 seconds instead of 4 seconds)

@communikein
Copy link
Author

The script is targeted at those users that will be presented with the following login prompt after selecting the VPN connection
Screenshot 2021-01-03 at 12 29 07

@communikein
Copy link
Author

If you are a Cisco employee, you can simply update line 51 to:
set inString to "blizzard"
And finally use your CEC ID for the username

@andreer
Copy link

andreer commented Feb 11, 2021

Awesome improvement. Thank you!

@ricarsan
Copy link

ricarsan commented Mar 4, 2021

The script is targeted at those users that will be presented with the following login prompt after selecting the VPN connection
Screenshot 2021-01-03 at 12 29 07

Hello, I am having an issue with the following routine:
repeat until (button "Log in" of group 2 of group 3 of UI element of scroll area 1 of group 1 of group 1 of window "Cisco AnyConnect Login" of process "Cisco AnyConnect Secure Mobility Client" exists)

It is not detecting the Log in button on the window pop up. I've tried many different options playing with group numbers, etc... no luck. No much experience in Applescripting... what might be wrong? thank you!!

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