ldap search

perl

(ldap_search_by_mail.pl)

#!/usr/bin/env perl
use Net::LDAP;

$hostname = '192.168.0.xx';
$base_dn = "dc=example,dc=co,dc=jp";

$ldap = Net::LDAP->new($hostname) || die "$@";

$message = $ldap->bind('xxx@example.co.jp', password=>'xxxxxx');

$message->code && die "code:".$message->code."\nerror:".$message->error;

$result = $ldap->search(
  base => $base_dn,
  filter=>"(mail=$ARGV[0])"
);

$result->code && die "code:".$result->code."\nerror:".$result->error;

foreach $entry($result->entries){
  print $entry->dump;
}

$message = $ldap->unbind;

参照したURL:https://metacpan.org/pod/Net::LDAP


使ってみる。

$ ./ldap_search_by_mail.pl kirk*
------------------------------------------------------------------------
dn:CN=カーク 駿,CN=Users,DC=example,DC=co,DC=jp

          objectClass: top
                       person
                       organizationalPerson
                       user
                   cn: カーク 駿
    distinguishedName: CN=カーク 駿,CN=Users,DC=example,DC=co,DC=jp
         instanceType: 4
          whenCreated: 20210223031335.0Z
          whenChanged: 20210409103951.0Z
          displayName: カーク 駿
           uSNCreated: 20586
           uSNChanged: 77874
           department: 書道部
                 name: カーク 駿
           objectGUID: tJHQ
   userAccountControl: 66048
          badPwdCount: 0
             codePage: 0
          countryCode: 0
      badPasswordTime: 132625169584440166
           lastLogoff: 0
            lastLogon: 132625169694422045
           pwdLastSet: 132602821756321877
       primaryGroupID: 513
            objectSid: nqvf]$
       accountExpires: 9223372036854775807
           logonCount: 10
       sAMAccountName: KirkTakashi
       sAMAccountType: 805306368
    userPrincipalName: KirkTakashi@example.co.jp
          lockoutTime: 0
       objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=example,DC=co,DC=jp
dSCorePropagationData: 16010101000000.0Z
   lastLogonTimestamp: 132624367043248920
                 mail: KirkTakashi@example.co.jp

ruby

(ldap_search_by_mail.rb)

#!/usr/bin/env ruby
require 'net/ldap'

base = "dc=example,dc=co,dc=jp"
auth= {
  :method => :simple,
  :username => 'xxx@example.co.jp',
  :password => 'xxxxxx'
}

ldap = Net::LDAP.new :host => "192.168.0.xx", :base => base,
                     :port => 389,
                     :auth => auth

filter = Net::LDAP::Filter.eq("mail", ARGV[0])
ldap.search(:filter => filter) do |entry|
  #p entry
  entry.each do |attribute, values|
    if attribute !~ /binary/
      print "#{attribute}:"
      values.each do |value|
        print "#{value}"
      end
      puts
    end
  end
end

puts "ldap.get_operation_result:" + ldap.get_operation_result.to_s


使ってみる。

$ ./ldap_search_by_mail.rb kirk*
dn:CN=カーク 駿,CN=Users,DC=example,DC=co,DC=jp
objectclass:toppersonorganizationalPersonuser
cn:カーク 駿
distinguishedname:CN=カーク 駿,CN=Users,DC=example,DC=co,DC=jp
instancetype:4
whencreated:20210223031335.0Z
whenchanged:20210409103951.0Z
displayname:カーク 駿
usncreated:20586
usnchanged:77874
department:書道部
name:カーク 駿
objectguid:tJHQ
useraccountcontrol:66048
badpwdcount:0
codepage:0
countrycode:0
badpasswordtime:132625169584440166
lastlogoff:0
lastlogon:132625169694422045
pwdlastset:132602821756321877
primarygroupid:513
objectsid:nqvf]$
accountexpires:9223372036854775807
logoncount:10
samaccountname:KirkTakashi
samaccounttype:805306368
userprincipalname:KirkTakashi@example.co.jp
lockouttime:0
objectcategory:CN=Person,CN=Schema,CN=Configuration,DC=example,DC=co,DC=jp
dscorepropagationdata:16010101000000.0Z
lastlogontimestamp:132624367043248920
mail:KirkTakashi@example.co.jp
ldap.get_operation_result:#<OpenStruct extended_response=nil, code=0, error_message="", matched_dn="", message="Success">