Is there a way to “expect” and “send” after “inter

2019-02-11 02:38发布

问题:

So I need an output of the remote process on my stdout, but I need also to be able to "listen" to it, and send the command after matching my keyword.

I need something like this (I know this code is not correct, it's only purpose is illustration of what I need)

#!/usr/bin/expect
log_user 0
spawn ssh -o PubkeyAuthentication=no [lindex $argv 0] -n [lindex $argv 1]
expect "Password:" {send "mypassword\r"}
interact
expect "mykeyword" {send "\003\177\015"}

回答1:

If I remember right, you do this:

#!/usr/bin/expect
log_user 0
spawn ssh -o PubkeyAuthentication=no [lindex $argv 0] -n [lindex $argv 1]
expect "Password:" {send "mypassword\r"}
interact {
     "mykeyword" {
         send "\003\177\015"
         exp_continue
     }
}

You pass the things to watch out for and actions to take as arguments to interact (just like with expect) and you tell the response script to exp_continue at the end so that it keeps on interacting/expecting.