【问题标题】:buffer truncated in expect script在期望脚本中截断缓冲区
【发布时间】:2016-02-11 13:33:46
【问题描述】:

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

#!/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。即使那样也帮不了我。这方面的任何建议对我都有很大的帮助。 非常感谢您回答这个问题。

【问题讨论】:

    标签: output buffer expect truncated


    【解决方案1】:

    两种解决方法:

    1. 缓冲区满时写出

      首先,在脚本的开始处设置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
      

    【讨论】:

    • 感谢Glenn 先生的宝贵解决方案。我尝试了解决方案。使用第二个我无法记录数据。第一个解决方案对我有帮助。即使现在修改后的输出是 truncated.script: log_user 0 match_max 999999999 #...setup variables set readfile [lindex $argv 3] set outfile [lindex $argv 4] set fp [open $readfile r] set log [open $outfile w] set datain [read $fp] spawn ssh $user@$host #.. expect "oracle" send "$datain\r" expect { full_buffer {puts $log $expect_out(buffer ); exp_continue} "end_expect" exit } puts $log $expect_out(buffer) close $log expect eof
    • 谢谢格伦。您的第二个解决方案现在有效。这是我使用的代码。 #!/usr/bin/expect log_user 0 设置密码 [lindex $argv 0] 设置用户 [lindex $argv 1] 设置主机 [lindex $argv 2] 设置超时 -1 设置输入文件 [lindex $argv 3] 设置输出文件 [lindex $argv 4] log_file -a $output_file set fp [open $input_file r] set data [read $fp] spawn ssh $user@$host expect "*?assword:*" send "$password\r" expect "*$ *" 发送 "su - oracle\r" 期望 "oracle" 发送 "$data\r" 期望 { "end_expect" 退出 } 期望 eof
    猜你喜欢
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多