2016-11-08 8 views
0

私は小さなkshスクリプトを書いています。ここでは、いくつかの変数の値を使ってldapsearchコマンドをフォーマットし、ldapサーバーの提供された詳細が正しいかどうかを検証するコマンドを実行する必要があります。このスクリプトは、スペースがあるLDAPユーザーにとっては失敗しています。私は脱出する場合kshの文字列処理

/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 

を失敗している

私のスクリプトは、スクリプトで作成された上記のスクリプト

[[email protected] ~]# /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コマンドの

[[email protected] ~]# 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 

出力以下のもののように見えますスペースを '\'を使用してCN=userwith\ space,CN=Users,DC=MYLDAP,DC=COMのようにコマンドラインで動作しますが、それはusername="CN=userwith\ space,CN=Users,DC=MYLDAP,DC=COM"とスクリプトはまた、このよう/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

この問題を解決する方法上の任意の手がかりにスクリプト形式ので、ldapsearchコマンドを失敗し

kshのスクリプトは動作しませんか?

+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)" 
+0

に "... CN = userwith \\空間" のような二重のバックスラッシュを試みることができるはいその作業...それはまだ\\ 'は/ usr/binに/のldapsearchで失敗 – Deep

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 
+0

解決してくれてありがとう - x -H ldap://192.168.1.2 -b dc = myldap、dc = com -sサブ-o nettimeout = 10 -LLL -A -D CN = 'userwith \'スペース、CN = Users、DC = MYLDAP、DC = COM -w Passw0rd '(objectClass = *)' -z1 dn' – Deep

+0

@Deep:私はansを更新しました。これを試してもらえますか? –

+0

@Deep:もう一度ansを更新しました。 –

関連する問題