我有一个看起来像这样的函数:
def get_users(yaml_file="AD_Users.yml"):
with open(yaml_file, 'r') as stream:
try:
data = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
return data.itervalues()
def create_user_in_ad(new_users): #username, password, base_dn, fname, lname, domain):
# LDAP connection
for new_user in new_users:
try:
logging.info('Connecting to LDAP Server %s ' % LDAP_SERVER)
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0)
ldap_connection = ldap.initialize(LDAP_SERVER)
ldap_connection.simple_bind_s(BIND_DN, BIND_PASS)
print "Connected to LDAP Server!"
except ldap.LDAPError, error_message:
print "Error connecting to LDAP server: %s" % error_message
#logging.info('Unable to connect to LDAP Server %s ' % LDAP_SERVER)
return False
# sys.exit(1)
# Check and see if user exists
try:
print new_user['username']
logging.info('Querying AD for user: %s ' % new_user['username'])
user_results = ldap_connection.search_s(BASE_DN, ldap.SCOPE_SUBTREE,
'(&(sAMAccountName=' +
new_user['username'] +
')(objectClass=person))',
['distinguishedName'])
print user_results
except ldap.LDAPError, error_message:
print "Error finding username: %s" % error_message
logging.info('Unable to query for user: %s ' % new_user['username'])
return False
# Check the results
if len(user_results) != 0:
print "User", new_user['username'], "already exists in AD:"
return False
sys.exit(1)
# Lets build our user: Disabled to start (514)
USER_DN = 'cn=' + new_user['fname'] + ' ' + new_user['lname'] + ',' + BASE_DN
GROUP_DN = 'ou=' + new_user['group_dn'] + ',' + BASE_DN
user_attrs = {}
user_attrs['objectClass'] = \
['top', 'person', 'organizationalPerson', 'user']
user_attrs['cn'] = new_user['fname'] + ' ' + new_user['lname']
user_attrs['userPrincipalName'] = new_user['username'] + '@' + new_user['domain']
# Add the new user account
try:
ldap_connection.add_s(USER_DN, user_ldif)
logging.info('Adding user into AD: %s ' % new_user['username'])
except ldap.LDAPError, error_message:
print "Error adding new user: %s" % error_message
return False
# Add the password
try:
ldap_connection.modify_s(USER_DN, add_pass)
logging.info('Password added for user: %s ' % new_user['username'])
except ldap.LDAPError, error_message:
print "Error setting password: %s" % error_message
return False
# Change the account back to enabled
try:
ldap_connection.modify_s(USER_DN, mod_acct)
logging.info('Enabling AD Account for user: %s ' % new_user['username'])
except ldap.LDAPError, error_message:
print "Error enabling user: %s" % error_message
return False
# Add user to their primary group
try:
ldap_connection.modify_s(GROUP_DN, add_member)
logging.info('Adding user to group %s: ' % new_user['group_dn'])
except ldap.LDAPError, error_message:
print "Error adding user to group: %s" % error_message
return
new_users = get_users()
if args.createusers is not "None":
create_user_in_ad(new_users)
它使用用户信息调用YAML文件:
User1:
username: adtest2
fname: adtest2
lname: adtest2
domain: test.com
group_dn: test
# group:
# User2:
# username: testing
# fname: testing
# lname: bbbbb
# domain: test.com
# group_dn: test
# # group:
ldap_connection.unbind_s()
print "User %s has been created in AD!" % new_user['username']
# All is good
return True
现在的问题是它只在第二个用户没有被注释时执行,而且我想知道我的for循环中是否有错误?它正在做我想要它做的一切。我使用Python 2.7.13 btw!
现在输出只显示第二个用户:
Connected to LDAP Server!
adtest2
答案 0 :(得分:0)
问题是返回是在for循环中执行的。我只是把它移到了for循环之外。