Skip to content

Instantly share code, notes, and snippets.

@andrewh
Last active April 12, 2024 08:48
Show Gist options
  • Save andrewh/7135352 to your computer and use it in GitHub Desktop.
Save andrewh/7135352 to your computer and use it in GitHub Desktop.
Applescript to automate the Cisco AnyConnect SSL VPN client on OS X Mavericks
-- 1. Place in ~/Library/Scripts and enable the Applescript menu via the Applescript Editor
-- 2. Substitute "vpn.example.com" and "redacted" for your VPN server and password
-- 3. Open Security & Privacy System Preferences, go to Privacy, Accessibility
-- 4. Enable Applescript Editor and System UI Server
-- 5. Trigger script from the menu
-- 6. Enjoy being connected
tell application "Cisco AnyConnect Secure Mobility Client"
activate
end tell
repeat until application "Cisco AnyConnect Secure Mobility Client" is running
delay 1
end repeat
tell application "System Events"
repeat until (window 1 of process "Cisco AnyConnect Secure Mobility Client" exists)
delay 1
end repeat
tell process "Cisco AnyConnect Secure Mobility Client"
keystroke ("vpn.example.com" as string)
keystroke return
end tell
repeat until (window 2 of process "Cisco AnyConnect Secure Mobility Client" exists)
delay 1
end repeat
tell process "Cisco AnyConnect Secure Mobility Client"
keystroke ("redacted" as string)
keystroke return
end tell
end tell
@nperez0111
Copy link

I use zsh so what @shivam13juna was close but not what I needed: I used

export CISCO_USERNAME="..."
export CISCO_PASSWORD="..."

vpn() {
  # Any connect can't be running (and doesn't have to be)
  kill $(pidof anyconnect) >/dev/null 2>&1
  echo "$CISCO_USERNAME\n$CISCO_PASSWORD\ny" | /opt/cisco/anyconnect/bin/vpn -s connect vpn.host.com
}

Do note that anyconnect can't be running if this is

@augustplaninsek
Copy link

Inspired by this thread I made shell script to toggle connection and BetterTouchTool Widget to show connection status and toggle connection on click.
Link to repo: https://github.com/augustplaninsek/vpn-cisco-anyconnect

@sarnobat
Copy link

I couldn't get the banner part to work, but this works at least:

	tell application "System Events"
		click button "Accept" of window "Cisco AnyConnect - Banner" of application process "Cisco AnyConnect Secure Mobility Client"
	end tell

@dipplabs
Copy link

dipplabs commented Sep 30, 2022

Since i updated Cisco Anyconnect two days ago, it seemed like a major update, this has stopped working. It is not called "Cisco AnyConnect Secure Mobility Client" any longer, it shows it as "Cisco Secure Client " now and the icon changed too. But this was working for me up until my company made me update this software. I think it was mandatory by Cisco. See screenshot of the new VPN icon/dialog box.

I tried renaming wherever "Cisco AnyConnect Secure Mobility Client" was set to "Cisco Secure Client " but in the Script Editor it says UI Disabled when i hit the play button...

Can someone help me out here?
This worked great because i have the password enetered automatically, and then i just click the Yubi key for the 2nd one... now i have to find the first password everyday now, and i had this working for 3 years now... So i hope this can be fixed easily.

Here are the screenshots of the new look of the app and the new app name, which is in version 5.0.00556 of their VPN app:

Screen Shot 2022-09-28 at 4 26 50 PM

Screen Shot 2022-09-30 at 5 01 39 AM

Thanks in advance!

@brozikcz
Copy link

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