0

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.

1 Answers1

1

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
    
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • thank you Mr.Glenn for your valuable solution. I tried the solutions. Using 2nd one im not able to log data.1st solution is helpful to me. hwever even now the output is truncated.script after modification: 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 – pindiproli chaitanya Feb 12 '16 at 05:59
  • thank you Glenn. your second solution worked now. here is the code i used. #!/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 input_file [lindex $argv 3] set output_file [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 "*$*" send "su - oracle\r" expect "*oracle*" send "$data\r" expect { "end_expect" exit } expect eof – pindiproli chaitanya Feb 12 '16 at 09:20