This works for me on Win32 ActivePerl
#/usr/bin/perl -w
use strict;
use Socket;
my $host = "www.yahoo.com";
my @addresses = gethostbyname($host) or die "Can't resolve $host: $!
+\n";
@addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
print "The next set is @addresses \n";
No credit to me.. this is a snippet that I got from some book or the other.. but I use it and it works for me
The following is the complete code for both ways..
#/usr/bin/perl -w
use strict;
use Socket;
my $host = "www.yahoo.com";
my $packed_ip = gethostbyname($host)
or die "Unable to resolve '$host'\n";
my $ipaddr = inet_ntoa($packed_ip);
print "$ipaddr for $host\n";
my $packed_add = gethostbyaddr($ipaddr, AF_INET);
my $hostname = inet_aton($packed_add) or die "Can't reverse lookup \n"
+;
print "The host name for $ipaddr is $hostname \n";
my $name = "www.perl.com";
my @addresses = gethostbyname($host) or die "Can't resolve $name: $!
+\n";
@addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
print "The next set is @addresses \n";
Try this posting for a much easier way of doing it.. Net::DNS
Update: Fixed tyop :)
Update 2: Gave both forward and reverse lookup code
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|