Skip to content

Instantly share code, notes, and snippets.

@twksos
Last active July 31, 2023 20:50
Show Gist options
  • Save twksos/44b45abf5263635776ec to your computer and use it in GitHub Desktop.
Save twksos/44b45abf5263635776ec to your computer and use it in GitHub Desktop.
Cisco VPN connection auto connect AppleScript
-- Please set your vpn connection name and password here
set VPNName to "VPN name"
set VPNpassword to "VPN password"
tell application "System Events"
tell current location of network preferences
set VPNService to service VPNName
end tell
set isConnected to connected of current configuration of VPNService
if isConnected then
disconnect VPNService
else
connect VPNService
set startTime to current date
repeat until exists (static text 1 of window 1 of application process "UserNotificationCenter" whose name is "Enter your user authentication")
if (current date) - startTime is greater than 8 then
error "Could not connect, the connection dialog did not pop up"
exit repeat
end if
delay 0.2
end repeat
set theProcess to application process "UserNotificationCenter"
set theWindow to window 1 of theProcess
set theDescription to static text 2 of theWindow
if name of theDescription is "Enter your user authentication" then
set theControls to get entire contents of theWindow
set thePassword to text field 1 of theWindow whose description is "secure text field"
set value of thePassword to VPNpassword
set theOKButton to button 1 of theWindow whose title is "OK"
click theOKButton
repeat until exists (button 1 of window 1 of application process "UserNotificationCenter" whose title is "Disconnect")
if exists (button 1 of window 1 of application process "UserNotificationCenter" whose title is "Cancel") then
set theCancelButton to button 1 of theWindow whose title is "Cancel"
click theCancelButton
error "please check password in the script."
exit repeat
end if
delay 0.2
end repeat
set theOKButton to button 1 of theWindow whose title is "OK"
click theOKButton
end if
end if
end tell
@cyberguy42
Copy link

Works perfectly on OSC 10.8.5! Thank you for your contribution

@mgguinne
Copy link

Hi,

Great job and it just works, also it's exactly what I was looking for after my work forced the cisco VPN to not allow the saving of passwords (I know its a good thing but hey it can be a pain to keep putting my password in every time). I do have one issue though, I am running on 10.10.5 and the script never stops it just keeps running even though it's successfully connected?

Also do you think there is anyway the script could get my password via the my keychain, would prefer not have my password in clear text :)

Thanks heaps.

@erikhansen
Copy link

While this looks like a nifty script, I don't like the idea of storing the password in plain text. It would be great if someone could add the ability for the password to come from Keychain item. For now, I'm using this scutil solution: http://superuser.com/a/934577

Also, I'm getting this error on OS X 10.11.3:

error "System Events got an error: Can’t get current configuration of service id \"3518D503-0B19-4392-8683-7592F307FB98\" of network preferences." number -1728 from current configuration of service id "3518D503-0B19-4392-8683-7592F307FB98" of network preferences

It may be able to be resolved using the workaround commented on here: https://gist.github.com/adgedenkers/3874427#gistcomment-1656529

@shivam13juna
Copy link

Hello guys, I finally found a working solution. I'm using Anyconnect VPN secure mobility 4.8

We can use this to connect to Anyconnect with terminal

To connect:

printf 'USERNAME\nPASSWORD\ny' | /opt/cisco/anyconnect/bin/vpn -s connect HOST

Replace USERNAME, PASSWORD, and HOST. The \ny at the end is to accept the login banner - this is specific to my host.

Note the single quotes ' instead of double quotes " - this is because double quotes tell Bash to interpret certain characters within strings, such as exclamation marks, as Bash history commands. Double quotes will make this command fail with an "event not found" error if the password contains an exclamation mark. Single-quoted strings pass exclamation marks along without interpreting them.

To disconnect:

/opt/cisco/anyconnect/bin/vpn disconnect

I've made an alias to bash_profile to those commands

@xEgorka
Copy link

xEgorka commented Oct 13, 2020

Thanks a lot, your solution works.

@ferreirafabio
Copy link

best combine this with https://github.com/plyint/encpass.sh to not store unencrypted passwords in scripts

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