Re: Test host reachable
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Test host reachable

From: Ronnie Brunner <ronnie.brunner@netcetera.ch>
Date: Sun Feb 26 2006 - 19:18:34 CET

antoniofcano wrote:
> Hello,
>
> I'm making a little function that gets the remote and local IP of one
> host and need to know If I've to use the local or remote.
>
> I thought that one way of do this could be try to ping using the local
> first and if this fail then take the remote. But how can I do the ping?

You can always [exec ping] and check if that works, but it's probably
not what you want. Bear in mind that depending on your setup, a host may
not respond to a ping but still be able to serve your request (aussuming
that this is what you ultimately want: choose one or the other host to
request something)

Why not just opening a socket to your local host on the correct port and
on failure connect to the remote host?

Something like (not nice code, as you want it to be configurable and to
work with a couple of possible hosts etc etc ... but you should get the
picture):

if {[catch {set mysock [socket $host1 $port1]} msg]} {
   puts stderr "cannot connect to $host1: $msg
   if {[catch {set mysock [socket $host2 $port2] msg]} {
     pust stderr "Oops. Cannot connect to $host2 either: $msg"
     exit 1
   }
}
# work with $mysock
close $mysock

hth
Ronnie
Received on Sun Apr 30 02:16:08 2006