VPN Auto-Connect
Last modification: Friday, October 30, 2009 04:24 pmMeant to be run as a Stay Open AppleScript Application this script will auto connect a specified VPN service, if it is found disconnected.
Implementation
property pSERVICE_NAME : "INSERT_YOUR_VPN_SERVICE_NAME_HERE"
property pPOLLING_INTERVAL : 60
on idle
try
with timeout of pPOLLING_INTERVAL seconds
-- Make sure host is connected to internet.
if pingNetworkAddress("www.google.com", 3) then
tell application "System Events"
tell current location of network preferences
set _service to service pSERVICE_NAME
-- Find out whether the service is connected.
set _isConnected to connected of current configuration of _service
-- Connect, if service is disconnected.
if _isConnected is false then connect _service
end tell
end tell
end if
end timeout
end try
return pPOLLING_INTERVAL
end idle
on pingNetworkAddress(_address, _timeout)
script PingNetworkAddress_Extras
on pathForTemporaryFile(_folderName)
(*
Generates a unique path for a file in the
current user's temporary items folder.
Takes a single argument that can be set
to the name of a subfolder or false to
create no subfolder.
*)
-- Create timestamp
set _date to current date
set _month to month of _date as integer
set _day to day of _date
set _year to year of _date as string
set _time to (time of _date)
set _hour to _time div 60 div 60
set _minute to (_time div 60) - (_hour * 60)
set _second to _time - (_minute * 60) - (_hour * 60 * 60)
if _month is less than 10 then set _month to "0" & _month
if _day is less than 10 then set _day to "0" & _day
set _yearShort to characters -2 thru -1 of _year
if _hour is less than 10 then set _hour to "0" & _hour
if _minute is less than 10 then set _minute to "0" & _minute
if _second is less than 10 then set _second to "0" & _second
set _prvDlmt to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set _timeStamp to ((_year & "-" & _month & "-" & _day & "_" & _hour & "-" & _minute & "-" & _second) as string)
set AppleScript's text item delimiters to _prvDlmt
-- Generate pseudorandom numbers
set _rand1 to (round (random number from 100 to 999)) as string
set _rand2 to (round (random number from 100 to 999)) as string
set _rand3 to (round (random number from 100 to 999)) as string
set _rand4 to (round (random number from 100 to 999)) as string
set _randomString to _rand1 & "-" & _rand2 & "-" & _rand3 & "-" & _rand4
-- Create file name
set _fileName to ((_timeStamp & "_" & _randomString) as string)
-- Get the path to the parent folder
set _temporaryFolderPath to path to temporary items folder from user domain as string
if _folderName is false then
set _parentFolderPath to _temporaryFolderPath
else
set _parentFolderPath to _temporaryFolderPath & _folderName & ":"
tell application "System Events" to if (exists folder _parentFolderPath) is false then make new folder at the end of folders of folder _temporaryFolderPath with properties {name:_folderName}
end if
-- Make sure the file does not exist
set _rNumber to 1
repeat
if _rNumber is 1 then
set _tempFilePath to _parentFolderPath & _fileName
else
set _tempFilePath to _parentFolderPath & _fileName & "_" & (_rNumber as string)
end if
tell application "System Events" to if (exists file _tempFilePath) is false then exit repeat
set _rNumber to _rNumber + 1
end repeat
return _tempFilePath
end pathForTemporaryFile
end script
-- Quote the address
set _addressQ to quoted form of _address
-- Get the path to a unique temporary file as a quoted posix path (QPP)
tell PingNetworkAddress_Extras to set _pingFilePath to pathForTemporaryFile("PingNetworkAddress")
set _pingFileQPP to quoted form of (POSIX path of _pingFilePath)
-- Run ping tool
do shell script "ping -c 1 " & _addressQ & " > " & _pingFileQPP & " 2>&1 &"
-- Check for result
set _timeTaken to 0.0
repeat
set _pingResult to do shell script "cat " & _pingFileQPP
if _pingResult is not "" then
if _pingResult contains " 0% packet loss" or _pingResult contains " 0.0% packet loss" then
-- Return positive result
do shell script "rm -f " & _pingFileQPP
return true
end if
end if
delay 0.1
set _timeTaken to _timeTaken + 0.1
if _timeTaken is greater than or equal to _timeout then exit repeat
end repeat
-- " 0% packet loss" was not in the result
-- Return negative result
do shell script "rm -f " & _pingFileQPP
return false
end pingNetworkAddress