title: LDAP Search -- Bad Filter
body: <p>So I am trying to search LDAP to populate a CSV file.  The User IDs are obtained by parsing a text file.  I want to search the LDAP for the user ID (mailNickname), FName (givenName), LName (sn), "OU", pwdLastSet.  How do I use the filter aspect of search because I keep getting the "bad filter" error.</p>
  
  <p>Thanks for your help.</p>
  
  <pre><code>    #!/usr/bin/perl
  use xSV;
  use Net::LDAP;
  use Term::ReadKey;
  # use strict;
  
  $debug_on = 1;  # This will enable some debugging messages.
  
  if (defined($ARGV[0])) {
      $input = $ARGV[0];
  } else {
      print "No input file specified!  Assuming MKSGroupsoutput.txt...\n";
      $input = "MKSGroupsoutput.txt";
  }
  
  if (defined($ARGV[1])) { 
      $output = $ARGV[1];
  } else {
      $output = $input . ".csv";
  }
  
  open GROUPS, "&lt; $input" or die "ERROR: Can't open input file: '$input'\n";
  
  print "INPUT: $input\nOUTPUT: $output\n";
  
  sub debug {
      if ($debug_on == 1) {
          print "@_";
      }
  }
  
  my $csv = Text::xSV-&gt;new(
      filename =&gt; $output,
      header   =&gt; [
      "AccountName", 
      "LastName", 
      "FirstName", 
      "EEID", 
      "SYSGenericAcct", 
      "Notes", 
      "AccessLevel", 
      "AccessCapability", 
      "Owner/Manager",
      "Description", 
      "Created", 
      "Status", 
      "LastStatusChange", 
      "LastPwdChange", 
      "DataSource"
      ],
    );
  $csv-&gt;print_header();
  
  my ($inputname, $fname, $lname, @name, $uname, $pwordinput, $pword, $domain, $line, $ldap, $bindstring, $root, $pword, $base_dn);
  #User driven input to connect to LDAP.  ReadMode 2 keeps password hidden, the reset back to 0.
  print "Please enter your Full Name: ";
  $inputname = &lt;STDIN&gt;;
  print "Please enter your password: ";
  ReadMode 2;
  $pwordinput = &lt;STDIN&gt;;
  ReadMode 0;
  
  #Builds the LDAP CN value from user input. Trims \n off user input values
  $inputname = substr($inputname,0,-1);
  @name = split(' ', $inputname);
  $fname = ucfirst(@name[0]);
  $lname = ucfirst(@name[1]);
  $pword = substr($pwordinput,0,-1);
  $uname = $lname ."\\, " . $fname;
  $bindstring = "CN=" . $uname . ",OU=User,DC=hq,DC=name,DC=com";
  
  #LDAP Connection parameters
  $base_dn = "OU=User,DC=hq,DC=name,DC=com";
  $ldap = Net::LDAP-&gt;new('ldap://local', onerror =&gt;'die');
  $ldap-&gt;bind($bindstring, password =&gt; $pword); #Sometimes the BIND fails (1 out of 10 times)
  $root = $ldap-&gt;root_dse;
  
  #Parses text file
  while ($line = &lt;GROUPS&gt;) {
      chomp($line);
      if ($line =~ m/^  user  .*/) {
          $line =~ s/^  user.\s//;
          my ($searchoutput, $user, @entries, $entry, $href, $strDomain, $strUsername, $strDN, $arrSplitResponse, $strLName, $strFName, $strUserType);
          $user = $line;
          $user = "mailNickname: " . $user;
          $searchoutput = $ldap-&gt;search(filter=&gt;$user,base=&gt;$base_dn); # ERROR HERE
          @entries = $searchoutput-&gt;entries;
          foreach $entry ( @entries ) {
              print "DN: ", $entry-&gt;dn, "\n"; #NEVER ENTERS THIS LOOP
          }
  </code></pre>