You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to Automatically Connect VPN

Recently got a MacBook and when I'm at home I use it to connect to my university's VPN connection. This works well except for the fact that it disconnects every time my laptop sleeps or locks so I wrote this applescript:


on idle

tell application "System Events"

tell current location of network preferences

set myConnection to the service "VPN University"

if myConnection is not null then

if current configuration of myConnection is not connected then

connect myConnection

end if

end if

end tell

return 120

end tell

end idle


I also edited the info.plist so that the application was completely hidden at all times. This works well whilst I have the ethernet cable attached and want to be on the VPN, but as soon as I move away from my desk I want to be on the wifi and the continuous 'failed to join VPN' error messages are quite annoying.


I was wondering if there was an applescript workaround that would somehow test whether I have the ethernet cable attached? I realise I may be asking a bit much.


Thanks.

MacBook Pro with Retina display, OS X Mavericks (10.9.4)

Posted on Nov 2, 2014 4:44 AM

Reply
6 replies

Jul 16, 2017 10:44 AM in response to Arkouda

hello. Why this isn't working in Sierra 10.12.5? Can you help me with works script? Thanks

on idle

tell application "System Events"

tell current location of network preferences

set myConnection to the service "VPN University"

if myConnection is not null then

if current configuration of myConnection is not connected then

connect myConnection

end if

end if

end tell

return 120

end tell

end idle

Nov 2, 2014 5:37 AM in response to edosmond

You can check whether a particular interface has an ip address set, so that will probably do what you want. This illustrates one way of checking whether ethernet or wifi is live:


set ethernet to do shell script "ipconfig getifaddr en0; exit 0"

set wifi to do shell script "ipconfig getifaddr en1; exit 0"

if ethernet is not "" then

display dialog "ethernet connected"

else

if wifi is not "" then

display dialog "wifi connected"

end if

end if

Nov 2, 2014 12:43 PM in response to edosmond

I think you just need to wrap your whole script in a test to see whether ethernet is connected:


on idle

set ethernet to do shell script "ipconfig getifaddr en0; exit 0"

if ethernet is not "" then

tell application "System Events"

tell current location of network preferences

set myConnection to the service "VPN University"

if myConnection is not null then

if current configuration of myConnection is not connected then


connectmyConnection

end if

end if

end tell

return 120

end tell

end if

end idle

Applescript to Automatically Connect VPN

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.