hostname <-> ip address

A

Ani

Hi All,
If I use the following script to get ip address if i supply a hostname
& vice-versa:
---------------------------------------------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

use Socket qw(AF_INET);

usage() if $#ARGV == -1;
display_info( @ARGV );

sub display_info {
foreach (shift) {
my ($ip, $host, $aliases, $addrtype, $length, @addrs);
$ip = $_;
if ( /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ ) {
print "IP is $ip\n";
($host, $aliases, $addrtype, $length, @addrs) =
gethostbyaddr( pack( 'C4', $1, $2, $3, $4 ), AF_INET );
die "Reverse lookup failed to find name for $ip\n" unless $host;
}
$host = $ip unless $host;
print "Hostname is $host\n";
($host, $aliases, $addrtype, $length, @addrs) = gethostbyname(
$host );
die "Lookup failed to find address for $host\n" unless @addrs;
print "Maps to these IPs:\n";
foreach (@addrs) {
print "IP: ".join( '.', unpack( 'C4', $_ ) )."\n";
}
}
}

sub usage {
print STDERR <<EOM;
Usage: getdnsinfo.pl <IP|host>...
Example `getdnsinfo.pl www.interarchy.com'
EOM
exit( 0 );
}
---------------------------------------------------------------------------

When I execute with the following cmdline:

$ perl getdnsinfo.pl netscape.com

then I get the following output:

Hostname is netscape.com
Maps to these IPs:
IP: 152.163.211.51

but when I execute with the ip address I got from this output:

$ perl getdnsinfo.pl 152.163.211.51

then I am getting the following output:

Hostname is nscp-rtc-vipb.websys.aol.com
Maps to these IPs:
IP: 152.163.211.51

I am unable to get the original hostname 'netscape.com' instead of
'nscp-rtc-vipb.websys.aol.com'.
How can I get the original hostname?

Any help will be highly appreciated.

~ Ani
 
A

anno4000

Ani said:
Hi All,
If I use the following script to get ip address if i supply a hostname
& vice-versa:
[...]

When I execute with the following cmdline:

$ perl getdnsinfo.pl netscape.com

then I get the following output:

Hostname is netscape.com
Maps to these IPs:
IP: 152.163.211.51

but when I execute with the ip address I got from this output:

$ perl getdnsinfo.pl 152.163.211.51

then I am getting the following output:

Hostname is nscp-rtc-vipb.websys.aol.com
Maps to these IPs:
IP: 152.163.211.51

I am unable to get the original hostname 'netscape.com' instead of
'nscp-rtc-vipb.websys.aol.com'.
How can I get the original hostname?

That has nothing to do with Perl, it's a question about DNS. An IP
address can be associated with one primary domain name and any number
of aliases. All aliases resolve to the same IP address. The IP
address resolves to the primary domain name. Possible aliases are
not delivered. At least that's how every DNS worked that I looked at.

IOW, your "original hostname" is not the primary host name but an
alias. You won't get it back from gethostbyaddr() (and neither
from gethostbyname()).

Anno
 
A

Ani

Thanks Anno!

Is it possible to get the list of aliases associated with an IP
address?

~ Ani

Ani said:
Hi All,
If I use the following script to get ip address if i supply a hostname
& vice-versa:
[...]

When I execute with the following cmdline:

$ perl getdnsinfo.pl netscape.com

then I get the following output:

Hostname is netscape.com
Maps to these IPs:
IP: 152.163.211.51

but when I execute with the ip address I got from this output:

$ perl getdnsinfo.pl 152.163.211.51

then I am getting the following output:

Hostname is nscp-rtc-vipb.websys.aol.com
Maps to these IPs:
IP: 152.163.211.51

I am unable to get the original hostname 'netscape.com' instead of
'nscp-rtc-vipb.websys.aol.com'.
How can I get the original hostname?

That has nothing to do with Perl, it's a question about DNS. An IP
address can be associated with one primary domain name and any number
of aliases. All aliases resolve to the same IP address. The IP
address resolves to the primary domain name. Possible aliases are
not delivered. At least that's how every DNS worked that I looked at.

IOW, your "original hostname" is not the primary host name but an
alias. You won't get it back from gethostbyaddr() (and neither
from gethostbyname()).

Anno
 
B

Ben Morrow

[please don't top-post. quoting fixed]

Quoth "Ani said:
Ani said:
Hi All,
If I use the following script to get ip address if i supply a hostname
& vice-versa:
[...]

When I execute with the following cmdline:

$ perl getdnsinfo.pl netscape.com

then I get the following output:

Hostname is netscape.com
Maps to these IPs:
IP: 152.163.211.51

but when I execute with the ip address I got from this output:

$ perl getdnsinfo.pl 152.163.211.51

then I am getting the following output:

Hostname is nscp-rtc-vipb.websys.aol.com
Maps to these IPs:
IP: 152.163.211.51

I am unable to get the original hostname 'netscape.com' instead of
'nscp-rtc-vipb.websys.aol.com'.
How can I get the original hostname?

That has nothing to do with Perl, it's a question about DNS. An IP
address can be associated with one primary domain name and any number
of aliases. All aliases resolve to the same IP address. The IP
address resolves to the primary domain name. Possible aliases are
not delivered. At least that's how every DNS worked that I looked at.

IOW, your "original hostname" is not the primary host name but an
alias. You won't get it back from gethostbyaddr() (and neither
from gethostbyname()).

Is it possible to get the list of aliases associated with an IP
address?

No, it's not.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top