Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

DNS Resolution4

by muaddib2 (Novice)
on Apr 25, 2001 at 02:38 UTC ( [id://75300]=perlquestion: print w/replies, xml ) Need Help??

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

#/usr/bin/perl use warnings; use Socket; print "Ip address "; my $ipaddr = <STDIN>; $peer_host = gethostbyaddr($ipaddr, AF_INET); $peer_addr = inet_ntoa($peer_host); print $peer_addr;
This doesn't seem to work and I am not sure why I am getting syntax error

Replies are listed 'Best First'.
Re: DNS Resolution4
by tinman (Curate) on Apr 25, 2001 at 02:58 UTC

    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

Re: DNS Resolution4
by hdp (Beadle) on Apr 25, 2001 at 03:45 UTC
    Your code doesn't work because you're passing a hostname to inet_ntoa; it expects an ip address packed into four characters.

    I think you probably misread this example:

    ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle)); $peer_host = gethostbyaddr($iaddr, AF_INET); $peer_addr = inet_ntoa($iaddr);
    Note that $iaddr, not the return value of gethostbyaddr, is passed to inet_ntoa. What you want can be reduced to a single line:
    (gethostbyaddr(gethostbyname($ipaddr), AF_INET))[0]
    hdp.

    p.s. chomp $ipaddr or it'll continue to fail, but in a new and interesting way!

(boo)Re: DNS Resolution4
by boo_radley (Parson) on Apr 25, 2001 at 02:53 UTC
    well, from the errors that you described, $peer_host is blank.
    since peer_host is set with gethostbyaddr($ipaddr, AF_INET), I changed that line to $peer_host = gethostbyaddr($ipaddr, AF_INET) || die "$!"; so I could see what happens when it dies.
    unfortunately, it just returned "unknown error", so that's not much help.
    I think this may be due to a win32 problem, or something similar. Have you considered checking out IO::Sockets?
      Is IO::Sockets the same syntax
Re: DNS Resolution4
by KM (Priest) on Apr 25, 2001 at 21:34 UTC
    I use this little script for checking IPs I get in SPAM headers to make sure I email the correct abuse@* addresses:

    #!/usr/bin/perl -w use strict; use Socket; my $ip = $ARGV[0] || undef; unless ($ip) { print "What is the IP?\n"; $ip = <STDIN>; chomp $ip; } die "No IP given\n" if !$ip; print "Finding host for $ip...\n"; my $iaddr = inet_aton($ip); my $name = gethostbyaddr($iaddr, AF_INET) || "NO DATA"; print "$ip is $name\n";

    Cheers,
    KM

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://75300]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-16 17:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found