Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

script to grab ip addresses

Hi,

I need to find the ip addresses of almost 50 machines. So I need to open a dos window and

issue ping hostname and then I get the reply form where I can get the ip address
like this
________________________________________________________________
C:\>ping hostname

Pinging HOSTNAME.corp.EXAMPLE.com [10.555.55.55] with 32 bytes of data:

Reply from 10.555.55.55: bytes=32 time=2ms TTL=122
Reply from 10.555.55.55: bytes=32 time=3ms TTL=122
Reply from 10.555.55.55: bytes=32 time=6ms TTL=122
Reply from 10.555.55.55: bytes=32 time=6ms TTL=122

Ping statistics for 10.555.55.55:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 2ms, Maximum = 6ms, Average = 4ms

________________________________________________________________

Now could somebody write a perl script for me which will be

perl inputfilename outputfilename

int the input file I will put all the hostnames

In the output file it should have the output as

hostname ipaddress
hostname 10.555.55.55




Avatar of mish33
mish33
Flag of United States of America image

You don't need ping. Install http://search.cpan.org/CPAN/authors/id/D/DA/DARREN/Net-Nslookup-1.16.tar.gz

use Net::Nslookup;
foreach $host (<>) {
  print $host, nslookup $host;
}
ASKER CERTIFIED SOLUTION
Avatar of Perl_Diver
Perl_Diver

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Amit

ASKER

Hello Perl Diver,

I issued

perl C:\Perl\scripts\ipaddresses.pl C:\Perl\scripts\hostname.txt

but nothing seems to be working. I am not familiar with perl, could you please modify this code so that it can take a file as input

thanks
Avatar of Perl_Diver
Perl_Diver

what is ipaddresses.pl, post the code.
Avatar of Amit

ASKER

I copied the code you posted in a file and named it ipaddresses.pl . So the code inside it is

my @p = grep{!/^\s*$/} qx/ping hostname -n 1/;
my ($h,$i) = $p[0] =~ /pinging\s+(\S+)\s+\[([^\]]+)]/i;    
print "$h $i\n";
What does the file you want to use as input look like?