Coding Myself a VPN Toggle

At fifty, I am becoming the Linux nerd I feel I should have been at twenty-five.

I had a problem. I wanted an easy way to connect and disconnect from Diamond’s VPN. Yes, I could use the Network Connections interface in Linux Mint, but sometimes it expands while I was navigating to the VPN toggle and I accidentally attempt to connect to another WiFi network. Or, I could have set my VPN to autoconnect, as my WiFi does, except I don’t always need to be connected to the VPN. So, I wanted some sort of simple toggle–if the VPN is on, turn it off; if off, turn it on.

Could it be done with a script? I knew how — or, at least I had the commands in a documentation file I wrote for myself last year — to turn the VPN on or off from the command line. What I needed was a way to test the VPN’s status, then execute the right command based on that status.

First, with thanks to the Googles, I figured out how to determine if the VPN was active from the command line with the ifconfig command. Then I wrote an if statement off that command, plugged in the nmcli commands that activate/deactivate the VPN, and tested it.

It worked! On! Off! On! Off!

One little thing annoyed me. Activating the VPN popped up a notification that the VPN was active. Deactivating the VPN did not have a similar result. I discovered the notify-send command and created a deactivation notification.

I created a desktop file so I could call the script from my taskbar. I felt quite good. I had a problem, and I had solved it.

This morning, I decided I wanted to be able to activate and deactivate the VPN from my notifications instead. It makes sense; that’s where I look for my network status. So I would need a Cinnamon Applet.

I didn’t need anything fancy, just an icon I could click on and it executes the script. The Linux Mint website has some basic documentation for creating an applet, and I started from there. I took their samples, made a few edits, and gave it a try.

It didn’t work.

The problem was, I didn’t know how to execute a shell command from a javascript file. Google again was my friend, and I found a blog post that had exactly the information I needed. I needed to add this const, I needed to use that js command. I made my edits, restarted Cinnamon, and…

It worked!

I have been careful not to turn on and turn off the VPN too rapidly, lest I lock myself out of the network.

The problem I had — a simple toggle to control my connection status with the work VPN — was solved.

No wonder I am “the problem project person” at work. See the problem, solve the problem.

Published by Allyn Gibson

A writer, editor, journalist, sometimes coder, occasional historian, and all-around scholar, Allyn Gibson is the writer for Diamond Comic Distributors' monthly PREVIEWS catalog, used by comic book shops and throughout the comics industry, and the editor for its monthly order forms. In his over ten years in the industry, Allyn has interviewed comics creators and pop culture celebrities, covered conventions, analyzed industry revenue trends, and written copy for comics, toys, and other pop culture merchandise. Allyn is also known for his short fiction (including the Star Trek story "Make-Believe,"the Doctor Who short story "The Spindle of Necessity," and the ReDeus story "The Ginger Kid"). Allyn has been blogging regularly with WordPress since 2004.

Leave a Reply

Your email address will not be published. Required fields are marked *