-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap-to-sql.py
executable file
·37 lines (33 loc) · 1.22 KB
/
ldap-to-sql.py
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
#! /usr/bin/env python
# This script is to read from a Active Directory Server
# to get sAMAccountName and objectGUID to transform them
# to SQL statements for a specific user table
import ldap
import getpass
import uuid
LDAPURL = "ldaps://ldapserver"
DOMAIN = "example.com"
# ask for login credentials
USER = raw_input("Username:")
PASS = getpass.getpass("Password for " + USER + ":")
USERNAME = USER + "@" + DOMAIN
l = ldap.initialize(LDAPURL)
try:
l.protocol_version = ldap.VERSION3
l.set_option(ldap.OPT_REFERRALS, 0)
bind = l.simple_bind_s(USERNAME, PASS)
base = "cn=Users"
for d in DOMAIN.split("."):
base += ",dc={}".format(d)
criteria = "(cn=*)"
attributes = ['sAMAccountName', 'ObjectGUID']
result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)
results = [entry for dn, entry in result if isinstance(entry, dict)]
finally:
l.unbind()
for item in results:
uobjectGUID = uuid.UUID(bytes=item['objectGUID'][0])
objectGUID = str(uobjectGUID).upper()
objectGUIDplain = objectGUID.translate(None, '-')
sAMAccountName = item.get('sAMAccountName')
print "update users set id_extern = '" + objectGUIDplain + "' where login = " + str(sAMAccountName).strip('[]') + ";"