Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Testing for live IP's

by Elijah_A (Novice)
on Jul 30, 2004 at 06:51 UTC ( [id://378608]=perlquestion: print w/replies, xml ) Need Help??

Elijah_A has asked for the wisdom of the Perl Monks concerning the following question:

I've tried ping but it's not very reliable for IP's having firewalls, the ping will never come back and the script will erroneously report that the IP is down but is actually a 'live' one. It's the simplest to make though:
#!/usr/bin/perl # IP adress or hostname of the remote machine to test $remote = "192.168.0.2"; if(system("ping -c 1 $remote 1> /dev/null")) { print "$remote is down\n"; } else { print "$remote is up\n"; }
how to do this in traceroute? I have no clue ... anyone got any ideas?? any alternative solutions for this??


Elijah

Replies are listed 'Best First'.
Re: Testing for live IP's
by inman (Curate) on Jul 30, 2004 at 09:06 UTC
    Any system that has a personal firewall will just pretend not to be there unless it has been configured to open a specific port. In addition, a security conscious network administrators could block certain protocols that might be used by hackers to probe the network. Then you have the whole business of NAT (network address translation) where significant numbers of computers can hide behind a single IP address.

    You can make DNS requests to attempt to resolve fixed IP addresses that have been assigned hostnames. Working with dynamic IP addresses is going to be more difficult since these will be allocated on an as needed basis by a DHCP server.

    The problem with 'Testing for live IP addresses' is that it is typical hacker/virus behaviour and is guarded against by Network security tools.

Re: Testing for live IP's
by gaal (Parson) on Jul 30, 2004 at 07:28 UTC
    Take a look at hping, which can do traceroute-like analysis with "pings" that aren't really ICMP packets: TCP, UDP, whatever.
Re: Testing for live IP's
by shotgunefx (Parson) on Jul 30, 2004 at 07:48 UTC
    Depending on your needs, you're probably better off testing services (such as mail, http,dns, etc) as it's not that uncommon for a borked machine to respond to pings.


    -Lee

    "To be civilized is to deny one's nature."
      Well, actually the script I'm working on is a dns system. So naturally I'd use traceroute, but like in ping .... I find it easier for the command to just return 0 or 1(success or fail). Traceroute returns a LOT and I have no idea how to tell the script if the IP is alive or not using those results.

      @gaal: Thanks for the link, I'll see if my supervisor will allow me to install it on our testserver, then I'll see if it'll work.

        Actually, traceroute doesn't "return a lot", it returns 0 for success, and 1 for failure, like many programs. It does "output a lot", but that's easy to fix. See if this works for you:

        #!/usr/bin/perl use strict; use warnings; # IP adress or hostname of the remote machine to test my $remote = "192.168.0.2"; if(system("traceroute $remote 1> /dev/null 2>&1")) { print "$remote is down\n"; } else { print "$remote is up\n"; } __END__
Re: Testing for live IP's
by sunadmn (Curate) on Jul 30, 2004 at 13:06 UTC
    Most of the work has been done for you I would suggest using Net::Traceroute which should do what you need or just seach through CPAN and I am sure there are many others out there.
    SUNADMN
    USE PERL
Re: Testing for live IP's
by roju (Friar) on Jul 30, 2004 at 14:25 UTC

    Since no-one beat me to it, look at nmap. It was designed to do this sort of thing. nmap -sP $HOST as root will do an ICMP ping, a TCP port 80 ping, and a SYN ping.

    Plus, you can use it from perl!

Re: Testing for live IP's
by davido (Cardinal) on Jul 30, 2004 at 15:42 UTC
    Consider my simple case as an argument for why it's not entirely possible to do what you're asking:

    • I use a cable internet connection with a DCHP-assigned dynamic IP address. ...no telling what it will be.
    • My router, connected to the cable modem, has a built in firewall. Ping requests are dropped by default. Also, ports are closed unless someone on my side of the network makes a request on a specific port. By closed, I mean asleep; they won't even respond, so there's no way of telling that they're even there.
    • Even if I turned on 'ping' response, the router may be turned on while no machines on my network are on. That means that you'll get a ping response, even though my network is essentially shut down.
    • My router / firewall's external web configuration interface is disabled by default, so there's no way to change its settings from the outside.

    So how can you ever get anything useful from that setup, unless I'm specifically broadcasting to your script or intentionally opening a port? You really can't. And the fact is, most of what I've described is the default behavior of a $50 dollar router.


    Dave

Re: Testing for live IP's
by Elijah_A (Novice) on Aug 02, 2004 at 00:38 UTC
    The script works! Thanks a lot =) it takes too long for traceroute to end it's job though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://378608]
Approved by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found