Skip navigation

Pinging IP Address via apple script

3691 Views 11 Replies Latest reply: Mar 20, 2013 5:50 AM by softwater RSS Branched to a new discussion.
hoarebags Calculating status...
Currently Being Moderated
Mar 27, 2012 2:01 AM

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)
  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Mar 27, 2012 4:19 AM (in response to hoarebags)

    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.

  • Cole Tierney Level 4 Level 4 (1,375 points)
    Currently Being Moderated
    Mar 27, 2012 5:21 AM (in response to hoarebags)

    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

  • MrHoffman Level 6 Level 6 (11,695 points)
    Currently Being Moderated
    Mar 27, 2012 5:56 AM (in response to hoarebags)

    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.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Mar 27, 2012 10:00 AM (in response to hoarebags)

    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

  • gexooo Calculating status...
    Currently Being Moderated
    Apr 22, 2012 10:02 AM (in response to hoarebags)

    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


  • gexooo Level 1 Level 1 (0 points)
    Currently Being Moderated
    Apr 22, 2012 10:59 AM (in response to gexooo)

    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


  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Apr 23, 2012 12:07 AM (in response to hoarebags)

    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.

  • gexooo Level 1 Level 1 (0 points)
    Currently Being Moderated
    Apr 23, 2012 1:17 AM (in response to Camelot)

    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"

  • softwater Level 5 Level 5 (5,370 points)
    Currently Being Moderated
    Mar 20, 2013 5:50 AM (in response to hoarebags)

    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!

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

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.