Updated: Oct 25, 2019
As engineers, there are tasks we perform manually on a daily basis. Some more than few times a day. As doing them once is quick, we don’t really think of alternate ways. In few days you realise the boredom and say - "this is too painful. I wish there was an easier and faster way".
One such scenario I came across recently is connecting to VPN with two factor authentication. Its a tedious multi-step process of launching the VPN application on your laptop, locating your phone, launching the authenticator app on the phone to generate an auth token, entering the token into the VPN app in a specific duration to avoid timeout and finally you get connected.
I am going to talk about how to write an AppleScript using a command line authenticator utility (2fa) to automate this and get you connected in just a few seconds. As you can guess, this is only applicable for mac users. The VPN client in my case is Cisco AnyConnect Secure Mobility Client.
set vpnapp to "Cisco AnyConnect Secure Mobility Client"
-- Open the VPN application
tell application vpnapp
-- Wait till the time the application loads
repeat until application vpnapp is running
tell application "System Events"
-- Wait till the time the dialog loads
repeat until (window 2 of process vpnapp exists)
-- Click the connect button
click button "Connect" of window 2 of process vpnapp
-- Wait for the next dialog box to appear
repeat until (window 3 of process vpnapp exists)
-- Generate the token via command line tool.
-- Replace with name of your service
set code to do shell script "/usr/local/bin/2fa <service>"
-- Enter the token in text box
-- Wait for the confirmation banner
repeat until (window "Cisco AnyConnect - Banner" of process vpnapp exists)
-- Hit accept and you are all set
Save the above script in Script Editor and export it as an application. All you have to then do is run the exported application to connect.
Below is a visual demo of how it works (which looks really cool 😃btw)
The above use case is just one example and the possibilities are endless. The key idea here is to identify things that are repetitive in nature and can be easily automated with readily available tools.
As they say - working smarter not harder is the secret for getting things done!