11 Replies Latest reply: Mar 20, 2013 5:50 AM by softwater Branched to a new discussion.
hoarebags Level 1 Level 1 (0 points)

Hi im new to apple scripting and this is my second script im trying to create.
What i would like this script to do is have the user input and ip and ping that address my problem lies in getting ip address from the dialouge box into the "do shell script", this is my script so far

 

set Ip_address to ""

set Dialog_1 to display dialog "Enter Ip Address" default answer "" with title "Ping"

set the Ip_adress to the text returned of Dialog_1

set ping to (do shell script "ping -c 2 & Ip_address & ")

if ping contains "64 bytes" then display dialog "Connection Secessful"

 

 

 

And also how do i make a variable so if it comes back i unresponsive  i can have a dialog box say "Connection Unsecessful"
Any help would be muchly appreciated. (I know i can do this maunally but i would like to create a script so its easier for the end user)


MacBook, Mac OS X (10.6.8)
  • 1. Re: Pinging IP Address via apple script
    Pierre L. Level 4 Level 4 (3,860 points)

    Let's begin with the first question:

    my problem lies in getting ip address from the dialouge box into the "do shell script"

     

    Here's how your script should be written:

     

    set IP_address to ""

    set dialog_1 to display dialog "Enter IP Address:" default answer "" with title "Ping"

    set the IP_address to the text returned of dialog_1

    set ping to (do shell script "ping -c 2 " & IP_address)

    if ping contains "64 bytes" then display dialog "Connection Successful." buttons {"OK"} default button 1


     

    Message was edited by: Pierre L.

  • 2. Re: Pinging IP Address via apple script
    Cole Tierney Level 4 Level 4 (1,375 points)

    You could ignore the ping results and just check the exit status. Here are a few examples checking to see if a volume is mounted with do shell script: http://putnamhill.net/applescript/check-volume-mounted

  • 3. Re: Pinging IP Address via apple script
    MrHoffman Level 6 Level 6 (12,455 points)

    Network Utility provides a graphical version of ping, if that'll help.

     

    Applications > Utilities > Network Utility

     

    It'd probably be simpler to try this ping sequence directly from a bash shell script, and to display that for the user via a Terminal.app window.  Alternatively, consider coding this using some Python (such as these suggestions, via PyObjC bridge or otherwise) and let that display the results.  (Python is more capable in this area than is AppleScript, particularly with the Objective C bridging.)

     

    AppleScript is good for scripting various Apple GUI-based tools, but it's not quite so good at performing tasks outside of that context.  And a Unix tool such as ping is outside of that context.

     

    In general, ping only provides the most basic idea of what's wrong with a connection failure, so I'm somewhat skeptical of turning an inexperienced user loose with a GUI-based ping tool.  If you do follow this course, then you'll likely also eventually want to fetch additional details such as the DNS servers and translations and maybe an OpenSSL s_client into the target server (or other test); more detals will probably also be necessary (for you or another experienced user) to troubleshoot the error.

  • 4. Re: Pinging IP Address via apple script
    Camelot Level 8 Level 8 (45,790 points)

    You're overcomplicating your test.

     

    If ping gets no response form the host it returns an error. This error will report back to AppleScript and AppleScript will, by default, report an error and stop the script.

     

    You can test this by pinging a bogus address to see the result.

     

    That might be enough, but if you want to customize the user response, use a try block:

     

    set IP_address to ""

    set dialog_1 to display dialog "Enter IP Address:" default answer "" with title "Ping"

    set the IP_address to the text returned of dialog_1

    try

              set ping to (do shell script "ping -c 2 " & IP_address)

              display dialog "Connection Successful." buttons {"OK"} default button 1

    on error

      -- if we get here, the ping failed

              display dialog "Conection failed. Host is down" buttons {"Darn"} default button 1

    end try

  • 5. Re: Pinging IP Address via apple script
    hoarebags Level 1 Level 1 (0 points)

    Thank you for your help this is what i needed the script to do and will make part of my job alot eaiser.

  • 6. Re: Pinging IP Address via apple script
    hoarebags Level 1 Level 1 (0 points)

    Okay cool thanks for the help.
    So basically i needed to keep the action in quotes and didn't need to have Ip_address in the quotes.

    Thanks again learnt something new and will help in my future scripts.

  • 7. Re: Pinging IP Address via apple script
    gexooo Level 1 Level 1 (0 points)

    This worked for me =)

     

    set IP_address to ""

    set dialog_1 to display dialog "Enter IP Address:" default answer "" with title "Ping"

    set the IP_address to the text returned of dialog_1

    try

              do shell script ("ping -c 2 " & IP_address)

      display dialog "Host up" buttons {"OK"} default button 1

    on error

      display dialog "Host unreachable" buttons {"OK"} default button 1

    end try


  • 8. Re: Pinging IP Address via apple script
    gexooo Level 1 Level 1 (0 points)

    Ok little improvements

     

    set IP_address to ""

    set dialog_1 to display dialog "Enter IP Address:" default answer "" with title "Ping"

    set the IP_address to the text returned of dialog_1

    try

              set ping to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")

              if ping contains "ms" then

      display dialog ("Host UP - " & ping) buttons {"OK"} default button 1 with title "UP"

              else if ping contains "timeout" then

      display dialog "Host unreachable" buttons {"OK"} default button 1 with title "DOWN"

              else

                        display dialog "Your input was not correct!" buttons {"Quit"} default button 1 with title "Error"

              end if

    end try


  • 9. Re: Pinging IP Address via apple script
    Camelot Level 8 Level 8 (45,790 points)

    Just as an aside, be careful when doing something like this - taking unaudited input from a user and passing it to a shell command.

     

    For example, if you look at my script, I ask the user for some input:

     

    set dialog_1 to display dialog "Enter IP Address:" default answer "" with title "Ping"

    set the IP_address to the text returned of dialog_1


    and I pass that input to a shell command:

     

              set ping to (do shell script "ping -c 2 " & IP_address)

     

    But nowhere here do I validate the input to make sure it's a valid IP address (or even looks like a valid IP).

     

    Now that kind of validation might not be necessary if you're just using this yourself, but if you're asking other people to run it, or running it on other machines there's a world of possibilities.

     

    For example, consider what would happen if the user entered '-c 100000 -f 192.168.0.255' (assuming that you're on a 192.168.0.0/255.255.255.0 network)... you've just caused a ping flood that will send 100,000 ping requests to your network's broadcast address (and, by extension, every device on your network). This will bring your network to its knees, or at least disrupt normal legitimate traffic flows.

     

    Even bigger problems could be at hand if the user enters a pipe symbol or a semicolon - these allow multiple commands to be strung together, so now you've given your user the ability to run ANY shell command on that machine. At least you're not using administrator privileges with your command, but the potential for damage is still there.

     

    So the script should be considered unfinished, at best - it needs an input validation check to ensure that what's entered is what you expect - in this case, an IP address and only an IP address.

  • 10. Re: Pinging IP Address via apple script
    gexooo Level 1 Level 1 (0 points)

    Thanks for your answer Iam fairly new at apple-script so i guess it is not easy to validate the input because the input can also be an DNS-name like "google.de"

  • 11. Re: Pinging IP Address via apple script
    softwater Level 5 Level 5 (5,370 points)

    Aside from what has been said above, you should look out for things like this

     

     

    set Ip_address to ""

    --

    set the Ip_adress to the text returned of Dialog_1

    set ping to (do shell script "ping -c 2 & Ip_address & ")

    --

     

    Look at your second use of Ip_address. You actually wrote 'Ip_adress', which in fact instantiates a completely different variable from the first one. Because of that, the third use will actually mean the do shell script command will get substitued with the value "" instead of the text returned of Dialog_1.

     

    Pay attention to the colour coding in AppleScript and it will help you find typos like this, which otherwise will totally screw up your script!