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




anshumaEngineeringAsked:
Who is Participating?
 
Perl_DiverCommented:
basci concept if you use the DOS ping command:

my @p = grep{!/^\s*$/} qx/ping hostname -n 1/;
my ($h,$i) = $p[0] =~ /pinging\s+(\S+)\s+\[([^\]]+)]/i;    
print "$h $i\n";
0
 
mish33Commented:
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;
}
0
 
anshumaEngineeringAuthor Commented:
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Perl_DiverCommented:
what is ipaddresses.pl, post the code.
0
 
anshumaEngineeringAuthor Commented:
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";
0
 
Perl_DiverCommented:
What does the file you want to use as input look like?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.