Parser ldapsearch to mysql


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Parser ldapsearch to mysql

Hi,


I'm trying to make a bash script to read LDAP (from MS active directory with ldapsearch), extract the fields 'mail', 'division', 'memberOf', 'userAccountControl', 'uidNumber', 'name', 'sAMAccountName' and save in a mysql database.

I have extracted the fields with ldapsearch but I am not clear how to parse user by user and enter it in the database.


Code:
ldapsearch -W -H ldaps://my.domainldap.com:3269 -D "CN=botreader,OU=accountsNoPerson,OU=Usuarios,DC=office,DC=domain,DC=com" "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=0))" -LLL "(&(objectclass=posixaccount)(mail=*))" 'mail' 'division' 'memberOf' 'userAccountControl' 'uidNumber' 'name' 'sAMAccountName'

But when i tried to use this ldapsearch and parser i have the last user in the result, example:
Code:
ldapsearch -W -H ldaps://my.domainldap.com:3269 -D  "CN=botreader,OU=accountsNoPerson,OU=Usuarios,DC=office,DC=domain,DC=com"   "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=0))"  -LLL "(&(objectclass=posixaccount)(mail=*))" 'mail' 'division'  'memberOf' 'userAccountControl' 'uidNumber' 'name' 'sAMAccountName' \
    | gawk -F":" ' $1=="mail" { mail = $2; sub(/^  */,"", mail); next}
    $1=="division" { division = $2; sub(/^  */,"", division);next}
    $1=="memberOf" { memberOf = $2; sub(/^  */,"", memberOf);next}
    $1=="userAccountControl" { userAccountControl = $2; sub(/^  */,"", userAccountControl);next}
    END {
        printf("%s,%s,%s,%s\n", mail,division,memberOf,userAccountControl)
          }'

Result:


Code:
EMAIL_USER,a0335f6393561hqwd4557ff5b67,CN=group01,OU=persons01,OU=Users,DC=office,DC=at,512

Anybody know any easy way to do this?

Thanks in advance.
# 2  
Welcome to the forum.


What exactly is your problem? Also, pls provide some sample data.
# 3  
Hi,


The output is like this:


Code:
....

dn: CN=User 1,OU=ACT-RES,OU=DT,OU=Person,OU=Users,DC=
 ffices,DC=domain,DC=com
memberOf: CN=vpn,OU=DT,OU=Person,OU=Users,DC=Offices,
 DC=domain,DC=com
memberOf: CN=Dept1,CN=Builtin,DC=Offices,DC=domain,DC=com
memberOf: CN=Developers,OU=DT,OU=Person,OU=Users,DC=Offices,DC
 =domain,DC=com
name: User 1
userAccountControl: 512
sAMAccountName: user1
division: 42a740c784db1612bba443d7262bba4se3a85622b0
mail: suser1@domain.com
uidNumber: 11235

dn: CN=User 2,OU=ACT-RES,OU=DT,OU=Person,OU=Users,DC=o
 ffice,DC=domain,DC=com
memberOf: CN=Dept_HCD,CN=Builtin,DC=Offices,DC=domain,DC=com
memberOf: CN=Dept_HCR,CN=Builtin,DC=Offices,DC=domain,DC=com
memberOf: CN=Dept1,CN=Builtin,DC=Offices,DC=domain,DC=com
memberOf: CN=Admins,OU=Person,OU=Users,DC=Offices,DC=domai
 n,DC=com
name: User 2
userAccountControl: 512
sAMAccountName: user2
division: baa5939bf3b161c2bba443d726af9b957e4dd77b0
mail: user2@domain.com
uidNumber: 13092

...

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Ldapsearch command not exiting
daniel333
All, New to this forum and unix here so please forgive any silly questions. I am on a Solaris 10 machine and need a script to query our Windows Domain controller to determine if a user is active or not. Man files were helpful enough and it worked. The Challenge is the program doesn't exit...... Solaris
1
Solaris
Ldapsearch problem
vasil
Hi I'm not familiar with ldap and I hope someone to help me. I need to get some attribute value from ldap DB. When i run the following command is OK: ldapsearch -h localhost -p 16611 -x -D cn=user -w passwd -b msisdn=359502479649,dc=MSISDN,DC=C-NTDB "(objectClass=SUBINNSS)" refinmocNAME the...... Red Hat
0
Red Hat
Need assistance on using ldapsearch
srhadden
Never knew of this command ldapsearch, but I would like to use it to lookup a single user and return where their office is. Is this possible? I'm totally starting from scratch. I already saw some of the gurus say read the man page, which is pretty greek when you don't know the details of...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers
LDAPSEARCH in C
tailangong
Hi all, I've been searching for ldapsearch function these few days. Still I couldn't found the solution. I would like to translate this query ldapsearch -x -LLL -h new_IP -p new_Port -D "cn=jw" -w "dummy" -b "id=2311,o=WC,c=jp" -s sub to ldap_search_ext_s() function in C program. ...... Programming
0
Programming
ldapsearch and XML
ldiaz2106
Hello all I would like to know if it's possible to extract users from Active Directory and parse the output to obtain a XML file with specific format. So the AD is a windows machine, and I would like to extract from a red hat linux serveur. I try ldapsearch command and ok I'm abble to extract...... UNIX for Advanced & Expert Users
5
UNIX for Advanced & Expert Users