2

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.

Piyush
  • 5,145
  • 16
  • 49
  • 71

2 Answers2

13

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.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • @Vu - i am sending the below command then checking the if else condition for calling one procedure. script calling is working and printing the puts in CMD of that calling procedure but not in log_myfile.log file. `send "n\r" expect ">" #expect * ;#this is for to get data in expect_out set passwordOption $expect_out(buffer) #puts "$passwordOption" set searchString "Not confirmed" if {[string match *$searchString* $passwordOption]} { puts "match found" }\ else { puts "match not found" xmlPasswordChange $polName }` how can i print all CMD output in log file? – Piyush Apr 12 '13 at 12:46
  • Can you update your main post with this information? I am not good at reading text like this. Sorry about that. – Hai Vu Apr 12 '13 at 15:15
  • @Vu - i have added in main post my problem. can you please look into this. – Piyush Apr 12 '13 at 21:27
  • thanks a ton Vu. after replacing puts with send_user it's working. – Piyush Apr 13 '13 at 05:06
0

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

abcdefgh
  • 83
  • 1
  • 7