Bash脚本保存ldapsearch使用自定义格式 - Bash script to save ldapsearch with custom format

- 此内容更新于:2015-12-20
主题:

我想呼应了每个用户的LDAP到一个文本文件,但自定义格式:最终目标是以下格式:例如:我目前在终端(Ubuntu14.04):以下是得到我所需要的一切但是格式如下:注意:这是所有吐出一行。任何帮助都是感激,谢谢编辑:通过研究我发现我能做的:通过添加大典时它不会ldap评论打印输出。导致下列格式:这可以帮助很多,现在如果有一种方法来删除“uid="和",ou=…,dc=公司,dc=com”和“cn:”和“邮件:”

原文:

I am trying to echo out every user on LDAP onto one text file but with a custom format:

The end goal is to have the following format:

uid=cn <mail>

So an example would be:

jdoe=John Doe <john.doe@company.com>
jdoe1=Jane Doe <jane.doe@company.com>
...

I'm currently doing the following in terminal (Ubuntu 14.04):

echo `ldapsearch -x -h 127.0.0.1 -b "dc=company,dc=com" uid=* cn mail` >> ldap-users.txt

Which is getting everything I need however the format is as follows:

# extended LDIF # # LDAPv3 # base <dc=company,dc=com> with scope subtree # filter: uid=* # requesting: cn mail # # jdoe, Special Users, company.com dn: uid=jdoe,ou=Special Users,dc=company,dc=com cn: John Doe User mail: john.doe@company.com # jdoe1, People, company.com dn: uid=jdoe1,ou=People,dc=company,dc=com cn: Jane Doe mail: jane.doe@company.com # ....... # search result search: 2 result: 0 Success # numResponses: 1387 # numEntries: 1386

NOTE: That that's all spit out in one line.

Any help is appreciated,

Thanks

EDIT: So by researching more I found out I can do:

ldapsearch -LLL -x -h 127.0.0.1 -b "dc=covisint,dc=com" uid=* cn mail

Which by adding -LLL it will not print ldap comments on output. Which results in the following format:

dn: uid=jdoe,ou=Special Users,dc=company,dc=com cn: John Doe User mail: john.doe@company.com ................... dn: blahblahblha... etc

So that helps alot, now if there's a way to remove the "uid=" and the ",ou=...,dc=company,dc=com" and "cn:" and "mail:"

解决方案:
不太优雅,但是管道ldapsearch命令的输出通过以下似乎工作我只是试着快速和肮脏的测试:
原文:

It's not particularly elegant, but piping the output of the ldapsearch command through the following seems to work for the quick and dirty test I just tried:

| sed 's/uid=/\nuid=/'g | awk 'NR>2 { for( i=1; i<=NF; i++ ) { if ( $i ~ /uid=/ ) { printf "%s=", substr( $i, 5, index( $i, "," ) - 5 ) }; if ( $i ~ /cn:/ ) { printf "%s %s ", $(i+1), $(i+2) }; if ( $i ~ /mail:/ ) { printf "<%s>\n", $(i+1) }; } }'
楼主:谢谢,这几乎是正确的,有两个小问题,第一个用户的uid不会出现,也都写在一行(这并不是坏但它会更容易和更好的每一个用户在一个新行)..编辑:首先要清楚它消除了uid,但是没有大典时它使第一个用户的uid,但之前有一个..

(原文:Thanks, that is almost right, there are two minor problems, the first user's uid doesn't show up and also it writes everything in one line (which isn't bad but it'd be way easier and better to have each user on a new line).. Edit: Just to be clear with the -LLL it removes the first uid, but without -LLL it keeps the first user's uid but there's a = before it..)

网友:“NR>2”的开头awk脚本导致输入的第一行被忽略,所以删除,应该让你得到第一个用户。但是,我不知道为什么一切都写在一行,“\n”最终printf使每个用户应该印在一个单独的行(假设每个用户都有一个电子邮件地址在尖括号印刷)。

(原文:The "NR>2" at the beginning of the awk script causes the first line of input to be skipped, so removing that should let you get the first user. However, I don't know why everything is written on one line; the "\n" after the final printf should cause each user to be printed on a separate line (assuming each user has an email address in angle brackets that is printed).)