期望脚本中的缓冲区被截断

buffer truncated in expect script

我正在尝试使用 expect 脚本从远程服务器收集信息。出于日志记录的目的,我抑制了屏幕上的输出并将其重定向到本地服务器中的文件。这是我使用的脚本。这将输出存储在本地文件中,但是日志被截断了。

#!/usr/bin/expect
log_user 0
set password [lindex $argv 0]
set user [lindex $argv 1]
set host [lindex $argv 2]
set timeout -1
set file "/tmp/b"
set fp [open "/tmp/gather_info" r]
set data [read $fp]
spawn ssh $user@$host
expect "*?assword:*"
send "$password\r"
expect "*$*"
send "su - oracle\r"
expect "*oracle*"
send "$data\r"
expect {
    "end_expect" exit
}
expect eof
match_max 10000
puts $file $expect_out(buffer)

我也尝试在 expect 之后使用 match_max。即使那样也帮不了我。在这方面的任何建议对我都有很大帮助。 非常感谢您回答这个问题。

解决这个问题的两种方法:

  1. 缓冲区满时写出

    首先,在脚本的 start 处设置 match_max。那么

    set file "/tmp/b"
    set log [open $file w]
    # ...
    send "$data\r"
    expect {
        full_buffer {puts $log $expect_out(buffer); exp_continue}
        "end_expect" exit
    }
    puts $log $expect_out(buffer)
    close $log
    expect_eof
    
  2. 工作量少得多,使用log_file记录您关心的部分。

    不用理会match_max,还有

    set file "/tmp/b"
    # ...
    log_file $file
    send "$data\r"
    expect {
        "end_expect" exit
    }
    expect_eof