Benches macOS

2

Trying to set a delay in an Applescript for VPN connection

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I need to be able to set some routes upon opening a particular VPN connection so I did some searching and found a really simple Applescript that does the job. Problem is it tries to set the routes before the VPN actually connects so the routes don't go in.

I added in a 10 second delay which does the trick, but I'm thinking there has to be a way to do this that waits until the VPN actually connects before continuing - so if it takes 5 seconds or 10 or whatever, it waits.

The other thing I'm doing that I think is bad is I'm sending a route delete command before sending the add command. Why? Because if I don't and for some reason the route is partially in the table, it doesn't give an error and ends up not routing. Again, probably a better way to do this.

Here is my current script"

-- Connect Work VPN

tell application "System Events"

tell current location of network preferences

set VPNservice to service "Work" -- name of the VPN service

if exists VPNservice then connect VPNservice

end tell

end tell

delay 10

set gateway to "x.x.x.x" -- omitted here for security

do shell script "route delete 192.168.25.0/24 " & gateway with administrator privileges

do shell script "route delete 192.168.20.0/24 " & gateway with administrator privileges

do shell script "route add 192.168.25.0/24 " & gateway with administrator privileges

do shell script "route add 192.168.20.0/24 " & gateway with administrator privileges

Any suggestions??

Thanks.

2 replies

Sign in or join Beachball Help to reply.

  1. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    you might want to try asking in the Applescript forum under OS X technologies.

    I don't have any VPN connections so can't test anything but applescript dictionary for system events indicates that configuration property of a service has a boolean property "connected". so just run a loop with, say, 1 second delay until this porperty becomes true. presumably it would be something along the lines

    <pre style="

    font-family: Monaco, 'Courier New', Courier, monospace;

    font-size: 10px;

    margin: 0px;

    padding: 5px;

    border: 1px solid #000000;

    width: 720px;

    color: #000000;

    background-color: #ADD8E6;

    overflow: auto;"

    title="this text can be pasted into the Script Editor">

    tell application "System Events"

    tell current location of network preferences

    set VPNservice to service "Work" -- name of the VPN service

    if exists VPNservice then connect VPNservice

    repeat until (connected of current configuration of VPNservice)

    delay 1

    end repeat

    end tell

    end tell

    set gateway to "x.x.x.x" -- omitted here for security

    do shell script "route delete 192.168.25.0/24 " & gateway with administrator privileges

    do shell script "route delete 192.168.20.0/24 " & gateway with administrator privileges

    do shell script "route add 192.168.25.0/24 " & gateway with administrator privileges

    do shell script "route add 192.168.20.0/24 " & gateway with administrator privileges</pre>

  2. 0

    Anthony Curcione

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Actually that was perfect and works! And I couldn't remember where the Applescript forum was, but now that I know, I'll link to this to see if there are any better ways of doing what I'm trying to do.

    Thanks.