1
votes

My TCL/Expect script:

foreach  data $test url $urls {
    send "test app website 1\r"
    expect "*#"
    send "commit \r"
    expect "*#"
    after 2000
    exec cmd.exe /c start iexplore.exe $url
    after 3000
    exec "C:/WINDOWS/System32/taskkill.exe" /IM IEXPLORE.EXE /F
    send "show application stats $data\r"
    expect "*#"
    expect "?" {
        puts [open urllist.txt w] $expect_out(buffer)
    }

 }

The above script is working fine except storing the output of $expect_out(buffer) into a file.

I want to send $expect_out(buffer) output to "urllist.txt" file continuously.

Please suggest me a way to achieve this.

Thanks in advance.

1

1 Answers

1
votes

The following trace procedure writes the value of expect_out (buffer) to a file specific to the spawn id.

proc log_by_tracing {array element op} {
    uplevel {
            global logfile
            set file $logfile($expect_out(spawn_id))
            puts -nonewline $file $expect_out(buffer)
     }
}

The association between the spawn id and each log file is made in the array logfile which contains a pointer to the log file based on the spawn id. Such an association could be made with the following code when each process is spawned.

spawn <some_app_name_here>
set logfile($spawn_id) [open exp_buffer.log w]

The trace has to be added in the code as

trace variable expect_out(buffer) w log_by_tracing

Internally, the expect command saves the spawn_id element of expect_out after the X, string elements but before the buffer element in the expect_out array. For this reason, the trace must be triggered by the buffer element rather than the spawn_id element.

Note : If you are not bother about much of spawned process or using only one spawned process or no spawned process at all, there is a simple way of doing the same, then it would be much easy.

Consider the following example.

proc log_by_tracing {array element op} {
    uplevel {
            puts -nonewline $file $expect_out(buffer)
     }
}

set file [ open myfile.log w ]
trace variable expect_out(buffer) w log_by_tracing

set timeout 60
expect {
       quit { exit 1 }
       timeout { exp_continue }
}

If you run the code, whatever you type in console till you type 'quit', the program will run and eventually it will be recorded in the file named 'myfile.log'

You can simply add the proc log_by_tracing and the trace statement into your code. Remember with this simple way, only one instance of expect_out(buffer) can be saved.

Reference : trace, uplevel & EXploring Expect