Changes

stoney core: OpenLDAP ldapseach and replace

1,397 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 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'>
dn="" # Reset the dn to an empty stringcontent="" # Reset the content to an empty string
while 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
echo "# Before: $line" linenew_content=${line//stepping stone GmbH${ldap_attribute_old}/stepping stone AG${ldap_attribute_new}} # Replace all matches of pattern ${ldap_attribute_old} with string${ldap_attribute_new}. website= fi if [ ${website/#line} -eq 0 ]; then cat <<EOF | sed 's/^ \{4\}//' | ldapmodify -x \ -H "$ldap_server" \/} echo -D "# After: $lineldap_bind_account"\ -w "$ldap_bind_password" ${dn} changetype: modify replace: ${ldap_attribute} ${new_content}EOF
fi
done <<< "$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}
EOF
fi
</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} changetype: modify replace: ${ldap_attribute} ${ldap_attribute}: ${new_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: ${dn}
changetype: modify
replace: ${ldap_attribute}
${ldap_attribute}: ${ldap_attribute_newnew_content}
EOF
donefi
</source>
[[Category: OpenLDAP directory]]
SLB, editor, reviewer
3,368
edits