Writing an Alfred Workflow About VPN Connection

Part of the team’s services is deployed on the internal network. Usually, when we are remotely processing some work, we need to connect to the VPN.
Although Cisco’s AnyConnect client tool can be used, it cannot save passwords for automatic login, so the OpenConnect becomes a good choice, which is an open-source command-line tool.
This article is based on the macOS platform, using Alfred’s Workflow feature to improve the portability of OpenConnect connections.

Result

Open Alfred via a shortcut key, enter specific keywords and enter the user password after selecting the function.

Process

What is Alfred

Alfred is an award-winning app for macOS which boosts your efficiency with hotkeys, keywords, text expansion and more. Search your Mac and the web, and be more productive with custom actions to control your Mac.

The free version has similar functions to Spotlight. It can be used as a quick application launcher, calculator, dictionary and other all-in-one applications. After purchasing Powerpack, it unlocks Workflows as its special features. It is recommended to buy the Mega version in the official site.

What is Workflow

Workflow is literally meant workflow, also knows as something like a shortcut. I think it has a certain degree of similarity with Tasker and IFTTT. The following figure is the Workflow I am using.

Scene

First, make sure that OpenConnect is installed on the macOS system.

1
brew install openconnect

There are already Shell scripts that can connect and disconnect the VPN, so the main goal of this article is to transform existing scripts below.

Connect Script:

Disconnect Script:

Thinking

Goal

  1. Make the best use of existing scripts without rebuilding the wheels.
  2. The steps should be as short as possible.

Step

  1. Create a new Workflow, add each component, and connect as shown in this figure.

  2. Transform existing script to NSAppleScript section.

  3. Improve various types of information and export scripts for distribution within the team.

Problem

Question 1:
The script needs high enough privileges, but we can’t escalate privileges directly through sudo in Alfred.

Solution:
Use NSAppleScript to escalate privileges, as shown in the following figure. We can also achieve similar goals by modifying user group permissions, but the latter is more invasive to the system.

Question 2:
Interactive code does not work。

Solution:
Transformed the script to change the interactive part of inputting passwords to pass parameters.

Final

Connect script

Disconnect script

More

After improving Workflow instructions, copyright notices, version descriptions, we can now export it to team members.