Difference between revisions of "Python - LDAP"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 15: | Line 15: | ||
#--------------------------------------------------------------------------------------------------- | #--------------------------------------------------------------------------------------------------- | ||
l = ldap.initialize("ldap:// | LDAP_HOST = 'xxx' | ||
#--------------------------------------------------------------------------------------------------- | |||
l = ldap.initialize("ldap://%s:389" 5 LDAP_HOST) | |||
l.simple_bind_s("","") | l.simple_bind_s("","") | ||
base_dn = "ou= | base_dn = "ou=xxx,dc=performiq,dc=com,dc=au" | ||
filter = "(&(objectclass= | filter = "(&(objectclass=xxx)(uid=svt*))" | ||
rows = l.search_s(base_dn, ldap.SCOPE_SUBTREE, filter) | rows = l.search_s(base_dn, ldap.SCOPE_SUBTREE, filter) | ||
Line 42: | Line 46: | ||
#--------------------------------------------------------------------------------------------------- | #--------------------------------------------------------------------------------------------------- | ||
</pre> | |||
Returned data looks like: | |||
<pre> | |||
( | |||
'uid=xxxx,ou=internal,ou=people,dc=xxxx,dc=com', | |||
{ | |||
'cn' : ['xxxx'], | |||
'description' : ['xxxx'], | |||
'objectClass' : ['xxxx'], | |||
'sn' : ['xxxx'], | |||
'groups' : ['cn=xxxx,ou=xxxx,dc=performiq,dc=com,dc=au'], | |||
'uid' : ['xxxx'] | |||
} | |||
) | |||
</pre> | </pre> | ||
Line 49: | Line 70: | ||
<pre> | <pre> | ||
#!/usr/bin/env python | |||
# | |||
#--------------------------------------------------------------------------------------------------- | |||
import sys | |||
import ldap | |||
#--------------------------------------------------------------------------------------------------- | |||
HOST = 'hx30' | |||
PORT = 6389 | |||
#--------------------------------------------------------------------------------------------------- | |||
idx = 0 | |||
l = ldap.initialize("ldap://%s:%d" % (HOST, PORT)) | |||
l.simple_bind_s("cn=xxx,dc=performiq,dc=com,dc=au","xxx") | |||
base_dn = "uid=svt_PLH%05d,ou=xxx,ou=xxx,dc=performiq,dc=com,dc=au" % idx | |||
print base_dn | |||
mod_attrs = [ | |||
(ldap.MOD_REPLACE, 'description', 'SVT User %05d' % idx), | |||
(ldap.MOD_DELETE, 'GivenName', 'Francis' ), | |||
(ldap.MOD_ADD, 'GivenName', 'Frank' ) | |||
] | |||
rc = l.modify_s(base_dn, mod_attrs) | |||
print rc | |||
</pre> | </pre> | ||
Line 60: | Line 114: | ||
import sys | import sys | ||
import ldap | import ldap | ||
#--------------------------------------------------------------------------------------------------- | #--------------------------------------------------------------------------------------------------- | ||
Line 79: | Line 119: | ||
def record(idx): | def record(idx): | ||
add_record = [ | add_record = [ | ||
('objectclass', [' | ('objectclass', ['objectclass']), | ||
('uid', ['svt_PLH%05d' % idx]), | ('uid', ['svt_PLH%05d' % idx]), | ||
('cn', ['SVT PLH%05d' % idx] ), | ('cn', ['SVT PLH%05d' % idx] ), | ||
('sn', ['svt_PLH%05d' % idx] ), | ('sn', ['svt_PLH%05d' % idx] ), | ||
('description', ['PLH test %05d' % idx]), | ('description', ['PLH test %05d' % idx]), | ||
('userpassword', ['secret']), | ('userpassword', ['secret']), | ||
('ou', [' | ('ou', ['ou']) | ||
] | ] | ||
Line 96: | Line 136: | ||
l = ldap.initialize("ldap://%s:%d" % (HOST, PORT)) | l = ldap.initialize("ldap://%s:%d" % (HOST, PORT)) | ||
l.simple_bind_s("cn= | l.simple_bind_s("cn=xxxx,dc=performiq,dc=com,dc=au","xxxx") | ||
base_dn = "uid= | base_dn = "uid=svt_xxx%05d,ou=xxx,ou=xxx,dc=performiq,dc=com,dc=au" % idx | ||
add_rec = record(idx) | add_rec = record(idx) | ||
Line 123: | Line 163: | ||
l = ldap.initialize("ldap://%s:%s" % (HOST,PORT)) | l = ldap.initialize("ldap://%s:%s" % (HOST,PORT)) | ||
l.simple_bind_s("cn= | l.simple_bind_s("cn=xxx,dc=performiq,dc=com,dc=au","XXXX") | ||
base_dn = "uid= | base_dn = "uid=svt_xxx%05d,ou=xxx,dc=performiq,dc=com,dc=au" % idx | ||
print base_dn | print base_dn | ||
Line 134: | Line 174: | ||
</pre> | </pre> | ||
=Links= | |||
* http://www.packtpub.com/article/python-ldap-applications-more-ldap-operations-and-the-ldap-url-library | |||
[[Category:Python]] | [[Category:Python]] | ||
[[Category:LDAP]] | [[Category:LDAP]] |
Latest revision as of 11:55, 2 April 2009
Using the Python LDAP Module
Search
The searches are made by binding anonymously.
#!/usr/bin/env python # #--------------------------------------------------------------------------------------------------- import sys import ldap #--------------------------------------------------------------------------------------------------- LDAP_HOST = 'xxx' #--------------------------------------------------------------------------------------------------- l = ldap.initialize("ldap://%s:389" 5 LDAP_HOST) l.simple_bind_s("","") base_dn = "ou=xxx,dc=performiq,dc=com,dc=au" filter = "(&(objectclass=xxx)(uid=svt*))" rows = l.search_s(base_dn, ldap.SCOPE_SUBTREE, filter) print "Returned -> %d rows" % len(rows) for row in rows: print row col0 = row[0].split(', ')[0] uid = col0.split(',')[0].split('=')[1] type = col0.split(',')[1].split('=')[1] if row[1].has_key('groups'): group = row[1]['groups'][0].split(',')[0].split('=')[1] else: group = '' print "%s,%s,%s" % (uid, type, group) #---------------------------------------------------------------------------------------------------
Returned data looks like:
( 'uid=xxxx,ou=internal,ou=people,dc=xxxx,dc=com', { 'cn' : ['xxxx'], 'description' : ['xxxx'], 'objectClass' : ['xxxx'], 'sn' : ['xxxx'], 'groups' : ['cn=xxxx,ou=xxxx,dc=performiq,dc=com,dc=au'], 'uid' : ['xxxx'] } )
Modify
Adds, mods and deletes require binding as a user with appropriate rights.
#!/usr/bin/env python # #--------------------------------------------------------------------------------------------------- import sys import ldap #--------------------------------------------------------------------------------------------------- HOST = 'hx30' PORT = 6389 #--------------------------------------------------------------------------------------------------- idx = 0 l = ldap.initialize("ldap://%s:%d" % (HOST, PORT)) l.simple_bind_s("cn=xxx,dc=performiq,dc=com,dc=au","xxx") base_dn = "uid=svt_PLH%05d,ou=xxx,ou=xxx,dc=performiq,dc=com,dc=au" % idx print base_dn mod_attrs = [ (ldap.MOD_REPLACE, 'description', 'SVT User %05d' % idx), (ldap.MOD_DELETE, 'GivenName', 'Francis' ), (ldap.MOD_ADD, 'GivenName', 'Frank' ) ] rc = l.modify_s(base_dn, mod_attrs) print rc
Add
#!/usr/bin/env python # #--------------------------------------------------------------------------------------------------- import sys import ldap #--------------------------------------------------------------------------------------------------- def record(idx): add_record = [ ('objectclass', ['objectclass']), ('uid', ['svt_PLH%05d' % idx]), ('cn', ['SVT PLH%05d' % idx] ), ('sn', ['svt_PLH%05d' % idx] ), ('description', ['PLH test %05d' % idx]), ('userpassword', ['secret']), ('ou', ['ou']) ] return add_record #--------------------------------------------------------------------------------------------------- idx = 3 l = ldap.initialize("ldap://%s:%d" % (HOST, PORT)) l.simple_bind_s("cn=xxxx,dc=performiq,dc=com,dc=au","xxxx") base_dn = "uid=svt_xxx%05d,ou=xxx,ou=xxx,dc=performiq,dc=com,dc=au" % idx add_rec = record(idx) rc = l.add_s(base_dn, add_rec) print rc
Delete
#!/usr/bin/env python # #--------------------------------------------------------------------------------------------------- import sys import ldap #--------------------------------------------------------------------------------------------------- idx = 0 l = ldap.initialize("ldap://%s:%s" % (HOST,PORT)) l.simple_bind_s("cn=xxx,dc=performiq,dc=com,dc=au","XXXX") base_dn = "uid=svt_xxx%05d,ou=xxx,dc=performiq,dc=com,dc=au" % idx print base_dn rc = l.delete_s(base_dn) print rc