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

Using Applescript to connect to VPN and add routes

I posted this in the wrong place last night, but actually managed to get an answer to it. Here is the original post:

---
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
---

From the response I received, I modified the script to this:

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
---

That seems to work perfectly, but I'm still wondering if there is a better way of handling the routes. Suggestions?

Thanks.

17" MBP Unibody, Mac OS X (10.6.2), 320GB 7200RPM, 4GB RAM, Anti-Glare

Posted on Dec 5, 2009 6:09 AM

Reply
2 replies

Dec 5, 2009 10:01 AM in response to Anthony Curcione

That seems to work perfectly, but I'm still wondering if there is a better way of handling the routes. Suggestions?


The most obvious way is to do it at the VPN server level.

The standard procedure when the VPN connection establishes is that the server tells the client what DNS server to use and what IP addresses/networks to send over the VPN connection.

Therefore, I can't help thinking that if the VPN server is configured properly, all this should be unnecessary.

That's in addition to the fact that your script seems to serve no purpose - it simply deletes a route then adds it straight back in. What do you expect this to achieve?

Dec 7, 2009 12:57 PM in response to Camelot

I realize the VPN gateway should add the routes, but it's not and they don't seem to want it to. No idea why.

As for deleting/adding the routes, yes it does seem wrong to do this or pointless. The reason I'm doing this is for what I found in my testing of the script which was if the script hung for some reason and the route did not fully go into the table, I would be unable to add it with the script when I ran it again.

I'm probably not explaining it that well, but when I couldn't route after trying to run my script initially, I tried manually running the add route command and I'd get an error. Thus, I deleted/added the route and than it worked again.

As pointless as it seems it works every time. I'm sure there's a better/cleaner/smarter way of doing this, but I don't know enough about Applescript to do any more. At this point I can probably remove the delete command, though, since it should run properly each time.

Using Applescript to connect to VPN and add routes

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