Changes

stoney core: OpenLDAP ldapseach and replace

2,009 bytes added, 15:13, 14 December 2020
Michael moved page [[stoney core - OpenLDAP: ldapseach and replace]] to [[stoney core: OpenLDAP ldapseach and replace]]
</source>
Finally, we need ti to 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
<source lang='bash'>
# Set the following bash variables
ldap_attribute="sstMailFromdescription" # The attribute we're interested in. For example: sstMailFromdescriptionldap_attribute_old="Support stepping stone GmbH <support@stepping-stone.ch>" # Original (old) value of the attribute.ldap_attribute_new="Support stepping stone AG <support@stepping-stone.ch>" # The new value, that the original (old) value of the attribute is to be replaced with.ldap_bind_password='' # The password of "cn=Manager,o=stepping-stone,c=ch"
# Don't change these bash variables
-D "$ldap_bind_account" \
-w "$ldap_bind_password" \
"(${ldap_attribute}=*${ldap_attribute_old}*)"
</source>
The result will contain something like:
<source lang='ldif'>
dn: ousstVirtualMachine=unsuccessful1a08a4b8-8daf-43d7-b7c0-6e92cf7c1db1,ou=templates,uid=5000000virtual machines ,ou=reseller,ou=configuration,ou=b ackupvirtualization,ou=services,o=stepping-stone,c=chdescriptionobjectClass: This leaf contains the quota templates for the (online) backupsersstVirtualizationVirtualMachine vice.objectClass: sstRelationshipobjectClass: sstSpiceobjectClass: labeledURIObject
objectClass: top
objectClasssstClockOffset: organizationalUnitutcobjectClasssstDisplayName: sstTemplateSetupoukvm-1243: unsuccessfulsstMailFrom: Support stepping stone GmbH <support@: CentOS 7 (Zabbix Database)sstMemory: 17179869184sstNode: vm-node-03.stepping-stone.ch>sstMailTemplatesstOnCrash: filedestroysstOnPowerOff:///var/www/selfcare/htdocs/themes/selfcaredestroysstOnReboot: restartsstOSArchitecture: x86_64sstOSBootDevice: hdsstOSMachine: pc-inti440fx-2.stepping1sstOSType: hvmsstStatus: runningsstType: kvmsstVCPU: 10sstVirtualMachine: 1a08a4b8-8daf-43d7-b7c0-6e92cf7c1db1 sstVirtualMachinePool: d9c3cc0c-9ae7-40e5-8fb7-ba5297e835aadescription: kvm-1243: stepping stone.ch/templates/services/backup/unsuccessful/unsuccessful_mailGmbH: CentOS 7 (Zabbix Database)sstMailTemplateFormatSourcesstFeature: txtacpisstMailTemplateFormatTargetsstFeature: txtpaesstMailTemplateResellersstVirtualMachineMode: fileready for usesstVirtualMachineType:///var/www/selfcare/htdocs/themes/selfcare-int.spersistent teppingsstVirtualMachineSubType: ServersstNetworkDomainName: stepping-stone.chsstNetworkHostname: kvm-1243uid: 3733401sstBelongsToCustomerUID: 3723426sstBelongsToPersonUID: 1000000sstBelongsToResellerUID: 2000000sstSpicePort: 5906sstSpicePassword: HCfNmk6sY9PWlabeledURI: ldap:/templates/services/backup/unsuccessful/unsuccessful_mail_ressstVirtualMachine=default,ou=default,ou=default,ou=default eller,ou=linux,ou=virtual machine profiles,ou=virtualization,ou=services,o=steppinsstMailTemplateResellerFormatSource: txt g-stone,c=chsstMailTemplateResellerFormatTargetmember: txtsstVirtualMachine=default,ou=default,ou=default,ou=default,ou=linux,ou =virtual machine profiles,ou=virtualization,ou=services,o=stepping-stone,c=ch
</source>
As we are only interested in Normalize the dn, we add output (without the following two lines to the search aboveldif 80 characters per line restriction):
<source lang='bash'>
| awk '{ if (/^ /) { sub(/^ /, ""); } else printf "\n"; printf "%s", $0 }' \
| awk '/^dn: / { print $2 }'
</source>
-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>
We need to create an array of the distinguished namesthe complete result:
<source lang='bash'>
childrenresult=$(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 to loop over all the distinguished names lines of the result and replace a section (part) of the original (old) content to the attribute with the a new contentsection (part):
<source lang='bash'>
for dn in $children="" # Reset the dn to an empty stringcontent="" # Reset the content to an empty stringwhile IFS= read -r line; do if [[ "$line" =~ ^dn.* ]]; then dn=$line fi # Check, if the line starts with the ldap attribute of which we want to replace a part of the content if [[ "$line" =~ ^${ldap_attribute}.* ]]; then new_content=${line//${ldap_attribute_old}/${ldap_attribute_new}} # Replace all matches of ${ldap_attribute_old} with ${ldap_attribute_new}. fi if [ ${#line} -eq 0 ]; then
cat <<EOF | sed 's/^ \{4\}//' | ldapmodify -x \
-H "$ldap_server" \
-D "$ldap_bind_account" \
-w "$ldap_bind_password"
dn: ${dn}
changetype: modify
replace: ${ldap_attribute}
${ldap_attribute}: ${ldap_attribute_newnew_content}
EOF
fidone<<< "$result"if [ ${#dn} -ne 0 ]; then cat <<EOF | sed 's/^ \{4\}//' | ldapmodify -x \ -H "$ldap_server" \ -D "$ldap_bind_account" \ -w "$ldap_bind_password" ${dn} changetype: modify replace: ${ldap_attribute} ${new_content}EOFfi
</source>
=== Search an replace - Replace a section (part) of an attribute - Putting it all together (copy and paste version) ===
<source lang='bash'>
childrenresult=$(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 }'
)
for dn in $children="" # Reset the dn to an empty stringcontent="" # Reset the content to an empty stringwhile IFS= read -r line; do if [[ "$line" =~ ^dn.* ]]; then dn=$line fi # Check, if the line starts with the ldap attribute of which we want to replace a part of the content if [[ "$line" =~ ^${ldap_attribute}.* ]]; then new_content=${line//${ldap_attribute_old}/${ldap_attribute_new}} # Replace all matches of ${ldap_attribute_old} with ${ldap_attribute_new}. fi if [ ${#line} -eq 0 ]; then
cat <<EOF | sed 's/^ \{4\}//' | ldapmodify -x \
-H "$ldap_server" \
-D "$ldap_bind_account" \
-w "$ldap_bind_password"
dn: ${dn}
changetype: modify
replace: ${ldap_attribute}
${ldap_attribute}: ${ldap_attribute_newnew_content}
EOF
fidone<<< "$result"if [ ${#dn} -ne 0 ]; then cat <<EOF | sed 's/^ \{4\}//' | ldapmodify -x \ -H "$ldap_server" \ -D "$ldap_bind_account" \ -w "$ldap_bind_password" ${dn} changetype: modify replace: ${ldap_attribute} ${ldap_attribute}: ${new_content}EOFfi
</source>
[[Category: OpenLDAP directory]]
SLB, editor, reviewer
3,368
edits