3 Replies Latest reply: Oct 23, 2012 9:18 AM by Camelot
macgeek4life Level 1 Level 1 (5 points)

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


  • 1. Re: Applescript/Ping/Connect VPN
    Camelot Level 8 Level 8 (45,790 points)

    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.

  • 2. Re: Applescript/Ping/Connect VPN
    macgeek4life Level 1 Level 1 (5 points)

    Your suggestion worked as designed. However, our needs have changed and we no longer want to connect to the VPN if the server cannot be reached. We simply want to end the script.

     

    So we'd like to ping the server and if available, mount the share. If it's not available, stop the script so that we don't receive an error becasue the share could not be mounted. I'm sure this is simple, but becasue I'm new to it I can't quite figure it out. I've got it to work except for ending the script if not available part.

     

    Any help would be much appreciated.

     

    This is what I have...

     

    if (do shell script "ping -c 1 -t 1 xxx.xxx.xxx.xxx") contains "1 packets received" then

              try

                        mount volume "smb://storm/files$"

              end try

    end if


  • 3. Re: Applescript/Ping/Connect VPN
    Camelot Level 8 Level 8 (45,790 points)

    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.