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

hi guys, is there anyway of getting a list of ip addresses of machines on a network in perl? i'm trying to create a script that makes a file available to certain machines on a network. i will specify the machines using a file. i'm trying to create a perl module and a script which will actually handle the copying of the files as well :o( thanks

Replies are listed 'Best First'.
Re: IP addresses
by Zaxo (Archbishop) on May 19, 2005 at 02:27 UTC

    How to enumerate hosts on your network depends on how much information you have to start with, and what kind of network it is. In a pinch, you can use Net::Ping for a scan, but you may be able to use gethostbyaddr with better effect. For copying files to other hosts, look at Net::SCP.

    After Compline,
    Zaxo

Re: IP addresses
by davido (Cardinal) on May 19, 2005 at 02:39 UTC

    Depending on your network's topology and complexity, you might find it most reliable to query your router. I have a fairly inexpensive Netgear router that has various configuration web pages, one of which offers a list of assigned IP's and their names. Just screen scrape the router's attached-devices page, and process the results accordingly.

    To do it this way, assuming your router serves a web configuration page, just look at using WWW::Mechanize, or LWP::UserAgent (from which WWW::Mechanize inherits.


    Dave

Re: IP addresses
by jpeg (Chaplain) on May 19, 2005 at 02:40 UTC
    Sure. Net::Ping seems to be the way to go. Tons of examples are available in super search. Something like
    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Net::Ping; my $i; my $host; my $p = Net::Ping->new("syn"); $p -> bind("10.90.1.27"); for( $i = 0; $i <= 254; $i++) { $host = "10.90.1.$i"; $p -> ping("$host", 2); print("$host\n") if pingecho($host); }
    is a slow but functional sweeper. Edit your subnet and local host as necessary.
    In addition, you might find How do I gather files from remote machines? useful.
    --
    jpg
Re: IP addresses
by wazoox (Prior) on May 19, 2005 at 11:09 UTC
    Wouldn't it be nice to have a nmap module? mmmmh, something new for my todo list :)
Re: IP addresses
by jwest (Friar) on May 19, 2005 at 14:32 UTC
    Generally, to determine the list of IP addresses associated with a given network, you should consider using Net::Netmask:

    use strict; use warnings; use Net::Netmask; my $spec = ...; my $block = Net::Netmask->new2($spec) or die "bad spec"; my $base = $block->base; my $bcast = $block->broadcast; my @ip = $block->enumerate; for (@ip) { next if @ip > 1 and $_ eq $base; next if @ip > 1 and $_ eq $bcast; ... }
    See the perldoc for Net::Netmask to see how the network block may be specified. You'll find that it accepts the most intuitive methods.

    --jwest


    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7