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/
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/

"Hi,
I have faced this VPN error many times. Though I have searched the internet for the solution to it and never came up with a concrete solution. Your solution looks impressive I am surely going to try it.
Thanks for this wonderful information. "
Reply to this
I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.
Reply to this
I suffered from this problem at my last job. I never did figure it out. The AppleScript you wrote looks like it would have done the trick for me. I'll put it in my tips and tricks file for future reference.
Thanks . . .
P.S. I love the name of your blog. LOL!
Reply to this
I find myself coming to your blog more and more often to the point where my visits are almost daily now!
Reply to this
Awesome Blog. I add this Post to my bookmarks.
Reply to this