hostname <-> ip address

Discussion in 'Perl Misc' started by Ani, Jul 19, 2006.

  1. Ani

    Ani Guest

    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
     
    Ani, Jul 19, 2006
    #1
    1. Advertising

  2. Ani

    -berlin.de Guest

    Ani <> 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
     
    -berlin.de, Jul 19, 2006
    #2
    1. Advertising

  3. Ani

    Ani Guest

    Thanks Anno!

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

    ~ Ani

    -berlin.de wrote:
    > Ani <> 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
     
    Ani, Jul 19, 2006
    #3
  4. Ani

    Ben Morrow Guest

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

    Quoth "Ani" <>:
    > -berlin.de wrote:
    > > Ani <> 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.|
    Musica vel ipsas arbores et horridas movet feras. |
     
    Ben Morrow, Jul 19, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AWieminer
    Replies:
    0
    Views:
    758
    AWieminer
    Jul 12, 2005
  2. vi
    Replies:
    5
    Views:
    16,858
    Roedy Green
    Sep 19, 2005
  3. Tim Veldhuizen
    Replies:
    2
    Views:
    1,006
    Tim Veldhuizen
    Jul 24, 2003
  4. dxuranus
    Replies:
    6
    Views:
    4,362
    dxuranus
    Jul 27, 2006
  5. Microlong®
    Replies:
    3
    Views:
    188
    Evertjan.
    Oct 12, 2005
Loading...

Share This Page