如何在使用expect'send'命令时将输出存储在变量中

use*_*829 8 tcl expect send output

谢谢.

但是需要帐户和密码.所以我必须发送它们然后发送ovs-vsctl命令.

脚本是这样的:

spawn telnet@ip 

expect -re "*login*" {
       send "root"
}

expect -re "password*" {
       send "****"
}

send "ovs-vsctl *******"
Run Code Online (Sandbox Code Playgroud)

我想存储这个命令的输出send "ovs-vsctl ****",但很多次我得到一些输出命令"发送"密码"",我怎么能得到输出send "ovs-vsctl****".命令的输出send "ovs-vsctl ***是两个字符串,每个字符串占一行.

谢谢.

gle*_*man 9

也许:

log_user 0                   ;# turn off the usual output
spawn telnet@ip 
expect -re "*login*"
send "root\r"
expect -re "password*"
send "****\r"
send "ovs-vsctl *******"
expect eof
puts $expect_out(buffer)     ;# print the results of the command
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 5

Expect可以使用输入缓冲区,它包含从交互式应用程序返回的所有内容,这意味着过程输出输入(只要从远程设备回显,通常就是这种情况).

expect命令用于从输入缓冲区恢复文本.每次找到匹配项时,清除直到该匹配结束的缓冲区,并保存到$ expect_out(缓冲区).实际匹配保存到$ expect_out(0,string).然后缓冲区重置.

在您的情况下,您需要做的是将输出与expect语句匹配,以获得您想要的.

在您的情况下,我要做的是在发送密码后匹配远程设备提示,然后在发送命令后再次匹配.这样,最后一次匹配后的缓冲区将保存所需的输出.

有点像:

[...]
expect -re "password*" {
       send "****"
}

expect -re ">"

send "ovs-vsctl *******\r"

expect -re ">"  # Better if you can use a regexp based on your knowledge of device output here - see below

puts $expect_out(buffer)
Run Code Online (Sandbox Code Playgroud)

通过基于您对输出的了解使用正则表达式进行匹配,您应该只能提取命令输出而不能提取回显命令本身.或者你总是可以通过使用regexp命令在事后做到这一点.

希望有所帮助!