Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Pinging IP Address via apple script

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)

Posted on Mar 27, 2012 2:01 AM

Reply
14 replies

Apr 6, 2017 7:33 PM in response to hoarebags

I have a very similar situation I hope you all can help me with.

I am trying to automate a task with a calendar event. I need to ping Google and then send the results at the end of the day to an email recipient.

I am new to Automater and AppleScript. I have figured out how use Automator with the record function, but am struggling on putting it all together.

I'm sure there must be an easier way with just a single script.

Any help would be greatly appreciated.

Apr 7, 2017 6:08 AM in response to hoarebags

Give this a try. Tested on El Capitan 10.11.6.


-- Get IP address from user, and determine if the connection is [un]successful.

-- Needs valid ip format checker for input, otherwise error path


use scripting additions


set ip_address to ""

set ping to false as boolean

-- if we find valid connection return true and otherwise false

set is_valid_ip to " | grep -qm1 64 && echo \"true\" || echo \"false\" &"


set ip_address to text returned of (display dialog "Enter IP Address " default answer "" with title "Ping")

-- log ip_address


try

set ping to (do shell script "ping -c2 " & ip_address & is_valid_ip) as boolean

if ping then

display dialog "Connection Successful"

else

display dialog "Connection Unsuccessful"

end if

on error errmsgnumbererrnbr

my error_handler(errnbr, errmsg)

end try

return


on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler

Apr 7, 2017 2:35 PM in response to VikingOSX

Decided to add an IPV4 and IPV6 address validator, and clean up the code.


-- Get IP address (IPV4, or IPV6) from user, validate it, and determine if the connection

-- can be made with ping or ping6. Report [un]successful.

-- Added IP address validator

-- Revision 2.0, Tested: El Capitan 10.11.6, Python 2.7.10 (OS X)

-- VikingOSX, 2017-04-07, Apple Support Communities


use scripting additions


set ip_dialog to "Enter the IP Address" & return & return & ¬

"IPV4 Format: nn.nn.nn.nn (as Integers)" & return & ¬

"IPV6 Format: nnnn:nnnn:nnnn:nnnn:: (as hexadecimal values)" & return & ¬

"IPV6 Format: nnnn:nnnn:nnnn:nnnn:nnnn:nnnn:nnnn:nnnn: (long form)"


set ip_address to ""

set ip_address to text returned of (display dialogip_dialogdefault answer "" with title "Ping")

-- log ip_address


set ret to validate_ip(ip_address) as boolean

if not ret then

display alert "Invalid IP Address" messageip_addressascriticalgiving up after 10

return

end if


try

if ip_address contains "." then

set ping to (do shell script "ping -c2 " & ip_address)

else if ip_address contains ":" then

set ping to (do shell script "ping6 -c2 " & ip_address)

end if

display dialog "Connection Successful"

on error errmsgnumbererrnbr

display dialog "Connection Unsuccessful"

my error_handler(errnbr, errmsg)

end try

return


on validate_ip(addrStr)


return do shell script "python <<'EOF' - " & addrStr & "

#!/usr/bin/python

# coding: utf-8


import socket

import sys


try:

addr = ''.join(sys.argv[1])

if addr.count('.') == 3 and socket.inet_pton(socket.AF_INET, addr):

print(True)

elif addr.count(':') >= 4 and socket.inet_pton(socket.AF_INET6, addr):

print(True)

else:

print(False)

except socket.error:

print(False)

EOF"

end validate_ip


on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler

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.

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.

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

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

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

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.

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

--

settheIp_adresstothetext returnedofDialog_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!

Pinging IP Address via apple script

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