为什么 $expect_out(buffer) 是空的?
Why is $expect_out(buffer) empty?
有谁知道为什么以下代码的 expect 变量 $expect_out(buffer) 为空?
几个 SO 线程说它应该包含最后一个发送命令的输出。那为什么变量是空的?
示例如下:
#!/bin/expect
spawn ssh $MY_NODE
# Logs in
# Does stuff
expect "$"
send "$MY_COMMAND\r" # Prints i7
puts $expect_out(buffer) # Empty, even though it should clearly be i7
基本上,Expect
将使用两个可行的命令,例如 send
和 expect
。如果使用 send
,则之后必须有 expect
(在大多数情况下)。 (反之亦然不是强制性的)
这是因为如果没有它,我们将错过派生进程中发生的事情,因为 expect 会假设您只需要发送一个字符串值而不期望会话中的任何其他内容。
因此,在您的情况下,您错过了在发送命令后添加 expect 语句。数组 expect_out
将使用缓冲的内容进行更新,仅当它期望一些这样输出。
注意:您已经使用了expect "$"
。我猜你已经尝试匹配文字美元符号。这不会匹配文字美元符号,而是指定行尾。它应该用作 expect "\$"
。
send "$MY_COMMAND\r"
expect "\$"
checkExpectBuffer.exp
#!/usr/bin/expect
#This is a common approach for few known prompts
set prompt "#|%|>|\$"; # We escaped the `$` symbol with backslash to match literal '$'
set hostname xxx.xxx.xx.xx
set user dinesh
set password welcome!2E
spawn ssh $user@$hostname
expect "password:"
send "$password\r";
expect -re $prompt; # Matching the prompt with regexp
send "echo i7\r"; # I'm just echoing the value 'i7'
# Comment the below line and run the code, you won't see 'echo i7' command
# as well as the output of it in 'expect_out(buffer)'
expect -re $prompt
puts "\n-->$expect_out(buffer)<--"
有谁知道为什么以下代码的 expect 变量 $expect_out(buffer) 为空?
几个 SO 线程说它应该包含最后一个发送命令的输出。那为什么变量是空的?
示例如下:
#!/bin/expect
spawn ssh $MY_NODE
# Logs in
# Does stuff
expect "$"
send "$MY_COMMAND\r" # Prints i7
puts $expect_out(buffer) # Empty, even though it should clearly be i7
基本上,Expect
将使用两个可行的命令,例如 send
和 expect
。如果使用 send
,则之后必须有 expect
(在大多数情况下)。 (反之亦然不是强制性的)
这是因为如果没有它,我们将错过派生进程中发生的事情,因为 expect 会假设您只需要发送一个字符串值而不期望会话中的任何其他内容。
因此,在您的情况下,您错过了在发送命令后添加 expect 语句。数组 expect_out
将使用缓冲的内容进行更新,仅当它期望一些这样输出。
注意:您已经使用了expect "$"
。我猜你已经尝试匹配文字美元符号。这不会匹配文字美元符号,而是指定行尾。它应该用作 expect "\$"
。
send "$MY_COMMAND\r"
expect "\$"
checkExpectBuffer.exp
#!/usr/bin/expect
#This is a common approach for few known prompts
set prompt "#|%|>|\$"; # We escaped the `$` symbol with backslash to match literal '$'
set hostname xxx.xxx.xx.xx
set user dinesh
set password welcome!2E
spawn ssh $user@$hostname
expect "password:"
send "$password\r";
expect -re $prompt; # Matching the prompt with regexp
send "echo i7\r"; # I'm just echoing the value 'i7'
# Comment the below line and run the code, you won't see 'echo i7' command
# as well as the output of it in 'expect_out(buffer)'
expect -re $prompt
puts "\n-->$expect_out(buffer)<--"