Skip navigation

Applescript/Ping/Connect VPN

1474 Views 3 Replies Latest reply: Oct 23, 2012 9:18 AM by Camelot RSS
macgeek4life Calculating status...
Currently Being Moderated
Oct 12, 2012 12:36 PM

I wrote an Applescript to start the VPN and then mount a network drive. Now I would like to add logic to the beginning to ping an internal resource first. If the resource is unreachable, then connect and mount the drives. Otherwise, just mount the drives.

 

Basically, I obviously don't need the VPN if the machine is on the network. I would then just need to mount the dirves. But if it's not on the network, I would need to connect to the VPN first.

 

My plan is to save it as an app and have it run at logon.

 

Here is what I have so far. Any help would be appreciated.

 

tell application "System Events"

          tell current location of network preferences

                    set VPNservice to service "AWH VPN"

  connect VPNservice

          end tell

end tell

 

 

delay 10

 

try

          mount volume "smb://Drive/share"

end try


  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Oct 13, 2012 8:06 AM (in response to macgeek4life)

    This doesn't seem hard to me...

     

    I would like to add logic to the beginning to ping an internal resource first.

     

    You can just have your script ping the server address first and connect the VPN if you're unable to, like:

     

    try

              do shell script "ping -c 1 -t 1 hostname.of.your.server"

    on error

      -- we're not online, so connect the VPN

              tell application "System Events"

                        tell current location of network preferences

                                  set VPNservice to service "AWH VPN"

      connect VPNservice

                        end tell

              end tell

      delay 10

    end try

     

    try

              mount volume "smb://Drive/share"

    end try


    The premise here is that the 'do shell script "ping..."' will fail if the server can't be reached, thereby triggering the 'on error' part of the script (where you establish the VPN connection).

    If the server can be reached then the 'on error' code is skipped, leading to the part of the script that mounts the share point.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Oct 23, 2012 9:18 AM (in response to macgeek4life)

    In this case you're being tripped by the fact that ping returns an error if the specified host can't be reached. If you want to catch that error and not report it to the user, then your ping needs to be inside the try block, like:

     

    try

              do shell script "ping -c 1 -t 1 hostname.of.your.server"

              mount volume "smb://server/sharename$"

    end try

     

    The idea here is that if any of the commands throw an error the script jumps to the 'end try' statement (or the on error' statement if there is one). In this case, if the ping fails, the script ends, and if the ping succeeds then the script progresses to the mount volume command.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.