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.
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:
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:
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.