fonManagerDN=cn=John A Smith JR,ou=NETWK SVCS,ou=People,o=Newco,c=US #### use Net::LDAP; use Net::LDAP::LDIF; use Net::LDAP::Entry; # ***************************************** # # How to use this program: # # You must have the Active Perl package installed on your computer. # It may be downloaded from # http://www.activeperl.com/Products/Download/Get.plex?id=ActivePerl/ # # You will also need to install the perl-ldap package that supports NET::LDAP classes # The perl interpreter, perl.exe, must be in your path. # # ***************************************** # # ******************************************************************* # Configuration # Modify these values to customize the program # ******************************************************************* # # Set $use_credentials to 0 to bind anonymously. # When anonymous access is used, restricted data including uid can not be accessed. $use_credentials = 0; # If $use_credentials is set to any non-zero value to use $bind_dn and $bind_pw $bind_dn = 'anonymous'; # login ID for server $bind_pw = 'nopassword'; # password # These values are the ones to use for Newco when this program was written. $ldap_server = 'newco.corp.newco.com'; # host name of ldap server $ldap_base = 'ou=People,o=Newco,c=US'; # DN of search base # The following defines what attributes will be included in the output # enter one attribute name per line in lower case inside 'single quotes' # and followed by a comma. @attr_list = ('cn','fonDeptName','telephoneNumber','l', 'st'); # print column header #print LDAPFILE #"InputValue:cn:fonDeptName:telephoneNumber:l:st\n"; # ************** End of configuration section *********************** # ******************************************************************* # ************** Initialization ****************** # Set up a search pattern that will recognize any attribute. $attrs = join(':', @attr_list); # Create the global variables with an assignment to null. foreach $attr (@attr_list) { ${$attr} = ""; } #print "Bind using server $ldap_server and ID of $bind_dn\n"; # establish LDAP connection $ldap = Net::LDAP->new($ldap_server); unless ($ldap){ my $errmsg = $@; print STDERR "Failed to open LDAP session. Error = $errmsg\n"; exit; } #log in if ($use_credentials) { $mesg = $ldap->bind($bind_dn, password => $bind_pw); if (scalar $mesg->code){ my $errmsg = $mesg->error; my $errcode = $mesg->code; print STDERR "Failed to bind as $bind_pw, Error = $errcode ($errmsg)\n"; $ldap->unbind; exit; } } else { # print STDERR "Warning: Not using credentials. Some attributes may not be available.\n"; } # ***************** main ***************** $nf="file1"; open (NEWFILE, "$nf"); $af="file2"; open (LDAPFILE, ">$af"); $sf="sysfile"; open (SYSOPS, ">$sf"); while () { chomp; #remove newline in input string # search for any attribute matching the input string $srchresult = searchPeople($ldap,"(zcanyattribute=$_)"); # Warn if not found if ($srchresult->count == 0) { $login = $_; $gcos = $USERS{$login}; print SYSOPS "$login:$gcos:Not Found!!\n"; #print NEWFILE "$login\n"; #print "$_|Not found\n"; } # Warn if not unique elsif ($srchresult->count > 1) { print SYSOPS "$_:Yields more than 1 result:Unknown!!\n"; } # otherwise generte the output else { my $entry = $srchresult->entry(0); my $dn = $entry->dn; $outline = ""; foreach $attr (@attr_list) { # for perl-ldap .22 $$attr = $entry->get_value($attr); $outline .= "$$attr\:"; #$str=~s/ : \s* # chop ($outline); # for older versions #my @vals = $entry->get($attr); #$outline .= "$vals[0]\,"; } #print "$_: $outline\n"; #print "$_|$outline\n"; #print LDAPFILE "$_:$outline\n"; print LDAPFILE join(":",$_,$outline), "Active\n"; } } $ldap->unbind; # ********************************************** sub searchPeople # ********************************************** # Search the people branch of the tree with a designated search filter. # { my ($ldap, $searchfilter) = @_; # Assign the search base and list of attributes that interest us my $base = $ldap_base; # call the search method and return the result my $result = $ldap->search ( base => "$base", scope => "sub", filter => "$searchfilter", attrs => \@attr_list ); } ## End of searchPeople ##