Python - LDAP

From PeformIQ Upgrade
Revision as of 16:21, 10 May 2008 by PeterHarding (talk | contribs) (New page: =Using the Python LDAP Module= ==Search== <pre> </pre> ==Modify== <pre> </pre> == Add== <pre> #!/usr/bin/env python # #---------------------------------------------------------------...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Using the Python LDAP Module

Search


Modify


Add

#!/usr/bin/env python
#
#---------------------------------------------------------------------------------------------------

import sys
import ldap

#---------------------------------------------------------------------------------------------------

new = (
   'uid=apstokesd,ou=internal,ou=people,dc=auspost,dc=com',
   {
      'cn'            : ['apxxxkesd'],
      'description'   : ['apxxxkesd'],
      'objectClass'   : ['auspostUser'],
      'sn'            : ['apxxxkesd'],
      'groups'        : ['cn=GRP_Facility,ou=groups,dc=auspost,dc=com'],
      'uid'           : ['apxxxkesd']
   }
)

#---------------------------------------------------------------------------------------------------

def record(idx):
   add_record = [
      ('objectclass', ['auspostuser']),
      ('uid', ['svt_PLH%05d' % idx]),
      ('cn', ['SVT PLH%05d' % idx] ),
      ('sn', ['svt_PLH%05d' % idx] ),
      ('description', ['PLH test %05d' % idx]),
      ('userpassword', ['secret']),
      ('ou', ['users'])
   ]

   return add_record

#---------------------------------------------------------------------------------------------------

idx = 3

l = ldap.initialize("ldap://%s:%d" % (HOST, PORT))

l.simple_bind_s("cn=XXX,dc=XXX,dc=com","XXX")

base_dn = "uid=svt_XXX%05d,ou=people,ou=external,dc=XXX,dc=com" % 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=XXX,dc=com","XXXX")

base_dn = "uid=svt_XXX%05d,ou=people,dc=XXX,dc=com" % idx

print base_dn

rc = l.delete_s(base_dn)

print rc