1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 | $ cat ldap_read.py
#!/usr/bin/python
import ldap
import time
import ldap.modlist as modlist
try:
read = ldap.initialize("ldap://krypton.roe.lan:389/")
read.protocol_version = ldap.VERSION3
read.simple_bind_s("cn=admin,dc=roe,dc=lan","xxxxx")
except ldap.LDAPError, e:
print e
quit
try:
write = ldap.initialize("ldap://localhost:389/")
write.protocol_version = ldap.VERSION3
write.simple_bind_s("cn=admin,dc=roe,dc=lan","xxxxxx")
except ldap.LDAPError, e:
print e
print "foo"
quit
baseDN = "dc=roe,dc=lan"
searchScope = ldap.SCOPE_SUBTREE
#retrieveAttributes = ['cn','firstname','sn','mail']
retrieveAttributes = []
searchFilter = "employeetype=Active"
try:
ldap_result_id = read.search(baseDN,searchScope,searchFilter,retrieveAttributes)
result_set = []
while 1:
result_type, result_data = read.result(ldap_result_id, 0)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
foo = result_data[0]
dn = foo[0]
bar = foo[1]
mail = bar['mail']
displayName = bar['displayName']
cn = bar['cn']
employeeType = bar['employeeType']
givenName = bar ['givenName']
sn = bar['sn']
userPassword = bar['userPassword']
objectClass = bar['objectClass']
# password = bar['password']
time = int(1000000*time.time())
attrs={}
attrs['objectclass'] = ['top','simpleSecurityOjbect','inetorgperson','posixAccount','shadowAccount','virtualMailAccount']
attrs['cn'] = bar['cn']
attrs['mail'] = bar['mail']
attrs['displayName'] = bar['displayName']
attrs['employeeType'] = bar['employeeType']
attrs['givenName'] = bar ['givenName']
attrs['userpassword'] = bar['userPassword']
attrs['sn'] = bar['sn']
attrs['uid'] = bar['cn']
attrs['uidNumber'] = 1001
attrs['gidNumber'] = 1001
attrs['homedirectory'] = "/var/virtual/roe.lan/"+str(displayName[0])
attrs['mailbox'] = "/var/virtual/roe.lan/"+str(displayName[0])
attrs['vdhome'] = "/var/virtual/roe.lan/"+str(displayName[0])
attrs['delete'] = 'FALSE'
attrs['lastchange'] = time
attrs['accountActive'] = 'TRUE'
print "====="
# print displayName[0]
# print attrs
# print dn
# print foo[0]
ldif = modlist.addModlist(attrs)
write.add_s(dn,ldif)
# print mail[0]
# print maildir
# print displayName[0]
# print cn[0]
# print employeeType[0]
# print givenName[0]
# print sn[0]
# print userPassword[0]
# print objectClass
# result_set.append(result_data)
# print result_set
# print "====\n"
# print result_set[0]
except ldap.LDAPError, e:
print e
===========output==========
$ ./ldap_read.py
=====
Traceback (most recent call last):
File "./ldap_read.py", line 78, in <module>
ldif = modlist.addModlist(attrs)
File "/usr/lib/python2.6/dist-packages/ldap/modlist.py", line 37, in addModlist
attrvaluelist = filter(lambda x:x!=None,entry[attrtype])
TypeError: 'int' object is not iterable
|