I have one requirement like...I need to write a python script which will compare objects between two ldap servers residing in two different servers (server A and server B) and list out the discrepancies if found any. Ideally the cn, posix attributes like uidNumber, gidNumber,homeDir, loginshell attributes.
So the script should find out the discrepancies if any and write it to CSV. Could anyone please help me? Thanks in advance
I have written some of it ,
#!/usr/bin/python3
from ldap3 import Server, Connection, SUBTREE, core
import csv
import argparse
import time
# Global Variables
t = time.localtime()
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
serverA_uid_number = ''
serverA_cn = ''
serverB_uid_number = ''
uid = ''
common_name = ''
serverB_Sync = True
serverA_Sync = True
csvwriter = ''
filename_1 = "serverA_serverB-{}.csv".format(timestamp)
filename_2 = "serverB_serverA-{}.csv".format(timestamp)
serverA_server = Server('serverA-ldap-eb.abc.com', port=636, use_ssl=True)
serverB_server = Server('serverB-ldap-eb.abc.com', port=636, use_ssl=True)
# Argument parser
parser = argparse.ArgumentParser(description="serverA-serverB Synchronization and vice versa")
parser.add_argument("-g", "--serverA", help="serverA to serverB Synchronization", action="store_true")
parser.add_argument("-r", "--serverB", help="serverB to serverA Synchronization", action="store_true")
args = parser.parse_args()
def get_list_of_serverA_users():
fields = ['Distinguished name', 'Comman Name', 'serverA UID Number', 'serverB UID Number', 'Synchronization']
csvfile = open(filename_1, 'a+', newline='')
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
try:
c = Connection(serverA_server, user='cn=test1',
password='LDAP')
c.bind()
c.search(search_base='o=asml', search_filter='(objectClass=Person)', search_scope=SUBTREE,
attributes=['cn', 'uidNumber', 'gidNumber','homeDirectory', 'fullName', 'loginShell'],
paged_size=5, size_limit=1000)
for entry in c.response:
distinguished_name = entry['dn']
attributes_dict = entry['attributes']
serverA_cn = ''.join(map(str, list(attributes_dict['cn'])))
serverA_uid_number = attributes_dict['uidNumber']
serverA_gid_number = attributes_dict['gidNumber']
serverA_homedirectory = attributes_dict['homeDirectory']
serverA_fullname = attributes_dict['fullName']
serverA_loginshell = attributes_dict['loginshell']
now this above variable from ServerA needs to be comapre with object attributes from ServerB.