user1116993
user1116993

Reputation: 21

How can I configure a shell script which will parse ldap data in ldif format for each dn:

I have a LDIF database something like this.

dn: uid=user1,ou=People,dc=example,dc=com
mail: [email protected]
passwordexpirationtime: 20120113203000Z

dn: uid=user2,ou=People,dc=example,dc=com
mail: [email protected]
passwordexpirationtime: 20120113203000Z

dn: uid=user3,ou=People,dc=example,dc=com
mail: [email protected]
passwordexpirationtime: 20120113203000Z

How can I configure a shell script to parse each dn: check the value of passwordexpirationtime, compare it with current date. if less then 10 days then send a mail to value in mail attribute ?

If I search the value of attribute by grep passwordexpirationtime |awk -F ':' '{print $2}' It will return value for all dn: how will I know which mail: is associated by which dn:

Upvotes: 2

Views: 4308

Answers (4)

Samus_
Samus_

Reputation: 2992

here's a solution in python, since that seems to be the language you're using:

with open('ldif.txt') as f:
    for line in f:
        if line.startswith('passwordexpirationtime'):
            _, date = line.split(': ')
            print date

Upvotes: 0

potong
potong

Reputation: 58483

This might work for you:

awk '{print $4,$6}' RS='' ldif.txt
[email protected] 20120113203000Z
[email protected] 20120113203000Z
[email protected] 20120113203000Z

Now all you have to do is add some date arithmetic, like so:

awk '{if(systime()+(10*24*60*60)-mktime(gensub(/(....)(..)(..)(..)(..)(..)(.)/,"\\1 \\2 \\3 \\4 \\5 \\6 \\7",1g,$6))>0)print $4}' RS='' ldif.txt

Upvotes: 0

Samus_
Samus_

Reputation: 2992

you can tell awk to split the input by different separators:

BEGIN { RS="\n\n"; FS="\n" }

that way it'll split the records as blocks and the fields as lines, then you can get the third field of each record, strip the date and compare it.

but if you have a python script to check the date, why don't you just do it all in python?

Upvotes: 1

tinychen
tinychen

Reputation: 2099

You could transform the text like this:

mail: [email protected] passwordexpirationtime: 20120113203000Z

mail: [email protected] passwordexpirationtime: 20120113203000Z

mail: [email protected] passwordexpirationtime: 20120113203000Z

use this scripts:

cat ldif.txt | tr '\n' '|' | sed 's/||/\n/g' | awk -F"|" '{print $2,$3}'

Upvotes: 0

Related Questions