Active Directory/Search for a user: Difference between revisions
({{omit from|ZX Spectrum Basic|Does not have network access.}}) |
(Added PicoLisp) |
||
Line 3: | Line 3: | ||
Make sure you [[Connect to Active Directory]] |
Make sure you [[Connect to Active Directory]] |
||
=={{header|PicoLisp}}== |
|||
<lang PicoLisp>(de ldapsearch (Sn) |
|||
(in |
|||
(list "ldapsearch" "-xH" "ldap://db.debian.org" |
|||
"-b" "dc=debian,dc=org" |
|||
(pack "sn=" Sn) ) |
|||
(list |
|||
(cons 'cn (prog (from "cn: ") (line T))) |
|||
(cons 'uid (prog (from "uid: ") (line T))) ) ) )</lang> |
|||
Test: |
|||
<pre>: (ldapsearch "Fischer") |
|||
-> ((cn . "Mika") (uid . "mf"))</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 13:02, 5 July 2011
You are encouraged to solve this task according to the task description, using any language you may know.
Make sure you Connect to Active Directory
PicoLisp
<lang PicoLisp>(de ldapsearch (Sn)
(in (list "ldapsearch" "-xH" "ldap://db.debian.org" "-b" "dc=debian,dc=org" (pack "sn=" Sn) ) (list (cons 'cn (prog (from "cn: ") (line T))) (cons 'uid (prog (from "uid: ") (line T))) ) ) )</lang>
Test:
: (ldapsearch "Fischer") -> ((cn . "Mika") (uid . "mf"))
Python
<lang python>import ldap
l = ldap.initialize("ldap://ldap.example.com") try:
l.protocol_version = ldap.VERSION3 l.set_option(ldap.OPT_REFERRALS, 0)
bind = l.simple_bind_s("me@example.com", "password") base = "dc=example, dc=com" criteria = "(&(objectClass=user)(sAMAccountName=username))" attributes = ['displayName', 'company'] result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)
results = [entry for dn, entry in result if isinstance(entry, dict)] print results
finally:
l.unbind()
</lang>
Ruby
Assume AD server talks LDAP.
<lang ruby>require 'rubygems' require 'net/ldap'
ldap = Net::LDAP.new(:host => 'hostname', :base => 'base') ldap.authenticate('bind_dn', 'bind_pass')
filter = Net::LDAP::Filter.pres('objectclass') filter &= Net::LDAP::Filter.eq('sn','Jackman')
- or
filter = Net::LDAP::Filter.construct('(&(objectclass=*)(sn=Jackman))')
results = ldap.search(:filter => filter) # returns an array of Net::LDAP::Entry objects
puts results[0][:sn] # ==> "Jackman"</lang>
Tcl
One can do it with the low level Connect to Active Directory based handle with this code:
This is just the basic setup. <lang tcl>set Username "TestUser" set Filter "((&objectClass=*)(sAMAccountName=$Username))" set Base "dc=skycityauckland,dc=sceg,dc=com" set Attrs distinguishedName</lang>
Now do the actual search. <lang tcl>set result [ldap::search $conn $Base $Filter $Attrs -scope subtree]</lang>
If we have only a single result its easy: <lang tcl>if {[llength $result] == 1} {
puts [dict get [lindex $result 0 1] distinguishedName]
}</lang>
Looping over the result set to output some values. <lang tcl>foreach pair $result {
lassign $pair cn attributes puts [dict get $attributes distinguishedName]
}</lang>
If you're bored you can also use this instead: <lang tcl>package require ldapx set conn [ldapx::connect $BindDN $Password] $conn traverse $Base $Filter $Attrs e {
puts [$e get distinguishedName]
}</lang>
VBScript
The search string and execution of the string <lang vbscript>strUsername = "TestUser" strQuery = "<LDAP://dc=skycityauckland,dc=sceg,dc=com>;"_
& "(&(objectclass=*)(samaccountname=" & strUsername & "));distinguishedname;subtree"
objCmd.ActiveConnection = objConn objCmd.Properties("Page Size")=100 objCmd.CommandText = strQuery Set objRS = objCmd.Execute</lang>
Doing something with a single result (this will output the returned users full DN) <lang vbscript>If objRS.RecordCount = 1 Then
WScript.Echo objRS.Fields("DistinguishedName")
End If</lang>
Doing something with multiple results (this will output each returned users full DN) <lang vbscript>If objRS.RecordCount > 0 Then
For Each objUser in ObjRS WScript.Echo objRS.Fields("DistinguishedName") Next
End If</lang>