import ldap import ldap.modlist as modlist #ADMIN_USER = raw_input("Enter the LDAP username to authenticate ") #PASS = raw_input("Enter your LDAP password ") #address = '10.91.49.35' address = '172.31.20.195' ADMIN_USER = 'Administrator@myforest.local' PASS = '.3L&GX?8Kn' def connectLDAP (ip_LDAP, ADMIN_USER, PASS): """Connect to Active Directory and return bind object""" ldap.set_option(ldap.OPT_REFERRALS, 0) # Open a connection l = ldap.initialize('ldap://' + address) # Bind/authenticate with a user with apropriate rights to add objects print("Establishing the connection".center(100, '*')) l.simple_bind_s(ADMIN_USER, PASS) return l def addUser(username, firstname, surname, email, password): """Create a new user in Active Directory""" # The dn of our new entry/object dn="cn=%s,DC=devcloud,DC=com" % (username) displayName = '%s %s [%s]' % (surname, firstname, username) # A dict to help build the "body" of the user object print("Creating User Object".center(100, '*')) attrs = {} attrs['objectclass'] = ['top','person','organizationalPerson','user'] attrs['cn'] = str(username) attrs['sAMAccountname'] = str(username) attrs['userPassword'] = str(password) attrs['givenName'] = str(firstname) attrs['sn'] = str(surname) attrs['displayName'] = str(displayName) attrs['userPrincipalName'] = "%s at mail.domain.it" % username # Some flags for userAccountControl property SCRIPT = 1 ACCOUNTDISABLE = 2 HOMEDIR_REQUIRED = 8 PASSWD_NOTREQD = 32 NORMAL_ACCOUNT = 512 DONT_EXPIRE_PASSWORD = 65536 TRUSTED_FOR_DELEGATION = 524288 PASSWORD_EXPIRED = 8388608 # this works! attrs['userAccountControl'] = str(NORMAL_ACCOUNT + ACCOUNTDISABLE) # Convert our dict to nice syntax for the add-function using modlist-module print("Adding the User".center(100, '*')) ldif = modlist.addModlist(attrs) l.add_s(dn,ldif) print("Unbinding the connection".center(100, '*')) l.unbind_s() def creategroup(groupname, groupdesc): ldap.set_option(ldap.OPT_REFERRALS, 0) # Open a connection address = '10.91.49.35' l = ldap.initialize('ldap://' + address) # Bind/authenticate with a user with apropriate rights to add objects ADMIN_USER = raw_input("Enter the LDAP username to authenticate ") PASS = raw_input("Enter your LDAP password ") print("Establishing the connection".center(100, '*')) l.simple_bind_s(ADMIN_USER, PASS) fs_dn = "cn=" + groupname + ",cn=Users,dc=devcloud,dc=com" print("Creating the group "+groupname) attr = {} attr['objectClass'] = ['Group'] attr['name'] = groupname attr['sAMAccountName'] = groupname attr['description'] = groupdesc ldif = modlist.addModlist(attr) print(l.add_s(fs_dn,ldif)) print("Unbinding the connection".center(100, '*')) l.unbind_s() def deletegroup(groupname): ldap.set_option(ldap.OPT_REFERRALS, 0) # Open a connection address = '10.91.49.35' l = ldap.initialize('ldap://' + address) # Bind/authenticate with a user with apropriate rights to add objects ADMIN_USER = raw_input("Enter the LDAP username to authenticate ") PASS = raw_input("Enter your LDAP password ") print("Establishing the connection".center(100, '*')) l.simple_bind_s(ADMIN_USER, PASS) deleteDN = "cn=" + groupname + ",cn=Users,dc=devcloud,dc=com print("Deleting group"+groupname) l.delete_s(deleteDN) print("Unbinding the connection".center(100, '*')) l.unbind_s() print("1. Create User. \n 2. Create Group. \n 3. Delete Group") choice = raw_input("Enter the choice ") if int(choice) == 1: username = raw_input("Enter the username to be added ") firstname = raw_input("Enter the firstname of user ") surname = raw_input("Enter the lastname of user ") email = raw_input("Enter the email of user ") password = raw_input("Enter the user's password ") addUser(username, firstname, surname, email, password) elif int(choice) == 2: groupname = raw_input("Enter the group name you want to add ") groupdesc = raw_input("Enter short group description ") creategroup(groupname, groupdesc) elif int(choice) == 3: groupname = raw_input("Enter the group name you want to delete ") deletegroup(groupname)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question