cannatag / ldap3

a strictly RFC 4510 conforming LDAP V3 pure Python client. The same codebase works with Python 2. Python 3, PyPy and PyPy3
Other
882 stars 272 forks source link

How to pass variable into ldap to create an account #1004

Open GokkulKumarVD opened 2 years ago

GokkulKumarVD commented 2 years ago

When I pass the variable with the value, it is creating account in the name of variable, not the value of variable. Please help

import ldap3 conn = ldap3.Connection("172.16.251.61", "SWINTERNAL\ad.manager", "Adm@n@ger", auto_bind=True)

cnn = 'dummy'

conn.add('cn=cnn,ou=POC,dc=SWINTERNAL,dc=LOCAL', 'User', {'givenName': 'Beatrix', 'sn': 'Young', 'departmentNumber': 'DEV', 'telephoneNumber': 1111})

jowage58 commented 2 years ago

If you are Python 3.6 or higher you can use f-strings to create your DN value. If the value comes from an untrusted source you may first want to escape it with the ldap3.utils.conv.escape_filter_chars function.

cnn = 'dummy'

conn.add(f'cn={cnn},ou=POC,dc=SWINTERNAL,dc=LOCAL', ...)

Or if using something less than 3.6 could use percent formatting:

cnn = 'dummy'

conn.add('cn=%s,ou=POC,dc=SWINTERNAL,dc=LOCAL' % cnn, ...)