How to automate your VPN connection. (Mac only)

Srijan Gulati
3 min readJan 9, 2022

Who is this article for?

Does your work require you to connect to a VPN? Are you still entering your username and password every single time to connect to your VPN? Do you wish to automate this process? Is so, this article is for you.

Here is how I connect to VPN -

As you can see, it took me hardly 10 seconds to connect to my VPN, that too with 2 factor authentication. Yes, I did get a notification on my phone in those 10 seconds, I approved it and got connected to VPN. All that even without touching my mouse or having to enter my username or password. The process to disconnect from VPN is even faster.

How to automate your VPN -

Step 1 — Write a script

  • First, create a text file to store your credentials
Employee
${usename}
${password}

Replace ${username} and ${password} with your username and password to connec to your VPN.

  • Write a script which runs VPN connection command -
#!/bin/bashVPN_SERVER="${your vpn endpoint}"echo "Connecting to VPN.."
/opt/cisco/anyconnect/bin/vpn -s < ${path to credentials file} connect ${VPN_SERVER}
exit 0

Replace ${your vpn endpoint} with your VPN endpoint and ${path to credentials file} with the absolute path of your text file which contains your credentials.

Now save this script as vpn_connect.command

  • Write a script to disconnect from VPN
/opt/cisco/anyconnect/bin/vpn -s disconnect ${your vpn endpoint}
exit 0

Save this file as vpn_disconnect.command

Voila, now you can just run these scripts to connect or disconnect from your VPN connection.

But that still would need you to click on these files, we can further automate this process.

Step 2 — Download Alfred

Alfred is a great tool for macs and works like spotlight search but with a few more features. Once downloaded, you will be able to click option + space to start searching in Alfred.

Once you download Alfred, go to its preferences → features → extras and make sure ```Folders and text files``` are selected.

Now you’ll be able to search for you .command files to run the script without even touching your mouse.

option + space → Opens Alfred

type vpn_connect (searches for the script file you write)

Click Enter

Failed to run the script? WHAT? WHY?

Step 3 — chmod

Well, the script will still not run vai Alfred needs permission.

All you need to do is run these commands on terminal

chmod 777 <relative path to VPN connection script>

chmod 777 <relative path to VPN disconnection script>

Now repeat the process using Alfred, and voila, you’ll connect to VPN without having to touch your mouse or entering your username and password.

Final step — Being fancy

As you can see in the recording, my terminal window closed after my VPN got connected. Here is how you can do that as well -

Go to terminal preferences -

Go to Profiles and select “Close if a shell exited cleanly”

And now post your VPN connection, your terminal will close automatically.

Now you can forget about opening any VPN connection tools to connect to the VPN and just use this.

Note -

If any of your VPN connection tools are running, your command will most likely fail, so make sure you quit out from any of your existing tools first.

References

Alfred - <https://www.alfredapp.com/> 
Alfred a great app for mac for productivity (Not sponsored)

--

--

Srijan Gulati

Hello, world! I’m a JavaScript developer who likes writing articles in my free time.