0

I'm attempting to compare the CSV file entries with the ldap production server and report the results that don't match.

On my csv file, for example, I have two columns:

CalStateEduPersonID EmployeeNumber

Now I want to compare the entries in my file to those on the ldap production server; if they match, that's OK; if not, I want to return the employeeNumber that doesn't have the same CalStateEduPersonID and either that doesn't have the CalStateEduPersonID.

Here is the script I wrote

from ldap3 import Server, Connection, ALL
import csv

## Connection to LDAP server
server = Server('server', get_info=ALL)
conn = Connection(server, 'uid=idmsa,ou=People,ou=auth,o=csun', 'password', auto_bind=True)

## Searching and retrieving data from the server
conn.search('o=csun', '(&(uid=*))', attributes=['employeeNumber', 'calStateEduPersonID']) # |(uid=rm473760)(uid=cer52518)
idap_result = conn.entries

## validates csv's employeeNumber and calStateEduPersonID with LDAP server's current values
def check_ldap_status(empid, csid):
    for data in idap_result:
        if data['employeeNumber'].value == empid and data['calStateEduPersonID'].value in ('', None):
            return 'unavailable'
        elif data['employeeNumber'].value == empid and data['calStateEduPersonID'].value != csid:
            return 'unmatched'
        else:
            return 'matched'

## Reading data from source csv file and writing updated status to another csv file
with open('csvfile', 'r') as csvfile:
    rows = csv.reader(csvfile)
    headers = next(rows)
    for i, row in enumerate(rows):
        empid, csid, *_ = row
        if empid == '':
            continue
        status = check_ldap_status(empid, csid)
        if status in ('unmatched', 'unavailable'):
            with open('z.csv', 'a') as csvfile:
                if i == 0:
                    csv.writer(headers)
                    csv.writer([row[0], status])
                else:
                    csv.writer([row[0], status])

Can anyone suggest what am I doing wrong and suggest if the script is correct?

Also when I put my file in the script it says:

Error

I am not getting the z.csv file 

0 Answers0