ldap3 python add user to group

亡梦爱人 提交于 2019-11-30 15:35:31
Anton Bezkrovnyy

Use ldap3.extend.microsoft.addMembersToGroups

from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addUsersInGroups

...

addUsersInGroups(conn, user_dn, group_dn)

Full script:

from ldap3 import Server, Connection, ALL, NTLM
from elizabeth import Personal, Address,Text
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addUsersInGroups
import random

serverName='dc1.stand.local'
connUser="stand.lsd\\Admin"
connUserPwd=""
usersOU = 'ou=test-ou,dc=stand,dc=local'
groupsOU = 'ou=test-groups,dc=stand,dc=local'

usersDnList = []
groupsDnList = []

server = Server(serverName, get_info=ALL)
conn = Connection(server, user=connUser, password=connUserPwd, authentication=NTLM)
conn.bind() #must be TRUE

conn.add(usersOU, 'organizationalUnit') # add test-ou for users
conn.add(groupsOU, 'organizationalUnit') # add test-ou for groups

data = Text('en')
for _ in range(0,10):
    currentGroup = 'cn='+data.word()+',ou=test-groups,dc=stand,dc=local'
    groupsDnList.append(currentGroup)
    conn.add(currentGroup, 'group')

address = Address('en')
person = Personal('en')
for _ in range(0,10):
    address_country = address.country()
    conn.add('ou='+address_country+',ou=test-ou,dc=stand,dc=local', 'organizationalUnit')
    for _ in range (0,10):
        name = person.name(gender='male')
        surname = person.surname(gender='male')
        currentUser = 'cn='+name+'.'+surname+','+'ou='+address_country+',ou=test-ou,dc=stand,dc=local'
        usersDnList.append(currentUser)
        conn.add(currentUser, 'User',
        {'givenName': name,
        'sn': surname,
        'departmentNumber': 'DEV',
        'telephoneNumber': 1111})

for _ in range(0,300):
    rndUser = random.choice(usersDnList)
    rndGroup = random.choice(groupsDnList)
    addUsersInGroups(conn, rndUser, rndGroup)

I solve it doing...

def insert_ldif (firstname, last_name, UserName, password):
host = 'YourldapIP'
user = 'root'
password = 'YourldapPassword'
a=''
b=a+1
uidNumber = str(b)


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))

session = Session()
session.handshake(sock)
session.userauth_password(user, password)
channel = session.open_session()

channel.shell()
channel.write('cat > /home/reemplaceusername/gen.ldif <<EOF\n')
channel.write('dn: uid= %s'',ou=MeLi,dc=lnxnet,dc=ldap\n' %UserName)
channel.write('objectClass: inetOrgPerson\n')
channel.write('objectClass: posixAccount\n')
channel.write('objectClass: shadowAccount\n')
channel.write('shadowLastChange: 0\n')
channel.write('uid: %s\n' %firstname)
channel.write('sn: %s\n' %last_name)
channel.write('givenName: %s\n'%firstname)
channel.write('cn: %s'%firstname)
channel.write(' %s\n'%last_name)
channel.write('displayName: %s ' %firstname)
channel.write('%s\n' %last_name)
channel.write('uidNumber: %s\n' %uidNumber)
channel.write('gidNumber: 5000\n')
channel.write('userPassword: %s\n'%password)
channel.write('gecos: %s ' %firstname)
channel.write('%s\n'%last_name)
channel.write('loginShell:/bin/bash\n')
channel.write('homeDirectory: /home/users/%s\n'%UserName)
channel.write('EOF\n')
channel.write('ldapadd -x -D cn=admin,dc=lnxnet,dc=ldap -W -f /home/reemplaceusername/gen.ldif\n')
channel.write('ldappassword\n')
channel.close()
size, data =  channel.read()
while size > 0:
    print(data.decode())
    size, data = channel.read()
channel.close()
print("Exit status: {0}".format(channel.get_exit_status()))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!