我正在写小ksh脚本,我需要使用少量变量的值格式化ldapsearch命令并运行该命令来验证ldap服务器的提供的详细信息是否正确。对于具有空间的ldap用户,该脚本失败。
我的脚本看起来像下面的内容
[root@c1n3 ~]# cat /tmp/test
#!/usr/bin/ksh
set -x
username="CN=userwith space,CN=Users,DC=MYLDAP,DC=COM"
pswd="Passw0rd"
cmd="/usr/bin/ldapsearch -x -H ldap://192.168.1.2 -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A "
cmd+="-D $username -w $pswd"
cmd+=" (objectClass=*) -z1 dn"
print $cmd
retstr="$($cmd 2>/tmp/ldapfail.tmpwrite)"
ret=$?
print "retstr=$retstr"
print "ret=$ret"
print "/tmp/ldapfail.tmpwrite : "
[[ $ret -ne 0 ]] && print "ERROR:--------------" && cat /tmp/ldapfail.tmpwrite
输出上述脚本
[root@c1n3 ~]# /tmp/test
+ username=CN='userwith space,CN=Users,DC=MYLDAP,DC=COM'
+ pswd=Passw0rd
+ cmd='/usr/bin/ldapsearch -x -H ldap://192.168.1.2 -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A '
+ cmd+='-D CN=userwith space,CN=Users,DC=MYLDAP,DC=COM -w Passw0rd'
+ cmd+=' (objectClass=*) -z1 dn'
+ print /usr/bin/ldapsearch -x -H ldap://192.168.1.2 -bdc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A -D CN=userwith space,CN=Users,DC=MYLDAP,DC=COM -w Passw0rd '(objectClass=*)' -z1 dn
/usr/bin/ldapsearch -x -H ldap://192.168.1.2 -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A -D CN=userwith space,CN=Users,DC=MYLDAP,DC=COM -w Passw0rd (objectClass=*) -z1 dn
+ /usr/bin/ldapsearch -x -H ldap://192.168.1.2 -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A -D CN=userwith space,CN=Users,DC=MYLDAP,DC=COM -w Passw0rd '(objectClass=*)' -z1 dn
+ 2> /tmp/ldapfail.tmpwrite
+ retstr=''
+ ret=49
+ print retstr=
retstr=
+ print ret=49
ret=49
+ print '/tmp/ldapfail.tmpwrite : '
/tmp/ldapfail.tmpwrite :
+ [[ 49 -ne 0 ]]
+ print ERROR:--------------
ERROR:--------------
+ cat /tmp/ldapfail.tmpwrite
ldap_bind: Invalid credentials (49)
additional info: 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1
在脚本中创建的ldapsearch命令失败
/usr/bin/ldapsearch -x -H ldap://192.168.1.2 -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A -D CN=userwith space,CN=Users,DC=MYLDAP,DC=COM -w Passw0rd '(objectClass=*)' -z1 dn
如果我在命令行上使用'{'(例如CN=userwith\ space,CN=Users,DC=MYLDAP,DC=COM
)来逃避空间它可以工作,但它在ksh脚本中不起作用
username="CN=userwith\ space,CN=Users,DC=MYLDAP,DC=COM"
的脚本也会失败,因为脚本以这种方式格式化ldapsearch命令/usr/bin/ldapsearch -x -H ldap://192.168.1.2 -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A -D CN='userwith\' space,CN=Users,DC=MYLDAP,DC=COM -w Passw0rd '(objectClass=*)' -z1 dn
有关如何解决此问题的任何线索?
答案 0 :(得分:0)
您需要将命令存储在数组中。这样,任何包含空格的数组元素仍将被视为整个单词。
username="CN=userwith space,CN=Users,DC=MYLDAP,DC=COM"
pswd="Passw0rd"
# build up the command as an array
cmd=( /usr/bin/ldapsearch -x -H ldap://192.168.1.2 -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A )
cmd+=( -D "$username" -w "$pswd" ) # variables must be quoted here
cmd+=( "(objectClass=*)" -z1 dn )
# print the contents, note the different outputs
print "${cmd[*]}"
printf "%q " "${cmd[@]}"; echo
# execute it
retstr="$("${cmd[@]}" 2>/tmp/ldapfail.tmpwrite)"
答案 1 :(得分:0)
你可以在你的剧本中试试吗;
LDAPHOST="ldap://192.168.1.2"
username="CN=\"userwith\ space,CN=Users,DC=MYLDAP,DC=COM\""
pswd="Passw0rd"
cmd="/usr/bin/ldapsearch -x -H "${LDAPHOST}" -b dc=myldap,dc=com -s sub -o nettimeout=10 -LLL -A "
cmd+="-D "${username}" -w "${pswd}" (objectClass=*) -z1 dn"
echo $cmd