如何为tcl脚本创建日志文件

如何为tcl脚本创建日志文件,tcl,Tcl,我正在运行用于连接Telnet端口的Tcl脚本。对于这个脚本,我想将所有CMD输出存储在一个日志文件中。如何在Tcl脚本中实现这一点?脚本如下: #!/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 "

我正在运行用于连接Telnet端口的Tcl脚本。对于这个脚本,我想将所有CMD输出存储在一个日志文件中。如何在Tcl脚本中实现这一点?脚本如下:

#!/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 
}
日志文件中未打印所有puts输出和xmlPasswordChange过程输出。你能指出我哪里做错了吗


提前感谢您的帮助。

您想在开始保存输出的位置插入一个
log\u文件
命令。例如,如果要保存所有输出,请将其粘贴到开头:

#!/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"
更新 根据您关于为什么
输出转到控制台而不是日志文件的问题。我的理解是,
put
将转到控制台。如果要登录到日志文件(使用
log\u file
命令打开的文件),请改用
send\u log
命令:

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"

上面的示例引入了另一个命令:
send\u user
,它的作用类似于将
put
send\u log
组合起来。请注意,
send\u user
不包含新行,因此调用者应该包含它。

我们也可以通过搁置的[open$file\u name w]命令创建自己的日志文件,并继续将所有内容写入该文件。

@Vu-我发送下面的命令,然后检查调用一个过程的if-else条件。脚本调用正在工作,并在调用过程的CMD中打印puts,但不在log_myfile.log文件中<代码>发送“n\r”expect“>”#expect*#这用于在expect_out set passwordOption$expect_out(buffer)#put“$passwordOption”set searchString“Not confirm”中获取数据,如果{[string match*$searchString*$passwordOption]}{put“match found”}\else{put“match Not found”xmlPasswordChange$polName}
如何在日志文件中打印所有CMD输出?您能用这些信息更新您的主要帖子吗?我不擅长读这样的课文。很抱歉。@Vu-我已经在主帖子中添加了我的问题。你能看看这个吗?非常感谢。将puts替换为send_user后,它就开始工作了。
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"