小fish 发表于 2018-8-7 06:15:58

Python 和 LDAP

#!/bin/env python  

  
import sys, ldap
  

  
LDAP_HOST = 'localhost'
  
LDAP_BASE_DN = 'dc=unisonis,dc=com'
  
MGR_CRED = 'cn=Manager,dc=unisonis,dc=com'
  
MGR_PASSWD = 'mypasswd'
  
STOOGE_FILTER = 'o=stooges'
  

  
class StoogeLDAPMgmt:
  

  
def __init__(self, ldap_host=None, ldap_base_dn=None, mgr_cred=None,
  
mgr_passwd=None):
  
if not ldap_host:
  
ldap_host = LDAP_HOST
  
if not ldap_base_dn:
  
ldap_base_dn = LDAP_BASE_DN
  
if not mgr_cred:
  
mgr_cred = MGR_CRED
  
if not mgr_passwd:
  
mgr_passwd = MGR_PASSWD
  
self.ldapconn = ldap.open(ldap_host)
  
self.ldapconn.simple_bind(mgr_cred, mgr_passwd)
  
self.ldap_base_dn = ldap_base_dn
  

  
def list_stooges(self, stooge_filter=None, attrib=None):
  
if not stooge_filter:
  
stooge_filter = STOOGE_FILTER
  
s = self.ldapconn.search_s(self.ldap_base_dn, ldap.SCOPE_SUBTREE,
  
stooge_filter, attrib)
  
print "Here is the complete list of stooges:"
  
stooge_list = []
  
for stooge in s:
  
attrib_dict = stooge
  
for a in attrib:
  
out = "%s: %s" % (a, attrib_dict)
  
print out
  
stooge_list.append(out)
  
return stooge_list
  

  
def add_stooge(self, stooge_name, stooge_ou, stooge_info):
  
stooge_dn = 'cn=%s,ou=%s,%s' % (stooge_name, stooge_ou, self.ldap_base_dn)
  
stooge_attrib = [(k, v) for (k, v) in stooge_info.items()]
  
print "Adding stooge %s with ou=%s" % (stooge_name, stooge_ou)
  
self.ldapconn.add_s(stooge_dn, stooge_attrib)
  

  
def modify_stooge(self, stooge_name, stooge_ou, stooge_attrib):
  
stooge_dn = 'cn=%s,ou=%s,%s' % (stooge_name, stooge_ou, self.ldap_base_dn)
  
print "Modifying stooge %s with ou=%s" % (stooge_name, stooge_ou)
  
self.ldapconn.modify_s(stooge_dn, stooge_attrib)
  

  
def delete_stooge(self, stooge_name, stooge_ou):
  
stooge_dn = 'cn=%s,ou=%s,%s' % (stooge_name, stooge_ou, self.ldap_base_dn)
  
print "Deleting stooge %s with ou=%s" % (stooge_name, stooge_ou)
  
self.ldapconn.delete_s(stooge_dn)
  
页: [1]
查看完整版本: Python 和 LDAP