如何按属性而不是 cn 对 ldap 搜索结果进行分组。使用 bash
how to group ldap search results by attributes and not by cn. using bash
我有这个从 ldap 中提取数据的 scriplet。它列出了带有 cn 和值的属性。但它遍历了一个 cn 的所有属性,然后移动到下一个。
for node in "${HOSTS[@]}"; do
LDAP_URI="ldaps://${node}:${LDAP_PORT}"
while IFS=: read -r key val; do
[[ $key = cn ]] && { cn=${val# }; continue; }
if [[ $val =~ ^\ -?[0-9]+(\.[0-9]+)?$ ]]; then
attr=$(sed 's/-/_/g' <<< "$key")l"
printf '%s, cn="%s %s\n"' "$attr" "$cn" "$val"
fi
done < <( ${LDAPSEARCH} -LLL -H "${LDAP_URI}" -x -D "${BINDDN}" -w "${LDAP_PASSWD}" -b "cn=monitor")
done;
上面的代码产生如下输出:
"connections, cn="1636 -632
"bytessent, cn="1636 492842558
"connections, cn="1389 698
"bytessent, cn="1389 389256181
但我不想按 cn 分组,而是像这样按属性分组:
"bytessent, cn="1389 389261940
"bytessent, cn="1636 711130488
"connections, cn="1389 700
"connections, cn="1636 -632
实现此目标的最佳方法是什么?
一个想法是将整个输出通过管道传输到 sort
。
我没有让 OP 工作的所有细节,所以我将使用 echo
:
进行模拟
echo '"connections, cn="1636 -632
"bytessent, cn="1636 492842558
"connections, cn="1389 698
"bytessent, cn="1389 389256181' | sort
这会生成:
"bytessent, cn="1389 389256181
"bytessent, cn="1636 492842558
"connections, cn="1389 698
"connections, cn="1636 -632
添加到 OP 的代码(见最后一行):
for node in "${HOSTS[@]}"; do
... snip ...
done | sort # send all output to sort
我猜 OP 真的想用 "
结束每一行,这样它看起来像:
"bytessent, cn="1389 389256181"
"bytessent, cn="1636 492842558"
"connections, cn="1389 698"
"connections, cn="1636 -632"
要完成这个只需要调整 printf
格式来切换最后一个 "
和 \n
:
printf '%s, cn="%s %s\n"' "$attr" "$cn" "$val" # before
printf '%s, cn="%s %s"\n' "$attr" "$cn" "$val" # after
我有这个从 ldap 中提取数据的 scriplet。它列出了带有 cn 和值的属性。但它遍历了一个 cn 的所有属性,然后移动到下一个。
for node in "${HOSTS[@]}"; do
LDAP_URI="ldaps://${node}:${LDAP_PORT}"
while IFS=: read -r key val; do
[[ $key = cn ]] && { cn=${val# }; continue; }
if [[ $val =~ ^\ -?[0-9]+(\.[0-9]+)?$ ]]; then
attr=$(sed 's/-/_/g' <<< "$key")l"
printf '%s, cn="%s %s\n"' "$attr" "$cn" "$val"
fi
done < <( ${LDAPSEARCH} -LLL -H "${LDAP_URI}" -x -D "${BINDDN}" -w "${LDAP_PASSWD}" -b "cn=monitor")
done;
上面的代码产生如下输出:
"connections, cn="1636 -632
"bytessent, cn="1636 492842558
"connections, cn="1389 698
"bytessent, cn="1389 389256181
但我不想按 cn 分组,而是像这样按属性分组:
"bytessent, cn="1389 389261940
"bytessent, cn="1636 711130488
"connections, cn="1389 700
"connections, cn="1636 -632
实现此目标的最佳方法是什么?
一个想法是将整个输出通过管道传输到 sort
。
我没有让 OP 工作的所有细节,所以我将使用 echo
:
echo '"connections, cn="1636 -632
"bytessent, cn="1636 492842558
"connections, cn="1389 698
"bytessent, cn="1389 389256181' | sort
这会生成:
"bytessent, cn="1389 389256181
"bytessent, cn="1636 492842558
"connections, cn="1389 698
"connections, cn="1636 -632
添加到 OP 的代码(见最后一行):
for node in "${HOSTS[@]}"; do
... snip ...
done | sort # send all output to sort
我猜 OP 真的想用 "
结束每一行,这样它看起来像:
"bytessent, cn="1389 389256181"
"bytessent, cn="1636 492842558"
"connections, cn="1389 698"
"connections, cn="1636 -632"
要完成这个只需要调整 printf
格式来切换最后一个 "
和 \n
:
printf '%s, cn="%s %s\n"' "$attr" "$cn" "$val" # before
printf '%s, cn="%s %s"\n' "$attr" "$cn" "$val" # after