VPN check connectivity

Hey.
I`m a newbie to applescript so here goes :smiley:

I`m trying to make an applescript that checks Viscosity or Tunnelblick if a profile is connected.
Then i want to connect to a network disk.
If the vpn connection is disconnected i want to disconnect the network drive.
And if i connect the vpn profile i want to connect to the network drive again.
And i want it to check all the time.

I have this so far:


	tell application "Finder"
		try
			mount volume "afp://user:password@150.0.0.100/arkiv"
		end try
	end tell

And it works :smiley:
If somebody would send me in the right direction or help me out a bit it would be great!

Hey Espensgr

I don’t have a direct answer for your question.

But I recently had to make a script/app that automatically mounted a given Disk, when a user logged in.

Hopefully you can use some of this.

Bare over with the dialogs, they are quickly translated from Danish…


(*===== Properties =====*)
property theURLPath : missing value
property theLocalPath : missing value
property theVolume : missing value
property theVPNid : missing value
property theHostname : missing value
property lastRun : missing value

(*===== Script =====*)

if lastRun = missing value then
	set lastRun to current date
else
	if lastRun > (current date) - 5 then
		my checkProperties("Clear")
	end if
end if


my checkProperties("Normal")

tell application "System Events"
	tell current location of network preferences
		if service theVPNid exists then
			tell application "Finder"
				if exists disk theVolume then
					activate
					make new Finder window to disk theVolume
				else
					if my checkVPN() is false then
						try
							my connectVPN()
							with timeout of 30 seconds
								repeat
									if my checkVPN() is true then
										exit repeat
									end if
								end repeat
							end timeout
						on error
							set front_app to (path to frontmost application as Unicode text)
							tell application front_app
								set theDialog to display dialog "Error when connecting to VPN " & quoted form of theVPNid buttons {"Reset", "Close"} default button 2 cancel button 2 with title "VPN connection failed" with icon stop
							end tell
							if button returned of theDialog is "Reset" then
								set theURLPath to missing value
								set theLocalPath to missing value
								set theVolume to missing value
								set theVPNid to missing value
								display dialog "The settings has been reset," & return & "restart the program to," & return & "enter new settings." with title "Settings is now reset" buttons {"Close"} default button 1 cancel button 1 with icon stop
								return
							end if
						end try
					end if
					try
						my mountVolume()
					on error
						set front_app to (path to frontmost application as Unicode text)
						tell application front_app
							set theDialog to display dialog "Error when connecting to the disk " & quoted form of theVolume buttons {"Reset", "Close"} default button 2 cancel button 2 with title "Connection to " & quoted form of theVolume & " failed" with icon stop
						end tell
						if button returned of theDialog is "Reset" then
							set theURLPath to missing value
							set theLocalPath to missing value
							set theVolume to missing value
							set theVPNid to missing value
							display dialog "The settings has been reset," & return & "restart the program to," & return & "enter new settings." with title "Settings is now reset" buttons {"Close"} default button 1 cancel button 1 with icon stop
							return
						end if
					end try
					set lastRun to current date
				end if
			end tell
		else
			set front_app to (path to frontmost application as Unicode text)
			tell application front_app
				set theDialog to display dialog "The VPN service " & quoted form of theVPNid & " does not exist in Network, in System Preferences" buttons {"Reset", "Close"} default button 2 cancel button 2 with title "The VPN service is missing" with icon stop
			end tell
			if button returned of theDialog is "Reset" then
				set theURLPath to missing value
				set theLocalPath to missing value
				set theVolume to missing value
				set theVPNid to missing value
				display dialog "The settings has been reset," & return & "restart the program to," & return & "enter new settings." with title "Settings is now reset" buttons {"Close"} default button 1 cancel button 1 with icon stop
				return
			end if
		end if
	end tell
end tell

(*===== Subrountines =====*)

on mountVolume()
	set theNumber to 0
	set thePath to theLocalPath
	repeat
		set theNumber to theNumber + 1
		try
			do shell script "cd " & quoted form of thePath
			set thePath to thePath & theNumber as string
		on error
			exit repeat
		end try
	end repeat
	do shell script "mkdir " & quoted form of thePath
	do shell script "mount -t afp " & quoted form of theURLPath & " " & quoted form of thePath
end mountVolume

on connectVPN()
	tell application "System Events"
		tell current location of network preferences
			set VPNservice to service theVPNid
			try
				if exists VPNservice then connect VPNservice
			on error
				set front_app to (path to frontmost application as Unicode text)
				tell application front_app
					display dialog "Unable to make VPN connection to " & quoted form of theVPNid buttons {"Close"} with title "VPN connection failed" with icon stop
				end tell
				return
			end try
		end tell
	end tell
	return true
end connectVPN

on checkVPN()
	tell application "System Events"
		tell current location of network preferences
			set VPNservice to service theVPNid
			if connected of current configuration of VPNservice then
				return true
			else
				return false
			end if
		end tell
	end tell
end checkVPN

on checkProperties(theCode)
	set theCurrentHostname to do shell script "hostname"
	
	if theCode = "Clear" then
		set front_app to (path to frontmost application as Unicode text)
		tell application front_app
			set theDialog to display dialog "Do you wish to reset settings" & return & "or continue normally??" buttons {"Continue", "Reset"} with title "Reset settings?"
		end tell
		if button returned of theDialog is "Reset" then
			set theURLPath to missing value
			set theLocalPath to missing value
			set theVolume to missing value
			set theVPNid to missing value
			set theHostname to theCurrentHostname
		end if
	end if
	
	if theHostname = missing value or theHostname is not theCurrentHostname then
		set theURLPath to missing value
		set theLocalPath to missing value
		set theVolume to missing value
		set theVPNid to missing value
		set theHostname to theCurrentHostname
	end if
	
	if theURLPath = missing value then
		set theTitle to "Enter the URL"
		set theText to "...the Disk to be mounted, " & return & "eg. afp://user:password@10.0.0.1/Disk"
		set theURLPath to my enterInfo(theText, theTitle)
		set lastRun to current date
	end if
	
	if theLocalPath = missing value then
		set theTitle to "Enter the local path"
		set theText to "...where the Disk is to be mounted, " & return & "eg. /Volumes/Disken"
		set theLocalPath to my enterInfo(theText, theTitle)
		set lastRun to current date
	end if
	
	if theVolume = missing value then
		set theTitle to "Enter the name of the Disk"
		set theText to "...to be mounted, " & return & "eg. Diskname"
		set theVolume to my enterInfo(theText, theTitle)
		set lastRun to current date
	end if
	
	if theVPNid = missing value then
		set theTitle to "Enter the name of the VPN service"
		set theText to "...in System Preferences, " & return & "eg. VPN eller Companyname"
		set theVPNid to my enterInfo(theText, theTitle)
		set lastRun to current date
	end if
end checkProperties

on enterInfo(theText, theTitle)
	repeat
		set front_app to (path to frontmost application as Unicode text)
		tell application front_app
			
			set theDialog to display dialog theText with title theTitle default answer "" buttons {"Close", "OK"} default button 2 cancel button "Close"
			if text returned of theDialog is not "" then
				exit repeat
			end if
		end tell
	end repeat
	return (text returned of theDialog) as string
end enterInfo