Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > hostname <-> ip address

Reply
Thread Tools

hostname <-> ip address

 
 
Ani
Guest
Posts: n/a
 
      07-19-2006
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

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-19-2006
Ani <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
Ani
Guest
Posts: n/a
 
      07-19-2006
Thanks Anno!

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

~ Ani

(E-Mail Removed)-berlin.de wrote:
> Ani <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > 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


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      07-19-2006
[please don't top-post. quoting fixed]

Quoth "Ani" <(E-Mail Removed)>:
> (E-Mail Removed)-berlin.de wrote:
> > Ani <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > > 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

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit.|(E-Mail Removed)
Musica vel ipsas arbores et horridas movet feras. |
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to let other people know my hostname through IP address dxuranus Java 6 07-27-2006 10:47 AM
Get hostname, when IP address is known vi Java 5 09-19-2005 10:36 PM
Trouble using a hostname in the address field for the Radius client in IAS Ned Cisco 0 08-11-2005 02:59 PM
RMI: take ip/hostname what client was using and give it back as aremote objects hostname AWieminer Java 0 07-12-2005 08:05 PM
linux tcp/ip: How to reverse resolve a hostname from an ip address? Tim Veldhuizen C++ 2 07-24-2003 09:34 AM



Advertisments