CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
gethostbyaddr()

 



Jodrell
Deleted

Nov 30, 2000, 8:15 AM

Post #1 of 5 (367 views)
gethostbyaddr() Can't Post

 
I've been trying to get to grips with the entry for this function in perlfunc. I just don't get it.

All I want to do is resolve a host name from an IP address. What's the easiest way to do this?

Cheers.

------------------
http://www.custard.org/~jodrell


sleuth
Enthusiast / Moderator

Nov 30, 2000, 9:50 AM

Post #2 of 5 (367 views)
Re: gethostbyaddr() [In reply to] Can't Post

 
I found something that may help you,
http://www.perldoc.com/perl5.6/pod/func/getpeername.html#getpeername%20SOCKET

Sleuth


Jodrell
Deleted

Dec 1, 2000, 2:02 AM

Post #3 of 5 (367 views)
Re: gethostbyaddr() [In reply to] Can't Post

 
The snippet of code on that page isn't really useful in this context. I'm actually reading IP addresses from a log file, so I don't have an open socket to the host to work with.

The part I tried before I made the post was

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$hostname = gethostbyaddr($ipaddr, AF_INET);</pre><HR></BLOCKQUOTE>

which returns nothing. Is there an explanation enywhere of what the AF_INET bit means, and what other arguments this function accepts?

------------------
http://www.custard.org/~jodrell


Jodrell
Deleted

Dec 1, 2000, 2:20 AM

Post #4 of 5 (367 views)
Re: gethostbyaddr() [In reply to] Can't Post

Found a solution:

It uses the Net::hostent module which is an OO interface to the gethost* functions.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl
use Net::hostent;
use Socket;
use strict;

my $ipaddr = '10.0.0.1';

my $h = gethost($ipaddr);

my $hostname = $h->name;</pre><HR></BLOCKQUOTE>

You create a host object ($h) using gethost(), and can then get its hostname using the $h->name() method.

------------------
http://www.custard.org/~jodrell


Cure
User

Dec 3, 2000, 11:36 AM

Post #5 of 5 (367 views)
Re: gethostbyaddr() [In reply to] Can't Post

To produce an ADDR froma dot vector, say this -->

use Socket;
$ipaddr = inet_aton('IP');
$claimed_host = gethostbyaddr($ipaddr,AF_INET);

With the new version 5.6 of Perl you can skip the inter_aton and use the new v-string notation that was invented for version numbers bu happens to work for IP address as well.
--->

use Socket;
$ipaddr = vIP_adrress;
$claimed_host = gethostbyaddr($ipaddr,AF_INET);

Cure

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives