Check for net connection - net-ping?

Q&A's, tips, howto's
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Check for net connection - net-ping?

Post by cormullion »

How do I check for a valid internet connection? I thought I could use net-ping but:

Code: Select all

> (net-ping "newlisp.org")
nil
although this works:

Code: Select all

> (exec {ping -c 1 "newlisp.org"})
("PING newlisp.org (66.235.209.72): 56 data bytes" "64 bytes from 66.235.209.72: icmp_seq=0 ttl=48 time=339.124 ms" 
 "" "--- newlisp.org ping statistics ---" "1 packets transmitted, 1 packets received, 0% packet loss" 
 "round-trip min/avg/max/stddev = 339.124/339.124/339.124/nan ms")
> 
Or is there a better way?

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

What exactly are your seeking? You like to check for a Layer-1/interface up or more for a remote system check?

Norman.
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

The rerturn value of 'nil' in 'net-ping' indicates a failure, use 'net-error' to find out what happened. Probably you get this:

Code: Select all

> (net-error)
(1 "ERR: Cannot open socket")
> 
You have to start newLISP in superuser mode, i.e. on Linux or Mac OS X try:

Code: Select all

~> sudo newlisp
Password:
newLISP v.8.8.0 on OSX UTF-8, execute 'newlisp -h' for more info.

> (net-ping "newlisp.org")
("66.235.209.72")
> 
> (net-ping '("newlisp.org" "yahoo.com") 5000)
("66.235.209.72" "66.94.234.13")
> 
Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I want to check whether my connection to "the Internet" has dropped (eg dial-up has disconnected after x hours), so that I can re-connect. So I want to write a check to see whether I have connectivity...

I see from the manual that, as you say, Lutz, I need to run as superuser to use this command, although I can do (exec "ping") instead. (Manual confuses with talk of net-peek, by the way...)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

The ping utility on your computer also runs in super-user mode. You can see this by doing:

Code: Select all

~> ls -l /sbin/ping
-r-sr-xr-x   1 root  wheel  33264 Mar 20  2005 /sbin/ping
~>
The 's' in '-r-sr-xr-x' indicates the super-user mode. The programs also must be owned by a super user or root. Then you don't need 'sudo' to start newLISP. You could try:

Code: Select all

sudo chmod +s /usr/bin/newlisp
As newlisp was installed in /usr/bin as 'root', setting the 's' flag should be enough. Now you can start newLISP as normal and 'net-ping' will work right away.

Lutz

ps: I updated the HTML and PDF versions of the manual to rev-3 correcting the 'net-ping' documentation (net-peek -> net-ping in the syntax description).

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

IMHO, "chmod +s newlisp" isn't a good idea either.

use (! (append "/bin/ping -c 1 -q " ip-address))
or , probably,, try something like nonblocking connect to port 80.
WBR, Dmi

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I ended up resorting to some AppleScript...

Code: Select all

(define (net-connection-status)
	(int (first (exec (string {osascript -e 'tell application "Internet Connect" to return state of status ' })))))
which returns 1 for connected, 0 for disconnected. Probably slower, but more elegant than using ping...

Locked