Categories
Code Sample Geek

Auto Connect to VPN on Boot & Login in MacOS Sierra

I added VPN connectivity to my network and I wanted to make sure the connection to the remote VPN was always running. One way to do this is to use AppleScript to check the connection status. The problem I had was that the AppleScripts that I found no longer worked with MacOS Sierra.

After some digging, I got it to work. Here’s my script:


on idle
  set myVPN to "VPN (dallas)" -- set name of VPN connection
  try
    set isConnected to do shell script "scutil --nc show \"" & myVPN & "\" | grep -c Connected"
  on error
    set isConnected to "0"
  end try
  if isConnected = "0" then
    do shell script "scutil --nc start \"" & myVPN & "\""
  end if
  return 15 -- how often to check, seconds
end idle

Modifications

Change the variable myVPN to the name of your network connection. Here mine is VPN (dallas).

Also modify the return value if you want the time between checks to be longer or shorter. Here I’ve set mine to 15 seconds.

References

These are the some of the pages that I started with. The instructions for script generation and auto-boot still apply. So you can still reference them for those other tasks:

  • http://osxdaily.com/2016/08/10/auto-connect-vpn-mac-boot-login/
  • https://www.maketecheasier.com/auto-connect-vpn-mac-startup/

Some other tech references which helped:
* https://www.cyberciti.biz/faq/mac-osx-applescript-run-shell-script/
* “on idle” reference

Leave a Reply

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