ping an ip address

I’ve never written a script before, so I’m looking for some help.

I would like to do the following: I would like a script that will ping an ip address at a regular interval (say, 5 minutes) for a period of time (say, 4 hours) to confirm my internet connection. I would like to stream the results of the ping to a text file on my desktop to review at the end of the 4 hours. Suggestions on how to go about doing this?

Thank you in advance for your time.

I would suggest learning a little Applescript first.

Then I would do a search here for terms like “ping” and “write to file”

Good luck!

nedloh99 is correct. This probably won’t make sense to you but this does what you asked for. Good luck and welcome to the AppleScript bbs.

To open the script below in your Script Editor don’t copy it, click on the line that says “Open this scriptlet in your Editor:”


property PingAddr : "192.168.1.101" -- ipAddress in quotes; put the one you want here
property HowOften : 5 * minutes -- this is in seconds
property howLong : 4 * hours -- this is in seconds
property PingLogHead : "PingLog for "
property start : missing value
property logFile : missing value

-- The following is called an 'on idle' handler. It must be saved as a stay-open application and then started when you want it to run. Click the 'open...' link at the top of the script in the forum to load it into your script editor.
-- A new log file will be created with a new name, but only once per day. If the application is run a second time, it will append to whatever was written the first time that day unless the earlier file has been moved off the desktop or had it's name changed.

on run -- this runs when you start it, gets the start time, opens the new file, and writes a header for it.
	set start to current date
	set SD to short date string of start
	set PingLog to PingLogHead & SD
	set logFile to open for access (path to desktop as text) & PingLog & ".txt" with write permission
	try
		write "Ping Log for " & PingAddr & " on " & SD & return & return to logFile
	on error e
		display dialog e
		close access logFile
	end try
end run

on idle -- this runs every 'howOften', pings, and writes the time and true if up, false if not.
	set P to count paragraphs of (do shell script "ping -c1 " & PingAddr)
	set pingBack to P > 5 -- more than five paragraphs is true, site is up.
	try
		write my getTime() & ": " & pingBack & return to logFile
	on error e
		display dialog e
		close access logFile
	end try
	if (current date) - start > howLong then quit -- this line quits the application after 'HowLong"
	return HowOften
end idle

on quit -- this writes the quit time and closes the file
	write return & "----- PingLogger quit at " & my getTime() & " -----" to logFile
	close access logFile
	continue quit
end quit

to getTime()
	tell (current date) to set {hr, mn, AP} to {its hours, its minutes, text -2 thru -1 of (it as string)}
	return hr & ":" & mn & " " & AP as string
end getTime

[Edited to include much shorter and simpler time stamp]

you could set up a cron
with
*/5 * * * * ping -c1 000.000.000.000 > /ping_log.txt