[英]Running Spawn/Expect to Join Active Directory
不太確定這是否是提出此問題的合適論壇。 但是我確實看到了很多關於使用期望/生成/發送的問題,所以就到這里了。 我正在構建一個bash腳本以加入Active Directory域。 當我嘗試執行腳本時,出現以下錯誤:
spawn realm join --user="foouser@foo.local" --computer-ou="OU=Foo,OU=Foo Servers,DC=foo,DC=local" FOO.LOCAL
realm: Specify one realm to join
send: spawn id exp6 not open
while executing
"send "foobarpassword\r""
我不確定為什么領域會抱怨看到多個領域,但這是我的bash腳本:
#!/bin/bash
ad_user="foouser@foo.local"
ad_password="bar"
ad_ou="OU=Foo,OU=Foo Servers,DC=foo,DC=local"
ad_domain="FOO.LOCAL"
expect <<-EOD
spawn realm join --user="$ad_user" --computer-ou="$ad_ou" "$ad_domain"
expect "Password for foouser@foo.local:"
send "$ad_password\r"
interact
EOD
有人可以告訴我什么可能導致領域抱怨多個域嗎? 為什么期望不起作用?
更新:我向腳本添加了更多詳細信息,並得到以下錯誤消息:
spawn realm join --user="foouser@FOO.LOCAL" --computer-ou="OU=Foo,OU=Foo Servers,DC=foo,DC=local" FOO.LOCAL
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {883}
Gate keeper glob pattern for 'Password for foouser@FOO.LOCAL: ' is 'Password for foouser@FOO?LOCAL: '. Activating booster.
expect: does "" (spawn_id exp6) match regular expression "Password for foouser@FOO.LOCAL: "? Gate "Password for foouser@FOO?LOCAL: "? gate=no
realm: Specify one realm to join
expect: does "realm: Specify one realm to join\r\n" (spawn_id exp6) match regular expression "Password for foouser@FOO.LOCAL: "? Gate "Password for foouser@FOO?LOCAL: "? gate=no
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "realm: Specify one realm to join\r\n"
send: sending "bar\n" to { exp6 send: spawn id exp6 not open
while executing
"send -- "bar\n""
您的變量ad_ou="OU=Foo,OU=Foo Servers,DC=foo,DC=local"
包含空格。 當您在bash的“此處文檔” <<-功能中使用它時,它將生成完全不含“”的期望腳本。 重擊代碼:
spawn realm join --user="$ad_user" --computer-ou="$ad_ou" "$ad_domain"
將生成預期的代碼
spawn realm join --user=foouser@foo.local --computer-ou=OU=Foo,OU=Foo Servers,DC=foo,DC=local FOO.LOCAL
可以期待,但正如我所說,您的變量包含空格。 您需要期望中的報價。 有兩種方法:
spawn realm join --user=\""$ad_user"\" --computer-ou=\""$ad_ou"\" \""$ad_domain"\"
或者,您可以在{}中使用Expect嚴格引用:
spawn realm join --user={"$ad_user"} --computer-ou={"$ad_ou"} {"$ad_domain"}
PS:您也可以按預期的方式編寫它,而無需bash:
#!/usr/bin/expect -f
set ad_user "foouser@foo.local"
set ad_password "bar"
set ad_ou "OU=Foo,OU=Foo Servers,DC=foo,DC=local"
set ad_domain "FOO.LOCAL"
spawn realm join --user="$ad_user" --computer-ou="$ad_ou" "$ad_domain"
expect "Password for $ad_user:"
send "$ad_password\r"
interact
@komar的答案讓我開始思考它們的引用。 Tcl引號僅在單詞的第一個字符時才是特殊的 (參考: https : //tcl.tk/man/tcl8.6/TclCmd/Tcl.htm ,規則4和6)。
例如,考慮以下交互式tclsh會話:
% proc spawn args {puts $args}
% set ad_user "foouser@foo.local"
foouser@foo.local
% set ad_password "bar"
bar
% set ad_ou "OU=Foo,OU=Foo Servers,DC=foo,DC=local"
OU=Foo,OU=Foo Servers,DC=foo,DC=local
+% set ad_domain "FOO.LOCAL"
FOO.LOCAL
%
% spawn realm join --user="$ad_user" --computer-ou="$ad_ou" "$ad_domain"
realm join --user=\"foouser@foo.local\" {--computer-ou="OU=Foo,OU=Foo Servers,DC=foo,DC=local"} FOO.LOCAL
請注意如何保護雙引號。 這表示您正在將文字雙引號發送到realm
命令。
因此,請嘗試以下操作:更改
spawn realm join --user="$ad_user" --computer-ou="$ad_ou" "$ad_domain"
# ......................^........................^
至
spawn realm join "--user=$ad_user" "--computer-ou=$ad_ou" "$ad_domain"
# ...............^.................^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.