Check for net connection - net-ping?

Started by cormullion, February 26, 2006, 04:10:32 AM

Previous topic - Next topic

cormullion

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


> (net-ping "newlisp.org")
nil


although this works:



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

#1
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

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



> (net-error)
(1 "ERR: Cannot open socket")
>


You have to start newLISP in superuser mode, i.e. on Linux or Mac OS X try:



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

#3
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

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



~> 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:



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

#5
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

#6
I ended up resorting to some AppleScript...


(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...