Auto-login for Cisco Anyconnect on Mac

1 Introduction

This blog post explains how to write an AppleScript to automate the log-in procedure for Cisco Anyconnect on a Mac. The post provides a two-step guide on:

  1. How to write the AppleScript to automate the procedure?
  2. How to make the script an actionable program and access it easily like an app?

1.1 Why is it needed?

Here is an example of why this is useful:

If you want to submit jobs from home to the high-performance computer at school, you need to log in to the school VPN through Cisco Anyconnect every time. Sometimes, you need to log in every day.

Usually, connecting to the school VPN address requires a few steps:

  1. Open Anyconnect.
  2. Press Connect to your VPN address.
  3. In the pop-up window, type your Username and Password.
  4. Press Log in.
  5. Wait a while, a push will appear on your phone, confirm it. Then connection completed.

Since Cisco Anyconnect doesn't remember your ID, password, or your computer every time, you need to repeat steps 1-5 every time. To save time (as an extreme lazy person), steps 2-4 can be completely automated, although step 5 is still necessary.

anyconnect1.gif

2 Apple Script for Automation

  1. Open Script Editor

    • path: /System/Applications/Utilities/Script Editor.app
    Untitled
  2. Click New Document to create a new script

    Untitled
  3. Add the script code (or download the script file here)

    • Find the following part and change to your Username and Password
      1
      2
      3
      tell application "System Events" to keystroke "Username"			
      tell application "System Events" to keystroke tab
      tell application "System Events" to keystroke "Password"
  4. Run it by clicking ▶️ to test if it works in your computer

2.1 Apple Script Codes

This code has been modified using the scripts from sgsvnk/cisco-vpn-auto-login.

The initial if loop is designed to launch Cisco AnyConnect on your Mac.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if application "Cisco AnyConnect Secure Mobility Client" is running then	
tell application "Cisco AnyConnect Secure Mobility Client" to activate
tell application "System Events" to keystroke return
else
do shell script "open -a /Applications/Cisco/Cisco\\ AnyConnect\\ Secure\\ Mobility\\ Client.app"
end if

set isConnectionEstablished to false
repeat while isConnectionEstablished = false
tell application "System Events"
if exists (window 3 of process "Cisco AnyConnect Secure Mobility Client") then
set isConnectionEstablished to true
delay 2

tell application "System Events" to keystroke "Username"
tell application "System Events" to keystroke tab
tell application "System Events" to keystroke "Password"
tell application "System Events" to key code 76

set isAuthenticated to false
repeat while isAuthenticated = false
delay 0.2

tell application "System Events"
if not (exists (window 3 of process "Cisco AnyConnect Secure Mobility Client")) then
delay 5
set isAuthenticated to true
tell application "System Events" to keystroke return
end if
end tell
end repeat
end if
end tell
end repeat

Here are the modifications I made: 1. Added a time delay after connecting to the VPN address because I noticed that it takes a while for the login window to appear. 2. Added steps to automatically type in your username and password and then press enter.

3 Turn this script to an application

Here is a apple support page about Save a script as an app in Script Editor on Mac

  1. Export this script Untitled

  2. Choose File Format as Application and give a name to this application Untitled

  3. Now you can find it in your application and call it out easily Untitled anyconnect2.gif

4 Reference

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/CreateaScript.html

https://support.apple.com/guide/script-editor/save-a-script-as-an-app-scpedt1072/mac

https://gist.github.com/andrewh/7135352

https://github.com/sgsvnk/cisco-vpn-auto-login