Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fetching Network Info

Status
Not open for further replies.
May 3, 2003
90
CA
Hy guys,

My question is simple. How do I retreive the "host name" from an IP address ?

I have tried gethostbyname, gethostbyaddr, getpeername and so on..

Either Im not doing it right or Im just not using the function or module.

Please help me out, all the help is very appreciated.

Thanks in advance.
 
Are you using DNS? What OS? If the IP is on DNS, nslookup <ip addr>. There is also a dig command(solaris for sure).
 
Well Im on a Win2k Server. Im not using DNS. I want to be able to translate the host name from a ip address.

I am able to do the opposite get the ip from host name like this:

my $addr = &quot;192.168.1.254&quot;;

my $name = gethostbyname($addr);

my ($val1, $val2, $val3, $val4) = unpack('C4', $name);

print &quot;$val1.$val2.$val3.$val4&quot;;


I think I sould be using &quot;gethostbyaddr&quot;. But I cant seem to be able to use it right. Can anyone help me out?

thanks,
 
22,

You've got to have DNS, otherwise you wont be able to resolve names into ip addresses.

Cheers
Paul
 
(For my second post , the ip addr replace it with a name like google.ca, my mistake)


MissBarBell,

I have tried to use the gethostaddr.

like this;

my $host = &quot;192.168.1.254&quot;;

my $hn = gethostbyaddr($addr, AF_INET);

but what does AF_INET mean? or what am I suppose to put there. ALways give me &quot;$hn uninitialized&quot;.

Whats my problem? Can anyone point this out?

thanks,
 
&quot;AF_INET defines the internet class of addressing&quot;

This and many others are constants that are exported by Socket.pm and used to define behaviour. Unfortunately I don't know what they all mean and do, so you might have to do a bit of digging inside the module and on google to get some more info.

Barbie
Leader of Birmingham Perl Mongers
 
When I try using AF_INET, it tells me that barewords not allowed while strict is on.

So i put it in quotes and tell me AF_INET or PF_INET value in not numeric.

I am lost now.

Is it a word a put or a numeric value ??
 
This will not work:

my $addr = &quot;192.168.1.254&quot;;
my $name = gethostbyaddr($addr, AF_INET);

I have not quite figured out the problem yet, but it has to do with passing a scalar as a string to gethostbyaddr(). If you were to do the following it would work:


my $name = gethostbyaddr(192.168.1.254, AF_INET);

Also to use gethostbyaddr() you have to put a 'use Socket;' statement at the start of your code. And as for AF_INET, well, you have to use it since you are dealing with TCP/IP, otherwise you would use AF_UNIX which deals with unix domain sockets.

So to repeat in programming terms:

#This will work
#!/user/bin/perl
use Socket;
$client = gethostbyaddr(172.168.1.1, AF_INET); # get name
if ($client)
{
print $addr, &quot; : &quot;, $client, &quot;\n\a&quot;;
}

#This will not work
#!/user/bin/perl
use Socket;
$addr = '172.168.1.1';
$client = gethostbyaddr($addr, AF_INET); # get name
if ($client)
{
print $addr, &quot; : &quot;, $client, &quot;\n\a&quot;;
}

If someone knows of a way to change a scalar of string type to a type that gethostbyaddr() can use, let us know. I guess if I wasn't lazy I could look in Socket.pm.

Later

 
Well, after taking a minute after my first post I found the answer in this post:

thread219-533839 here is the way you need to do it:

$addr = '192.168.1.254';
$ip = inet_ntoa($addr);#<-- This is the key method
$hn = gethostbyaddr($ip, AF_INET);

From :

inet_aton HOSTNAME
Takes a string giving the name of a host, and translates that to the 4-byte string (structure). Takes arguments of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name cannot be resolved, returns undef. For multi-homed hosts (hosts with more than one address), the first address found is returned.

Basically it takes your string and translates it to structure that gethostbyaddr() needs to work correctly in the above case.

Hope I helped, later.
 
ARRRGH, Scratch the last post. It doesn't work. Plust I had inet_ntoa written and gave a def for inet_aton. But, I do believe the problem lies in using one of those methods. I will work awhile longer and see what I find. Sorry for all the wasted space.
 
All right, this is my last post on this issue. This will work:

$addr = '172.17.141.1';#Can be any IP address
$ip = inet_aton($addr);#Still the key to success
$client = gethostbyaddr($ip, AF_INET); # get client name
if ($client){print $addr, &quot; : &quot;, $client, &quot;\n&quot;;}#For giggles

O.K. so I was correct earlier you need to use inet_aton(). This will change the ip network byte order (ie, 1011101....). Then once you have this byte string you can send it to gethostbyaddr(). Wooo, I am glad I finally got it.

Also good info on this page:

 
Feeble, this last post worked indeed correctly. I've got a different problem. I was doing some logging of users browsing different sites and I was wondering if I could use this to see to which sites they are going.

I've got the logging and filtering down. Now I got a list (a hash actually) of IP addresses which I want to check and get the domain / name.

By using you're script in my script I got this:

foreach (sort keys %ip)
{
if( $ip{$_} >1000 ) {
$ip = inet_aton($_);
$client = gethostbyaddr($ip, AF_INET);
if ($client)
{
print MYOUT &quot;$_ \t $ip{$_} \t $client \n&quot;;
}
}
}
close MYOUT;

The result of this gives me:

D:\Perl\Homework\Perl Scripting\Handy>tracereport.pl
195.121.244.198 : content55-2.wxs.nl

While the actual domain name is planetinternet.nl.

Is there some kind of additional resolving which needs te be done?

Please help me ;-)

- Raenius



&quot;Free will...is an illusion&quot;
 
Raenius,

It could be that there are a number of sites on the one host, because of Virtual Hosting

Try running a traceroute against both domains. Sometimes a gateway machine will forward a client to one of a number of servers for round robin load balancing, or even some funky version thereof

--Paul
 
Thanks Paul I figured as much with Virtual Hosting. The script does the job well enough now..

- Raenius

&quot;Free will...is an illusion&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top