home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / getnet < prev    next >
Encoding:
Text File  |  1991-08-04  |  999 b   |  33 lines

  1. #!/usr/bin/perl
  2.  
  3. setnetent(1);
  4. sethostent(1);
  5.  
  6. for $host (@ARGV) {
  7.     $name = '';
  8.     if (@addrs = $host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
  9.     @addrs = pack(C4, @addrs);
  10.     ($name, $aliases, $addrtype, $length, @addrs) = gethostbyaddr($addrs[0],2);
  11.     } elsif ($host =~ /^[\dx]+$/ ) { 
  12.     @addrs = pack(L, /^0/ ? oct : $_);
  13.     ($name, $aliases, $addrtype, $length, @addrs) = gethostbyaddr($addrs[0],2);
  14.     } else {
  15.     ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($host);
  16.     # print "  $host is really $name\n" if $host ne $name; 
  17.     } 
  18.     if (!$name) {
  19.     warn "$0: gethostbysomething: $host: $!\n";
  20.     next;
  21.     } 
  22.     for $addr ( @addrs ) {
  23.     @bytes = unpack(C4, $addr);
  24.     $dotaddr = join('.', @bytes);
  25.     printf "%-20s %-16s ",$name, "[$dotaddr]";
  26.     $network = pack(C4, @bytes[0..2], 0); # XXX: apparent class C only
  27.     ($name, $aliases, $addrtype, $net) = getnetbyaddr(unpack(L, $network),2);
  28.     print $name ? $name : "an unnamed network";
  29.     print " ($aliases)" if $aliases;
  30.     print "\n";
  31.     }
  32.