#!/usr/bin/perl
#
# QSUBNET6 - Jim Dutton - 25 Oct 99
#
# query an LDAP database for user information for a given
# IP host (address or hostname) on 6.0/6.128 subnets
#
# Requires: Perl (obviously) and LDAPSEARCH (UMich, OpenLDAP, Netscape LDAP)
#
$numargs = scalar @ARGV;
if ($numargs == 0){
	print "\nUsage (one of the following):\n\n";
	print "    qsubnet6 <last octet of IP address>\n";
	print "    qsubnet6 <first qualifier of hostname>\n\n";
	exit;
}
$s = shift @ARGV; $filter = "empty";
if ($s =~ /^[0-9]+/) {$filter = "dnsrecord=<IP addr prefix>.$s"}
else {$filter = "dc=$s"};
#print "filter = [$filter]\n";

$ldapcmd = "/usr/local/bin/ldapsearch"; $ldaphost = "-h 127.0.0.1";
$ldapbase = "-b o=<org>,c=US";
$attributes = "description owner l dnsrecord";

$result = `$ldapcmd $ldaphost $ldapbase "$filter" $attributes`;
if ($? != 0) {
	print "\n*** LDAPSEARCH ERROR (rc=$?) ***\n";
	print "--- search terminated ---\n\n"; exit;
}

@results = split(/\n/,$result);
#print "result: [@results]\n";
$numargs = scalar @results; $desc_num = 0;
#print "numargs =[$numargs]\n";
for ($i=0;$i<$numargs;$i++) {
	($w1,$remainder) = split(/=/,@results[$i],2);
#   print "w1=[$w1], remainder=[$remainder]\n";
     if ($w1 eq "dc") {
         ($w1,$remainder) = split(/,/,$remainder,2);
         $host = $w1.".<hostname suffix>" }
  elsif ($w1 eq "description") {
         @description[$desc_num++] = $remainder}
  elsif ($w1 eq "owner")   {$owner = $remainder}
  elsif ($w1 eq "l")    {$location = $remainder}
  elsif ($w1 eq "dnsrecord") {$IPaddress = $remainder}
}

format =

=======================================================
Hostname         : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$host
IP Address       : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$IPaddress
Owner            : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$owner
Location         : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$location
Description      : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@description[0]
Description      : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@description[1]
=======================================================

.
write;
exit;
