Disconnecting VPN on Sleep

I have been experiencing an constant annoyance when I forget to disconnect from my Corporate VPN. In this case when the Mac goes to sleep for an extended period of time, on wake it thinks it is still connected to the VPN and tries to reestablish a connection at which point  (I assume) our Cisco VPN server detects this as an attempted hack and disables my account for a period of time.

I know this may not be a common issues for all VPNs however for me it is annoying. My solution was to write a small AppleScript that would disconnect from the VPN on shutdown.

1. Create /Library/Scripts/DisconnectVPN.scpt
2. Add the following script logic

tell application "System Events"
    tell current location of network preferences
        set VPNService to service "Corporate VPN"
        if exists VPNService then disconnect VPNService
    end tell
end tell

3. Note that "Corporate VPN" is the name of the network connection found in the left pane of the "Network Preferences" page.

The next step is to have this script execute on sleep. This involves creating a hidden .sleep file in your home directory. Be careful if you use TextEdit to create the new file as it will append an additional extension to the file name and thus it will not work properly.

1. Create ~/.sleep
2. Add the following script

#!/bin/sh
osascript ~/Library/Scripts/DisconnectVPN.scpt


Note, that because of the leading . this file will be invisible in the normal finder window. You can use the terminal command ls -al to show all files or follow the below link to show hidden files in finder:

www.brooksandrus.com/blog/2007/03/23/mac-os-x-show-hide-hidden-files-in-finder/
 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

Leave a comment

Submitted comments are subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.