2
votes

I am running Tcl script for connecting Telnet port. For this script I want to store all the CMD output in a log file. how to do this in Tcl script? Script as below:

#!/usr/bin/expect -f
#!usr/bin/expect
package require Expect
spawn telnet $serverName $portNum
expect "TradeAggregator>"
send "Clients\r"
expect "Client:"
send "1\r"
expect "1-Client>"
send "Pollers\r"
expect "Client Pollers"
send "2\r"
send "n\r"
expect ">"
set passwordOption $expect_out(buffer)
set searchString "Not confirmed" 
if {[string match *$searchString* $passwordOption]} { 
puts "match found" }\ 
else { 
puts "match not found" 
xmlPasswordChange $polName 
}

all the puts output and xmlPasswordChange procedure output is not printing in the log file. can you please point out where i am doing wrong.

Thanks for your help in advance.

2

2 Answers

12
votes

You want to insert a log_file command where you want to start saving the output. For example, if you want to save all the output, then stick it to the beginning:

#!/usr/bin/expect -f

log_file myfile.log ;# <<< === append output to a file

package require Expect
spawn telnet $serverName $portNum
expect "TradeAggregator>"
send "Clients\r"
expect "Client:"
send "1\r"
expect "1-Client>"
send "Pollers\r"
expect "Client Pollers"
send "2\r"

Be default, log_file appends if the file exists, or create a new one. If you want to start a new log every time, then:

log_file -noappend myfile.log

Update

Per your question regarding why puts outputs go to the console and not the log file. The way I understand is puts will go to the console. If you want to log to the log file (open that was opened with the log_file command), then use the send_log command instead:

log_file -noappend myfile.log
send_log "This line goes into the log file"
send_user "This line goes into both the log file and console\n"
puts "This line goes to the console"

The above example introduced another command: send_user, which acts like puts and send_log combined. Note that send_user does not include a new line, so the caller should include it.

0
votes

Also We can create our own log file by stranded [open $file_name w] command and keep writing everything to that file.