Query on python ldif module

218 Views Asked by At

This may be already answered. But i couldn't able to find any good material online or on stackoverflow. I am trying to parse the below LDIF file and write it to a new file removing some DN in the file. i saw LDIFParser, LDIFWriter and some examples. But not a good one. Can some one throw some light here.or any online materials

dn: uid=rasannak,ou=people,dc=LNU,dc=edu
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
objectclass: peoplefinderPerson
givenname: rasanna
cn: rasanna Kumaravel
sn: rasanna
telephoneNumber: +1 (314) 9777777
mail: [email protected]
uid: prasannak
title: Executive Assistant to the President and Board Administrator
campus: view Campus
building: Day Hall
room: 206
status: Faculty/Staff
ou: Office
physicalDeliveryOfficeName: Day Hall
1

There are 1 best solutions below

1
Sokre On

The example you are looking for is provided on: https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldif.html. Scroll to the end of the page.

Goes like this:

import sys
from ldif import LDIFParser,LDIFWriter

SKIP_DN = ["uid=foo,ou=People,dc=example,dc=com",
    "uid=bar,ou=People,dc=example,dc=com"]

class MyLDIF(LDIFParser):
    def __init__(self,input,output):
    LDIFParser.__init__(self,input)
    self.writer = LDIFWriter(output)

def handle(self,dn,entry):
    if dn in SKIP_DN:
       return
    self.writer.unparse(dn,entry)

parser = MyLDIF(open("input.ldif", 'rb'), sys.stdout)
parser.parse()