Ldap

如何從 ldapsearch 輸出中提取屬性

  • June 6, 2020

我有一個 ldap 目錄,其中包含一個名為 的類的一些實例store,如下所示:

# ldapsearch -LLL -x -b "utente=las,dc=labammsis" -s base
dn: utente=las,dc=labammsis
objectClass: store
utente: las
indirizzo: 10.1.1.1
chiave: ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAA
CFBAHTwDePWTXkV32Ggso3fxFHqJ9iPbiAiZ5TadkCIv1gF6GGVsHBUtfq+OuGwOCaDAdi/FXuxt2
5Be4K0NGlmkwPvABaT3O6pXvA2ucUNzRm/dPME+kwMPc2lWT2MYOnjZ6TBiMl/PVXqHLREZjEJNDX
4nW6C+7SgGA4d0FGIB+wKEy1gg== las@Client
modificato: 1

我只想提取 type 的屬性chiave,所以想要的輸出是這樣的:

ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAHTwDePWTXkV32Ggso3fxFHqJ9iPbiAiZ5TadkCIv1gF6GGVsHBUtfq+OuGwOCaDAdi/FXuxt25Be4K0NGlmkwPvABaT3O6pXvA2ucUNzRm/dPME+kwMPc2lWT2MYOnjZ6TBiMl/PVXqHLREZjEJNDX4nW6C+7SgGA4d0FGIB+wKEy1gg== las@Client

在網上搜尋我發現了這個有用的連結,所以我能夠做到這一點:

# ldapsearch -LLL -x -b "utente=las,dc=labammsis" -s base chiave
dn: utente=las,dc=labammsis
chiave: ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAA
CFBAHTwDePWTXkV32Ggso3fxFHqJ9iPbiAiZ5TadkCIv1gF6GGVsHBUtfq+OuGwOCaDAdi/FXuxt2
5Be4K0NGlmkwPvABaT3O6pXvA2ucUNzRm/dPME+kwMPc2lWT2MYOnjZ6TBiMl/PVXqHLREZjEJNDX
4nW6C+7SgGA4d0FGIB+wKEy1gg== las@Client

不錯,但不是我想要的。

有沒有辦法僅通過某些參數來實現我的目標,ldapsearch或者我應該操縱輸出以獲得所需的輸出?

您可以使用 禁用包裝-o ldif-wrap=no,之後它只過濾輸出,例如使用sed

ldapsearch -LLL -x -b "utente=las,dc=labammsis" -s base -o ldif-wrap=no chiave \
| sed -ne 's/^chiave: //p'

引用自:https://unix.stackexchange.com/questions/591260