home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 4 / 4034 / nslook.pl < prev   
Encoding:
Perl Script  |  1991-09-11  |  1.6 KB  |  92 lines

  1. eval 'exec perl -s -S $0 ${1+"$@"}'
  2.     if 0;
  3.  
  4. #-------
  5. # nslook.pl - simple name server lookup
  6. #
  7. # Usage:
  8. #    nslook [-l] [hostname|IP address] ...\n";
  9. #
  10. # Options:
  11. #    -l    long format output
  12. #
  13. # Idea from a program of the same name posted in alt.sources
  14. # by Juergen Nickelsen <nickel@cs.tu-berlin.de>, 10 Sep 91.
  15. #
  16. # DaviD W. Sanderson
  17. #-------
  18.  
  19. # require 'sys/socket.ph';    # use this if you have to...
  20. sub AF_INET {2;}
  21.  
  22. #-------
  23. # These convert between the decimal quartet and the internal form of
  24. # the internet addresses.
  25. #-------
  26. sub inet2str
  27. {
  28.     sprintf('%u.%u.%u.%u', unpack('C4', $_[0]));
  29. }
  30. sub str2inet
  31. {
  32.     $_[0] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  33.     pack('C4', $1, $2, $3, $4);
  34. }
  35.  
  36. #-------
  37. # Return a description of the results of a gethost* function.
  38. #-------
  39. sub HostDesc
  40. {
  41.     local    ($name, $aliases, $addrtype, $length, @addrs) = @_;
  42.     local    ($desc);
  43.  
  44.     $desc .= 'Name:    '. $name.    "\n"    if $name ne '';
  45.     $desc .= 'Alias:   '. $aliases. "\n"    if $aliases ne '';
  46.  
  47.     foreach (@addrs)
  48.     {
  49.         $desc .= 'Address: '. &inet2str($_). "\n";
  50.     }
  51.  
  52.     $desc;
  53. }
  54.  
  55. #-------
  56. # Look up the address or hostname in $_.
  57. #-------
  58. sub DoArg
  59. {
  60.     local(@ans);
  61.     local($ans);
  62.  
  63.     if(/^\d+\.\d+\.\d+\.\d+$/)
  64.     {
  65.         @ans = gethostbyaddr(&str2inet($_), &AF_INET);
  66.         die "$0: $_: unknown address\n"    if !defined @ans;
  67.         $ans = $ans[0];
  68.     }
  69.     else
  70.     {
  71.         @ans = gethostbyname($_);
  72.         die "$0: $_: unknown name\n"    if !defined @ans;
  73.         $ans = &inet2str($ans[4]);
  74.     }
  75.     $ans .= "\n";
  76.     $ans =    &HostDesc(@ans)    if $l ne '';
  77.     $ans;
  78. }
  79.  
  80. $0 =~ s#.*/##;
  81.  
  82. if ($#ARGV < $[)
  83. {
  84.     print "usage: $0 [-l] [hostname|IP address] ...\n";
  85.     exit 0;
  86. }
  87.  
  88. for (@ARGV)
  89. {
  90.     print &DoArg;
  91. }
  92.