Search code examples
outputbufferexpecttruncated

buffer truncated in expect script


I am trying to use expect script for collecting information from a remote server. for logging purpose, i am suppressing the output on the screen and redirecting it to a file in the local server. Here is the script i used. this is storing the output in the local file, however the log is truncated.

#!/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)

I tried to use match_max just after expect also. even that could not help me. Any suggestion in this regard is of great help to me. Thank you very much for going though this question.


Solution

  • Two ways to approach this:

    1. write out the buffer when it's full

      First, set match_max at the start of your script. Then

      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. and much less work, use log_file to log the parts you care about.

      Don't bother with match_max, and

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