Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ring

macrumors regular
Original poster
Nov 17, 2011
156
0
Hi. I'm trying to get a script working that basically disconnects my internet if the vpn goes out. I found a script on lifehacker that will automatically reconnect the vpn if it fails, and I took that and modified it into this.



But I noticed that it didnt work properly (nothing happened when I manually disconnected it.

Code:
 on idle
	
	tell application "System Events"
		
		tell current location of network preferences
			
			set myConnection to the service "BTGuard VPN"
			
			set failsafe to the service "Wi-Fi"
			
			if myConnection is not null then
				
				if current configuration of myConnection is not connected then
					
					disconnect failsafe
					
				end if
				
			end if
			
		end tell
		
		return 120
		
	end tell


So I looked up a way to turn wifi off in apple script, and I attempted to manipulate that bit of code into the original script. I resulted with this bit of code, which also failed to work properly.

Code:
 on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "BTGuard VPN"
			set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
			if status = "On" then
				do shell script "/usr/sbin/networksetup -setairportpower en1 off"
				if myConnection is not null then
					if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
				end if
			end if
		end tell
		return 120
	end tell
end idle


Is there a way to fix this? I'm relativley new to applescript (I used it to write an autotyper one time), so any help would be appreciated. The objective is to make it run as an always running .app (like the original lifehacker), and to have it instantly pull the plug on the internet the second the VPN drops. I will bake cookies for anyone who is willing to help/correct my thought process.


Love,
Ring :apple:
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
First, simply saying "it doesn't work" or "it failed to work properly" is too vague to diagnose. You need to apply some basic debugging principles, namely Break It Down.

Does the first do shell script run correctly? What does it return in 'status'? Be specific. Log what's in status. Post the actual output.

Does the second do shell script run correctly? Same questions: what does it return; log it; post the actual log output.


Second, what have you looked at as reference docs? Be specific. If online, post urls. If you used the 'man' command, say which man pages you read.

For example, from the online reference for networksetup:
https://developer.apple.com/library...n/Reference/ManPages/man8/networksetup.8.html
The networksetup command is used to configure network settings typically configured in the System Preferences application. The networksetup command requires at least "admin" privileges to run. Most of the set commands require "root" privileges to run.
This is the same as what 'man networksetup' would show you, so either you haven't read the man page, or you missed the important detail as underlined.


Third, refer to the reference doc for the do shell script command, and look at the option for running with admin privileges. If that won't work for you, because it requires user interaction, then you need to enter your admin password into the script as well.


http://www.mikeash.com/getting_answers.html
 

ring

macrumors regular
Original poster
Nov 17, 2011
156
0
First, simply saying "it doesn't work" or "it failed to work properly" is too vague to diagnose. You need to apply some basic debugging principles, namely Break It Down.

Does the first do shell script run correctly? What does it return in 'status'? Be specific. Log what's in status. Post the actual output.

Does the second do shell script run correctly? Same questions: what does it return; log it; post the actual log output.


Second, what have you looked at as reference docs? Be specific. If online, post urls. If you used the 'man' command, say which man pages you read.

For example, from the online reference for networksetup:
https://developer.apple.com/library...n/Reference/ManPages/man8/networksetup.8.html
The networksetup command is used to configure network settings typically configured in the System Preferences application. The networksetup command requires at least "admin" privileges to run. Most of the set commands require "root" privileges to run.
This is the same as what 'man networksetup' would show you, so either you haven't read the man page, or you missed the important detail as underlined.


Third, refer to the reference doc for the do shell script command, and look at the option for running with admin privileges. If that won't work for you, because it requires user interaction, then you need to enter your admin password into the script as well.


http://www.mikeash.com/getting_answers.html



Sorry about that. When I was messing with it, and I pressed "run", nothing came as the output. Nothing happened at all actually. The way I was testing it is I would make changes -> run app -> disconnect vpn -> wait for wifi to be killed or other changes to occur.


I guess I'm really asking is, how can I tell applescript to disconnect to wifi when the vpn connection fails. I'm not familiar with the language apart from what I picked up.


I assume, from looking at the docs I'm going to use getairportpower, but when I try that command in the Terminal.app, I get the result

networksetup -setairportpower airport off
Turning off the only airport interface found: en1


I have the below script (I am using this now) to automatically reconnect when BTGuard disconnects. Instead of disconnecting, I want it to cut off the internet completely when that happens.

This:

Code:
on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "BTGuard VPN"
			if myConnection is not null then
				if current configuration of myConnection is not connected then
					connect myConnection
				end if
			end if
		end tell
		return 1.0E-19
	end tell
end idle




I really appreciate your time to reply and want to thank you and everybody for your time/help. It's greatly appreciated, and I apologize for my lack of understanding (this is the first real time I needed to do a serious task in AppleScript!



:apple:
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Sorry about that. When I was messing with it, and I pressed "run", nothing came as the output. Nothing happened at all actually. The way I was testing it is I would make changes -> run app -> disconnect vpn -> wait for wifi to be killed or other changes to occur.

For testing comment out the on idle handler and return statement from the script and then run your script.
eg

Code:
--on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "BTGuard VPN"
			set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
			if status = "On" then
				do shell script "/usr/sbin/networksetup -setairportpower en1 off"
				if myConnection is not null then
					if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
				end if
			end if
		end tell
--		return 120
	end tell
--end idle
 

ring

macrumors regular
Original poster
Nov 17, 2011
156
0
For testing comment out the on idle handler and return statement from the script and then run your script.
eg

Code:
--on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "BTGuard VPN"
			set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
			if status = "On" then
				do shell script "/usr/sbin/networksetup -setairportpower en1 off"
				if myConnection is not null then
					if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
				end if
			end if
		end tell
--		return 120
	end tell
--end idle



Thank you very much for your help! I figured everything out (not sure exactly what didn't happen properly, but it is working now.)
 

ring

macrumors regular
Original poster
Nov 17, 2011
156
0
for those interested the code is


Code:
on idle
tell application "System Events"
	tell current location of network preferences
		set myConnection to the service "BTGuard VPN"
		set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
		if status = "On" then
			do shell script "/usr/sbin/networksetup -setairportpower en1 off"
			if myConnection is not null then
				if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
			end if
		end if
	end tell
	--		return .000000000000000000000000000000000001
end tell
end idle
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.