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

hi monks, i am working on a script to get hostnames of all the systems in LAN. i written a script to ping all the systems in a LAN but how to capture the replay and get hostname corresponding to ipaddress whose ping replay we received. i tried to use Net::Hostent module, but its not even getting installed in Win32 platform( when i seached for it using PPM -search its saying no modules are found). please help. Thanks in advance.
  • Comment on How to get Remote system hostnames in LAN?

Replies are listed 'Best First'.
Re: How to get Remote system hostnames in LAN?
by saintmike (Vicar) on Jan 25, 2006 at 05:09 UTC
    Are you saying you want to transform a pinged IP address into a hostname? If so, use
    use Socket; my ($addr) = inet_aton $ip_address_as_string; print scalar gethostbyaddr($addr, AF_INET), "\n";
      for gethostbyaddr to work, every IP address in the LAN must have a corresponding entry in the DNS. it is called reverse DNS lookup.

      the host from where your program runs, must know, where to find that information, so first it looks it up locally in a file ( c:\windows\system32\drivers\etc\hosts on Windows XP) and makes a query to a DNS server.

      so, if you are not running a DNS server in your LAN, you must edit your systems hosts file...

      :)))))
Re: How to get Remote system hostnames in LAN?
by asz (Pilgrim) on Jan 25, 2006 at 10:01 UTC
    if you want to capture the output from the ping command, in Windows XP, you might want to try something like this:
    use warnings; use strict; # capture the output from ping my $ping = qx( ping -n 4 127.0.0.1 ); # extract the values $ping =~ m/ .* Sent \s = \s (\d+) .* Lost \s = \s ([\d\.]+) \s \( ([\d\.]+) % \s loss .* Average \s = \s ([\d\.]+) ms .* /sx ; my $sent = $1; my $loss_no = $2; my $loss_proc = $3; my $average = $4; print "Lost $loss_no packets from $sent packets ( $loss_proc % ). ", "Average RTT $average ms.\n";
    you might want to take a look at the regular expressions tutorial.

    :)))))