home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / programm / 5694 < prev    next >
Encoding:
Internet Message Format  |  1992-12-14  |  2.4 KB

  1. Xref: sparky comp.unix.programmer:5694 comp.sources.wanted:5371 comp.lang.perl:7422
  2. Path: sparky!uunet!munnari.oz.au!sol.deakin.OZ.AU!mimas.cc.deakin.OZ.AU!not-for-mail
  3. From: tim@deakin.OZ.AU (Tim Cook)
  4. Newsgroups: comp.unix.programmer,comp.sources.wanted,comp.lang.perl
  5. Subject: Re: Get hostname from IP address
  6. Date: 15 Dec 1992 11:36:31 +1100
  7. Organization: Deakin University
  8. Lines: 75
  9. Message-ID: <1gj9afINN47k@mimas.cc.deakin.OZ.AU>
  10. References: <Bz2FFM.D4z@avalon.nwc.navy.mil> <1992Dec11.152438.18365@fwi.uva.nl> <1992Dec13.122835.28103@hyphen.com> <1992Dec13.180845.28960@hyphen.com>
  11. NNTP-Posting-Host: mimas.cc.deakin.oz.au
  12.  
  13. dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
  14. >
  15. > I'd like to find a utility I can call from the command-line with an
  16. > internet IP address as an argument, and have it return to me the full
  17. > hostname related to it. I've looked into the function gethostbyaddr()
  18. > but haven't been able to work. I also remember reading about a name-
  19. > resolver funtion nres_gethostbyaddr() (sp?), but can't find any
  20. > references to it on my system. I'm running SunOS 4.1.2 on a Sparc.
  21.  
  22.  
  23. There Is Always A Simple Perl Script That Will Do It (tm).
  24.  
  25. The enclosed Perl script will convert a DNS hostname to an IP address
  26. or vice-versa.
  27.  
  28.  
  29. #!/usr/local/bin/perl
  30. # ipq -    Answer one of the two common IP queries
  31. #
  32. # Tim Cook, December 1992
  33.  
  34. $prog = substr ($0, rindex ($0, '/') + 1);
  35.  
  36.  
  37. if ($#ARGV != 0) {
  38.    die "usage: $prog ( hostname | IP-address )\n"; }
  39.  
  40.  
  41. if ($ARGV[0] =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) {
  42.    &address_to_name ($ARGV[0]); }
  43. else {
  44.    &name_to_address ($ARGV[0]); }
  45.  
  46. exit (0);
  47.  
  48.  
  49.  
  50. sub name_to_address {
  51.    local ($name) = shift (@_);
  52.  
  53.    local (@octets);
  54.  
  55.    local ($nam, $aliases, $addrtype, $length, $address) =
  56.       gethostbyname ($name); 
  57.  
  58.    if (! length ($address)) {
  59.       die "$prog: no address found for $name\n"; }
  60.  
  61.    @octets = unpack ("CCCC", $address);
  62.  
  63.    print (join ('.', @octets[0..3]), "\n");
  64.    }
  65.  
  66.    
  67. sub address_to_name {
  68.    local ($address) = shift (@_);
  69.  
  70.    local (@octets);
  71.    local ($name, $aliases, $type, $len, $addr);
  72.    local ($ip_number);
  73.  
  74.    @octets = split ('\.', $address) ;
  75.  
  76.    if ($#octets != 3) {
  77.       die "$prog: IP-address must have four octets\n"; }
  78.  
  79.    $ip_number = pack ("CCCC", @octets[0..3]);
  80.  
  81.    ($name, $aliases, $type, $len, $addr) = gethostbyaddr ($ip_number, 2) ;
  82.  
  83.    if ($name) {
  84.       print ($name, "\n") ; }
  85.    else {
  86.       die "$prog: no host-name found for $address\n"; }
  87.    }
  88.