How to login Cisco VPN automatically on macOS High Sierra

There is no way to save password when logging into Cisco IPSec VPN on macOS.

No save password for security reason?

The best solution to me is writing an AppleScript in Automator or run it from command line to automate the login.

AppleScript in Automator

  1. Open Apple’s Automator,
    Automator

  2. Choose New Document
    New Document

  3. Select Service
    The service created has receives selected text by default. It means you need to select text or having focus in a text editor to enable the service.
    Serivce

  4. Try to search for action Run AppleScript. Then, drag the action into the right-hand-side.
    Search for action "Run AppleScript", and then drag it to the right-hand-side

  5. Paste below code into the editor,

1
on run {input, parameters}
2
3
  set vpn_name to "'your VPN name'"
4
  set user_name to "your username"
5
  set passwd to "your password"
6
7
	tell application "System Events"
8
		set rc to do shell script "scutil --nc status " & vpn_name
9
		if rc starts with "Disconnected" then
10
			do shell script "scutil --nc start " & vpn_name & " --user " & user_name
11
			delay 3
12
			keystroke passwd
13
			keystroke return
14
		end if
15
	end tell
16
	return input
17
end run

Editor with the script

  1. Update vpn_name, username and passwd in the script.

You can take reference from below screenshot for the vpn_name, which is VPN (Cisco IPSec). The script uses scutil --nc status to check the VPN connection status, and scutil --nc start to start the VPN connection. Usually the VPN login dialog shows within 3 seconds. If your laptop is slow please update value in delay 3. Try to run the script with the play button to see how it is automated.

Your VPN name

  1. Save the script with a name such as VPN Login.
    In the System Preferences->Keyboard->Shortcuts, you can see the automation script. Assign a shortcut key. The script is assigned to text service by default during the service creation. You will need to select text or having focus in a text editor to enable the keyboard shortcut.
    Assign hotkey for your VPN

  2. Try to select text from an text editor such as Atom. With the proper shortcut key setup you should have the VPN login automated. It should works with Sierra or below but I haven’t tested it. Please let me know your result in older macOS.

Success!

AppleScript from command line

  1. Open Apple’s Script Editor,
    Automator

  2. Choose New Document

  3. Paste below code into the editor. Please refers to AppleScript in Automator

1
set vpn_name to "'your VPN name'"
2
set user_name to "your username"
3
set passwd to "your password"
4
5
tell application "System Events"
6
	set rc to do shell script "scutil --nc status " & vpn_name
7
	if rc starts with "Disconnected" then
8
		do shell script "scutil --nc start " & vpn_name & " --user " & user_name
9
		delay 3
10
		keystroke passwd
11
		keystroke return
12
	end if
13
end tell
  1. Save the script. You can run the script with osascript [programfile] from Terminal.

Have fun!

Share