是否可以像那样导入LDIF
dn: cn=vpupkin,cn=people,ou=company,dc=domain,dc=com
c: UA
cn: vpupkin
employeetype: Indoor Front-end developer
gidnumber: 500
givenname: Vasya
homedirectory: /home/vpupkin
host: example.com
l: Kyiv
loginshell: /bin/bash
mail: vpupkin@domain.com
o: Microsoft
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: top
objectclass: shadowAccount
objectclass: ldapPublicKey
objectclass: extensibleObject
labeleduri: skype://test
sn: Pupkin
sshpublickey: ssh-rsa key
st: Trudova, 15
telephonenumber: 7777777777
uid: vpupkin
uidnumber: 1000
userpassword: {SHA}fEqNCco3Yq9h5ZUglD3CZJT4lBs=
使用python-ldap库(http://www.python-ldap.org/)?
是的,我可以用http://www.grotan.com/ldap/python-ldap-samples.html#add这样的方式向LDAP添加新记录,但我不确定这样的文本ldif文件。
PS。完整答案在http://pastebin.com/eQU7xBfj
答案 0 :(得分:3)
python-ldap 包含LDIF Parser module。使用它来解析LDIF并将结果字典提交到ldap_connection.add_s()
。
使用示例:
from StringIO import StringIO
import ldif
from ldap import modlist
ldif_file = StringIO("""dn: cn=vpupkin,cn=people,ou=company,dc=domain,dc=com
c: UA
cn: vpupkin
""")
parser = ldif.LDIFRecordList(ldif_file)
parser.parse()
for dn, entry in parser.all_records:
add_modlist = modlist.addModlist(entry)
ldap_conn.add_s(dn, add_modlist)
答案 1 :(得分:1)
您也可以尝试以下方法:
class MyLDIF(ldif.LDIFParser):
def __init__(self, input):
ldif.LDIFParser.__init__(self,input)
def handle(self,dn,entry):
ldif = modlist.addModlist(entry)
l = ldap.open('localhost', 389)
try:
l.bind("cn=Manager,dc=exapmle,dc=org", "Secret123")
except ldap.SERVER_DOWN, e:
print "ldap server is down"
else:
l.add_s(dn, ldif)
def enable_schema():
parser = MyLDIF(open('/tmp/a1.ldif', 'rb'))
parser.parse()
中给出的示例推导出上述解决方案