Home
Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Parser ldapsearch to mysql

Tags
ldap ldapsearch script bash, ldapsearch, mysql, parser, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-31-2018
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  
Old 08-31-2018
Welcome to the forum.


What exactly is your problem? Also, pls provide some sample data.
# 3  
Old 09-03-2018
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 to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with output from ldapsearch command who10 UNIX for Beginners Questions & Answers 8 10-10-2016 03:04 PM
Ldapsearch command not exiting daniel333 Solaris 1 06-27-2013 08:59 PM
Ldapsearch problem vasil Red Hat 0 04-23-2013 03:53 AM
Need assistance on using ldapsearch srhadden UNIX for Dummies Questions & Answers 1 05-26-2011 12:27 PM
LDAPSEARCH in C tailangong Programming 0 07-01-2010 03:57 AM
Counting the ldapsearch results ennstate UNIX for Advanced & Expert Users 0 02-27-2010 05:10 AM
ldapsearch and XML ldiaz2106 UNIX for Advanced & Expert Users 5 02-05-2010 02:02 AM
How to trim ldapsearch results a_calder UNIX for Dummies Questions & Answers 2 01-06-2010 07:15 PM
ldapsearch time limit jayjabour UNIX for Dummies Questions & Answers 2 02-10-2009 05:50 PM
Help Needed regarding Ldapsearch script pranav.mehta3 IP Networking 0 10-10-2008 01:19 PM


All times are GMT -4. The time now is 12:13 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password