简体   繁体   中英

Expect: regexp to include various shell prompts

I use:

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

but this doesn't always detect some prompts such as:

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

Is it possible to make a regexp detect all and any prompts?

Thanks very much in advance!


Below shows the entire output when using expect -d , as Glenn and sexpect suggested: It took sometime for me to find a way to avoid the error and finally include this long output as a code. Sorry for the delay, and thank you again!

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@rd\r" 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 "\r\n" (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 "\r\nLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  \r\nLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  \r\nLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131\r\r\n" (spawn_id exp8) match glob pattern "assword: "? no
"(yes/no)?"? no
"closed by remote host"? no
bash-4.3$
expect: does "\r\nLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  \r\nLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  \r\nLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131\r\r\nbash-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 "\r\nLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  \r\nLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  \r\nLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131\r\r\nbash-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) "\r\nLast successful login:       Thu Dec 15 08:34:08 PST 2022 10.10.10.131  \r\nLast authentication failure: Fri Dec  9 16:09:53 PST 2022 10.10.10.237  \r\nLast login: Thu Dec 15 08:34:10 2022 from 10.10.10.131\r\r\nbash-4.3$"
send: sending "sudo passwd root\r" 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 root\r\n" (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 root\r\nChanging password for root\r\n" (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 root\r\nChanging password for root\r\nNew 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 root\r\nChanging password for root\r\nNew password: "
send: sending "MyNewP@$$w@rd\r" 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 "\r\n" (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 "\r\nRe-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) "\r\nRe-enter new password: "
send: sending "MyNewP@$$w@rd\r" 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 "\r\n" (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 "\r\n" (spawn_id exp8) match regular expression "[#>$%]"? (No Gate, RE only) gate=yes re=no
expect: timed out
send: sending "exit\r" 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) "\r\nexit\r\nPasswd successfully changed\r\nbash-4.3$ exit\r\nexit\r\n"

Prompts can be literally arbitrary strings (they're user-configurable and ending with $ or # is merely a common convention) so there isn't a way to match all of them that won't also pick up literally everything else as a prompt. You can usually avoid the worst problems by setting the environment variable TERM to dumb prior to calling spawn (so you probably don't get escape codes in the prompt.) but ultimately the prompt matcher may just have to be written to the specific system (or systems) that you have.

This sort of thing is part of why sharing expect scripts is actually quite difficult; the details of the systems being automated tend to sabotage our efforts!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM