Changes

Jump to: navigation, search

stoney core: OpenLDAP ldapseach and replace

1,083 bytes added, 11:17, 14 December 2020
/* Search an replace - Replace the content of sstMailFrom attribute */
</source>
Execute the searchto retrieve all occurrences of the given search filter (ldap attribute and attribute value):
<source lang='bash'>
# Get all child entries.children=$(ldapsearch -LLL -x -s children sub \ -H "$ldap_server" \
-b "$ldap_base" \
-H "$ldap_server" \
-D "$ldap_bind_account" \
-w "$ldap_bind_password" \
sstMailTemplateResellerFormatSource: txt
sstMailTemplateResellerFormatTarget: txt
</source>
As we are only interested in the dn, we add the following two lines to the search above:
<source lang='bash'>
| awk '{ if (/^ /) { sub(/^ /, ""); } else printf "\n"; printf "%s", $0 }' \
| awk '/^dn: / { print $2 }'
</source>
The final search returns a list of distinguished names, one per line (without the ldif 80 characters per line restriction):<source lang='bash'>
ldapsearch -LLL -x -s children \
-H "ldaps://ldapm.stepping-stone.ch:636$ldap_server" \ -b "ou=services,o=stepping-stone,c=ch$ldap_base" \ -D "cn=Manager,o=stepping-stone,c=ch$ldap_bind_account" \ -w "${ldap_bind_password}" \ "(sstMailFrom${ldap_attribute}=Support stepping stone GmbH <support@stepping-stone.ch>${ldap_attribute_old})" \ | awk '{ if (/^ /) { sub(/^ /, ""); } else printf "\n"; printf "%s", $0 }' \ | awk '/^dn: / { print $2 }'
</source>
 
We need to create an array of the distinguished names:
<source lang='bash'>
children=$(ldapsearch -LLL -x -s children \
-H "$ldap_server" \
-b "$ldap_base" \
-D "$ldap_bind_account" \
-w "$ldap_bind_password" \
"(${ldap_attribute}=${ldap_attribute_old})" \
| awk '{ if (/^ /) { sub(/^ /, ""); } else printf "\n"; printf "%s", $0 }' \
| awk '/^dn: / { print $2 }'
)
</source>
 
Finally, we need ti loop over all the distinguished names and replace the original (old) content to the attribute with the new content:
<source lang='bash'>
for dn in $children
do
echo "# dn: ${dn}"
echo "# changetype: modify"
echo "# replace: ${ldap_attribute}"
echo "# ${ldap_attribute}: ${ldap_attribute_new}
done
</source>
 
[[Category: OpenLDAP directory]]
3,368
edits