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

  1. Xref: sparky comp.unix.programmer:5717 comp.sources.wanted:5405 comp.lang.perl:7460
  2. Path: sparky!uunet!think.com!ames!purdue!not-for-mail
  3. From: spaf@cs.purdue.edu (Gene Spafford)
  4. Newsgroups: comp.unix.programmer,comp.sources.wanted,comp.lang.perl
  5. Subject: Re: Get hostname from IP address
  6. Date: 16 Dec 1992 13:36:04 -0500
  7. Organization: Department of Computer Sciences, Purdue University
  8. Lines: 68
  9. Message-ID: <1gnsukINN1hk@uther.cs.purdue.edu>
  10. References: <Bz2FFM.D4z@avalon.nwc.navy.mil>
  11.     <1992Dec11.152438.18365@fwi.uva.nl> <1992Dec13.122835.28103@hyphen.com>
  12.     <1992Dec13.180845.28960@hyphen.com>
  13.     <1gj9afINN47k@mimas.cc.deakin.OZ.AU>
  14.     <1992Dec16.015357.10437@hyphen.com>
  15. NNTP-Posting-Host: uther.cs.purdue.edu
  16. In-reply-to: dwight@hyphen.com's message of Wed, 16 Dec 1992 01:53:57 GMT
  17.  
  18. It took me all of 10 minutes to build this Perl program a while ago.
  19. It took me more time today to add comments for releasing it than it
  20. did to write it!
  21.  
  22. It is about 40 plus lines of code, but that is because I choose to
  23. write my programs to include error checking, print usage messages, and
  24. be readable. :-)  
  25.  
  26. Something to take an IP number and turn it into a hostname can be done
  27. on the command line like so:
  28. perl -le 'print ((gethostbyaddr(pack("C4", split(/\./, shift)), 2))[0])' ip-#-arg
  29. (this assumes that AF_INET == 2 on your machine).
  30. This is not as general, nor is it as good form as what is below (I believe).
  31.  
  32.  
  33. #!/usr/local/perl/perl
  34. #  "canon" program.  E. Spafford <spaf@cs.purdue.edu>
  35. #
  36.  
  37. require "getopts.pl";
  38. require "sys/socket.ph";
  39.  
  40. $usage = "usage: canon [-a] <name|#>\n";
  41.  
  42. &Getopts("ah") || die $usage;    # make sure we have proper options
  43. $host = shift || die $usage;    # get a hostname/number
  44.  
  45. @a = ($host =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);    # IP number?
  46.  
  47. if (@a) {
  48.     ($A, $junk, $junk, $junk, @addrs) = 
  49.     gethostbyaddr(pack('C4', @a), &AF_INET);
  50. } else {
  51.     ($A, $junk, $junk, $junk, @addrs) = gethostbyname($host);
  52. }
  53.  
  54. $A || &oops($?);
  55.  
  56. if ($opt_a) {
  57.     foreach $addr (@addrs) {
  58.     print join('.', unpack(C4, $addr)) . "\n";
  59.     }
  60. } else {
  61.     print "$A\n";
  62. }
  63.  
  64.  
  65. sub oops {
  66.    local($herr, %errm) = @_;
  67.    require "netdb.ph";
  68.  
  69.    %errm = (
  70.     &HOST_NOT_FOUND, "Host $host not found (authoritive)",
  71.     &TRY_AGAIN, "Temporary failure looking up $host -- try again later",
  72.     &NO_RECOVERY, "Non-recoverable error looking up $host",
  73.     &NO_DATA, "$host is a valid name but no data is available"
  74.            );
  75.  
  76.    die "$0: " . (defined($errm{$herr}) ? $errm{$herr} : 
  77.     "Unknown error ($herr)") . ".\n";
  78. }
  79.  
  80. -- 
  81. Gene Spafford
  82. Software Engineering Research Center & Dept. of Computer Sciences
  83. Purdue University, W. Lafayette IN 47907-1398
  84. Internet:  spaf@cs.purdue.edu    phone:  (317) 494-7825
  85.