On my home network, my laptop (a MacBook Pro) has a static IP address, and is accessible over VNC (screen sharing). This means that I can enjoy peak laziness – instead of getting off the couch to see if something has completed, I can just pull it up on my phone. But what about when I travel and I’m not on my home network? How can I be lazy from across campus, or a friend’s living room when my laptop is upstairs? A simple little script that connects my laptop to my home VPN on demand.
Corporate networks, as well as the network of a friend or family member, typically aren’t under our control. I can’t ask my friends to open ports on their router so I can connect to my machine when it’s on their network just as a matter of politeness. And my corporate network really won’t like me asking for a static IP and a network exception just to VNC into my machine.
In theory, I could sign up for a service that enables Remote Desktop through a central service – something like LogMeIn, GoToMyPC, or TeamViewer. These are all great services, but they add an extra layer onto the system – my laptop already has VNC built in, and installing VNC is easy and simple on machines that don’t have it. The problem is that VNC is meant to be connected to on the local network, and while VNC connection services are available, that’s again, another layer.
In theory, I could have my laptop always connected to my home VPN when I’m out and about. I’d have to remember to connect it each time, and depending on my settings, it might route all traffic through my home network which slows things down and inflates my internet usage. I really only need it to connect when I need to VNC in, and that’s why this script is useful.
How It Works
The script works by checking for a file on my web server ever 2 minutes. If that file is present, it runs a script to connect the VPN. If the file isn’t present, it does nothing. Here’s the code:
if curl –output /dev/null –silent –head –fail “$yourURL”
pgrep -f -x “$COMMAND” > /dev/null 2>&1 || $COMMAND
All you have to do is replace the COMMAND with the connection command for your VPN solution (or anything else you want to remotely trigger) and the yourURL with the address you want to query. On my web server, I can use a command such as ‘touch connect-vpn’ in my web directory to create the file, and then simply ‘rm connect-vpn’ to reset. On my computer, I have saved this script as macbook-vpn.sh and use the following CRON entry to run it every 2 minutes, although you could run it however often or as little as you like:
*/2 * * * * /Users/jon/macbook-vpn.sh
From here, you could make this as complex or as simple as you like. You could have another URL that, if found, would disconnect the VPN. You could also script multiple things into your VPN connection/disconnection. In the past I’ve used this script to create a reverse SSH proxy connection, effectively giving my firewalled PC a publicly accessible port – useful again when I need to get into something remotely and securely.