Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active March 11, 2022 16:24
Show Gist options
  • Select an option

  • Save slowkow/f0405ccbcadecf8d0e77 to your computer and use it in GitHub Desktop.

Select an option

Save slowkow/f0405ccbcadecf8d0e77 to your computer and use it in GitHub Desktop.
Connect to VPN and type your password automatically
on ButtonClick(theObjectID)
tell application "System Events"
repeat until exists theObjectID
delay 0.5
end repeat
click theObjectID
end tell
end ButtonClick
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 "System Events"
set theID to (unix id of processes whose name is targetApp)
try
do shell script "kill -9 " & theID
end try
end tell
else
tell application targetApp
activate
end tell
tell application "System Events"
tell process targetApp
-- wait until the first window appears
repeat until exists window 1
delay 1
end repeat
-- press OK when it appears in the first window
if (button "OK" exists) then
my ButtonClick(button "OK" of window 1)
end if
-- press Connect in the second window
my ButtonClick(button "Connect" of window 2)
-- wait for the user/pass login form to appear
repeat until exists window "Cisco AnyConnect Login"
delay 1
end repeat
-- UI elements
delay 2
set uiElems to entire contents
-- get the password from secure storage in Keychain
set inString to "Partners_AnyConnect_VPN"
set PSWD to do shell script "/usr/bin/security find-generic-password -wl " & quoted form of inString
-- type the password in the form and submit
keystroke PSWD as text
keystroke return
end tell
end tell
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment