Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. ## I want to be able to return a single value, from an LDAP search.
  2. ## I can do this with a shell script, like this :
  3.  
  4.  
  5. ## SHELL
  6. ldapsearch -x -b ou=people,dc=caret,dc=cam,dc=ac,dc=uk -s one uid=* | grep uidNumber | tail -n 1 | awk '{print $2}'
  7.  
  8.  
  9.  
  10.  
  11. ## PERL
  12. ## ----------------------------
  13.  
  14. ## Start the LDAP Bind process
  15.  
  16. if (! defined ($LDAPSERVER))
  17. {
  18. $LDAPSERVER = "ldap.dmz.caret.local";
  19. }
  20.  
  21. my $CAFILE = "/etc/ldap/cacerts/ldap-clients.pem";
  22. print "Enter your LDAP password, now. \n";
  23. my $BINDPASSWD = <>;
  24.  
  25. if (defined ($VERBOSE))
  26. {
  27. print "SERVER - $LDAPSERVER\n";
  28. print "PASSWORD - $BINDPASSWD\n";
  29. }
  30. my $ldaps = Net::LDAP->new(
  31. "ldaps://$LDAPSERVER",
  32. port => '636',
  33. onerror => 'warn',
  34. verify => 'none',
  35. cafile => $CAFILE,
  36. ) or die "Can't connect to ldaps://$LDAPSERVER\n";
  37.  
  38. $ldaps->bind( "uid=$BINDUSER,ou=people,dc=caret,dc=cam,dc=ac,dc=uk", password => "$BINDPASSWD" );
  39.  
  40.  
  41. sub lastuser {
  42.  
  43. my($lastuid);
  44.  
  45. my $lastuser = $ldaps->search(
  46. base => "ou=people,$LDAPBASE",
  47. filter => "(uid=*)",
  48. attrs => ['uidNumber'],
  49. scope => 'one'
  50. );
  51. if (defined ($VERBOSE)) {
  52. foreach my $entry ($lastuser->entries) { $entry->dump; }
  53. }
  54.  
  55. foreach my $entry ($lastuser->entries) {
  56. $lastuid = $entry->get('uidNumber');
  57. }
  58.  
  59. print "LASTUID = \n";
  60. my $uidtopass = print Dumper($lastuid);
  61.  
  62.  
  63. return $uidtopass;
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement