期望:regexp包含各种shell提示符



我使用:

set prompt {[#>$%]}
...
expect -re $prompt
...

,但这并不总是检测到一些提示,如:

/home/user1$
root#
bash-4>
etc.

是否有可能使regexp检测所有和任何提示?

提前感谢!

下面的

显示了使用expect -d时的整个输出,正如Glenn和sexexpect所建议的:我花了一些时间才找到一种方法来避免错误,并最终将这个长输出作为代码包含。抱歉耽误了您的时间,再次感谢您!

spawn ssh -t user@example.net
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {36}
expect: does "" (spawn_id exp8) match glob pattern "assword: "? no
"(yes/no)?"? no
"closed by remote host"? no
(user@example.net) Password:
expect: does "r(user@example.net) Password: " (spawn_id exp8) match glob pattern "assword: "? yes
expect: set expect_out(0,string) "assword: "
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) "r(user@example.net) Password: "
send: sending "MyNewP@$$w@rdr" to { exp8 }
expect: continuing expect
expect: does "" (spawn_id exp8) match glob pattern "assword: "? no
"(yes/no)?"? no
"closed by remote host"? no

expect: does "rn" (spawn_id exp8) match glob pattern "assword: "? no
"(yes/no)?"? no
"closed by remote host"? no
Last successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131
Last authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237
Last login: Thu Dec 15 08:34:10 2022 from 10.10.10.151
expect: does "rnLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  rnLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  rnLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131rrn" (spawn_id exp8) match glob pattern "assword: "? no
"(yes/no)?"? no
"closed by remote host"? no
bash-4.3$
expect: does "rnLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  rnLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  rnLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131rrnbash-4.3$ " (spawn_id exp8) match glob pattern "assword: "? no
"(yes/no)?"? no
"closed by remote host"? no
expect: timed out
Gate keeper glob pattern for '[#>$%]' is ''. Not usable, disabling the performance booster.
expect: does "rnLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  rnLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  rnLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131rrnbash-4.3$ " (spawn_id exp8) match regular expression "[#>$%]"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "$"
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) "rnLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  rnLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  rnLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131rrnbash-4.3$"
send: sending "sudo passwd rootr" to { exp8 }
Gate keeper glob pattern for '(N|n)ew (P|p)assword: ' is ''. Not usable, disabling the performance booster.
expect: does " " (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no
sud
expect: does " sud" (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no
o passwd root
expect: does " sudo passwd rootrn" (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no
Changing password for root
expect: does " sudo passwd rootrnChanging password for rootrn" (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no
New password:
expect: does " sudo passwd rootrnChanging password for rootrnNew password: " (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "New password: "
expect: set expect_out(1,string) "N"
expect: set expect_out(2,string) "p"
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) " sudo passwd rootrnChanging password for rootrnNew password: "
send: sending "MyNewP@$$w@rdr" to { exp8 }
expect: continuing expect
expect: does "" (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no

expect: does "rn" (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no
Re-enter new password:
expect: does "rnRe-enter new password: " (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "new password: "
expect: set expect_out(1,string) "n"
expect: set expect_out(2,string) "p"
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) "rnRe-enter new password: "
send: sending "MyNewP@$$w@rdr" to { exp8 }
expect: continuing expect
expect: does "" (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no

expect: does "rn" (spawn_id exp8) match regular expression "(N|n)ew (P|p)assword: "? (No Gate, RE only) gate=yes re=no
"Password:"? no
"again:"? no
"Segmentation Fault"? no
expect: timed out
Gate keeper glob pattern for '[#>$%]' is ''. Not usable, disabling the performance booster.
expect: does "rn" (spawn_id exp8) match regular expression "[#>$%]"? (No Gate, RE only) gate=yes re=no
expect: timed out
send: sending "exitr" to { exp8 }
exit
Passwd successfully changed
bash-4.3$ exit
exit
expect: read eof
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) "rnexitrnPasswd successfully changedrnbash-4.3$ exitrnexitrn"

提示符可以是任意字符串(它们是用户可配置的,以$#结尾只是一种常见的惯例),因此没有一种方法可以匹配所有的提示符,而不会同时将其他所有内容作为提示符。您通常可以通过在调用spawn之前将环境变量TERM设置为dumb来避免最严重的问题(因此您可能不会在提示符中获得转义代码!),但最终提示匹配器可能只需要写入您拥有的特定系统(或多个系统)。

这类事情是为什么共享期望脚本实际上相当困难的部分原因;自动化系统的细节往往会破坏我们的努力!