Using Ping command

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

Using Ping command

Post by derr1ck » Wed May 30, 2012 8:40 pm

Hi,

I need to be able to ping a domain form within my desktop program, I have tried using :-


put shell ("ping mydomain.com") into field 1

Put this doesn't work and it seems to lockup livecode and I need to force quit the program on my Mac.

Any ideas on how I can do this, would like to check if a website running or not and have a colour bar represent its state.

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Using Ping command

Post by Klaus » Wed May 30, 2012 8:58 pm

Hi Derrick,

one thing to know: SHELL calls in LiveCode are BLOCKING!

Means Livecode will wait until the shell command has finished,
which of course looks as if the app has crashed!

Sorry, no idea how to do this non-blocking.


Best

Klaus

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Using Ping command

Post by sturgis » Wed May 30, 2012 9:02 pm

If you're on windows it should return after 4 pings. If you're on ox x I believe ping continues indefinitely. To fix this use the -c switch

put shell("ping -c4 yourdomain.to.check.com") into field 1 -- will send 4 pings -c = count

derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

Re: Using Ping command

Post by derr1ck » Wed May 30, 2012 11:22 pm

sturgis wrote:If you're on windows it should return after 4 pings. If you're on ox x I believe ping continues indefinitely. To fix this use the -c switch

put shell("ping -c4 yourdomain.to.check.com") into field 1 -- will send 4 pings -c = count

Thanks that seems to have worked :)

derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

Re: Using Ping command

Post by derr1ck » Fri Jun 01, 2012 8:54 am

One other question:

How can I have the script repeat the ping command, say every 5 mins?

so far, via a button I check it by doing:-

set the backgroundColor of graphic monitor to "white"
put shell ("ping -c2 mydomain.com") into results
put item 2 of results into testit
if testit is empty then
set the backgroundColor of graphic monitor to "red"
else set the backgroundColor of graphic monitor to "green"

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Using Ping command

Post by shaosean » Fri Jun 01, 2012 9:33 am

If you take your code out of the mouseUp handler and create a new one, you can just use the send command to call the handler every five minutes

Code: Select all

on mouseUp
  doMyPing
end mouseUp

command doMyPing
  set the backgroundColor of graphic monitor to "white"
  put shell ("ping -c2 mydomain.com") into results
  put item 2 of results into testit
  if testit is empty then 
  set the backgroundColor of graphic monitor to "red"
  else set the backgroundColor of graphic monitor to "green"
  -- you will need a check here to see if you need to send the command or not
  send "doMyPing" to me in 300 seconds
end doMyPing

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm
Location: Seattle, USA

Re: Using Ping command

Post by townsend » Fri Jun 01, 2012 3:11 pm

Any ideas on how I can do this, would like to check if a website running or not and have a colour bar represent its state.
Ping actually returns a trace route. If you just want to check if the website is up and running maybe you could just try grabbing a page.

Code: Select all

put URL("http://www.somewebsite.com/") into a.variable

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Using Ping command

Post by sturgis » Fri Jun 01, 2012 3:15 pm

This is a good point. Pinging the machine shows that its there and alive, but that doesn't necessarily mean apache (or whatever webserver they're using) is actually up and working. Hitting a page is probably the better option.
townsend wrote:
Any ideas on how I can do this, would like to check if a website running or not and have a colour bar represent its state.
Ping actually returns a trace route. If you just want to check if the website is up and running maybe you could just try grabbing a page.

Code: Select all

put URL("http://www.somewebsite.com/") into a.variable

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Using Ping command

Post by shaosean » Fri Jun 01, 2012 11:42 pm

Of course some people do run servers that are not web servers, so just knowing that you can reach the darn thing is always good too ;-)

ninetrees
Posts: 7
Joined: Thu Apr 22, 2010 6:54 am

Re: Using Ping command, time response

Post by ninetrees » Wed Dec 05, 2012 8:33 am

Any ideas on how I can do this, would like to check if a website running or not and have a colour bar represent its state.
I modified the code of "shaosean" (Thanks!) to report the average time for the ping command to a particular site. (apple.com gives the lowest ping time I've ever found.)

Code: Select all

command pingaSite
   put shell ("ping -c4 -q -t4 www.apple.com") into results -- 4 pings, quiet (minimal) response, timeout 4 sec
   put item 2 of results into s
   if s is empty then 
      report "no response"
   else -- parse the line: round-trip min/avg/max/stddev = 21.527/25.327/35.363/5.805 ms (item 2 of word 4)
      put word 4 of the last line of results into s
      set the itemdelimiter to "/"
      put  "Ave Ping: " && round(item 2 of s,1) && "ms"  into field "pingTime" -- n/n/n/n ms
   end if
 end pingaSite

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”