Search code examples
regextclexpectspawnmultiple-processes

Expect to multiple process in Tcl with Regex


I am trying to spawn 2 process at a same time with expect and sending commands simultaneously.

spawn telnet $IP1 $Cons1
set id1 $spawn_id
set spawns(1) $spawn_id
set logfile($spawn_id) [open "./$IP1-$Cons1-logs.txt" a]

spawn telnet $IP2 $Cons2
set id2 $spawn_id
set spawns(2) $spawn_id
set logfile($spawn_id) [open "./$IP2-$Cons2-logs.txt" a]


trace add variable expect_out(buffer) write log_by_trace
expect -i spawns(1)  "]"
expect -i spawns(2) "]"
send -i spawns(1) "\r\r\r"
send -i spawns(2) "\r\r\r"
expect -i spawns(1) ">"
expect -i spawns(2) ">"
send -i spawns(1) "sometext\r"
expect -i spawns(1) {
         #Here, match itself is not happening. Does it has anything to do with '-i spawn(1)'
        -re {This\s+is\s+sample(\d).*info\s+(\w{3})} {}
        timeout {puts "timeout happened"}
}
#Here, i am getting no such element for the below 2 statements and 
#there is no such output in expect_out(buffer)
puts $expect_out(buffer)
set x $expect_out(1,string)
set y $expect_out(2,string)
puts $x;
puts $y
send -i spawns(2) "sometext\r"
expect -i spawns(2) {
        -re {This\s+is\s+sample(\d).*info\s+(\w{3})} {}
        timeout {puts "timeout happened"}
}
set slot $expect_out(1,string)
set card_state $expect_out(2,string)
 set x $expect_out(1,string)
set y $expect_out(2,string)
puts $x;
puts $y

I am not able to get the match for the regex and there is not content in

put $expect_out(buffer)

The data I am matching is as follows,

This is sample-2(1), info ACT

I am trying to get the value '2' and 'ACT' from this match.

Do I have to give it specific to the particular spawn process like I did for expect and send ? If yes, how to do so ?

Thanks


Solution

  • Your regex isn't trying to match the-2( and ) around the digit at all. You could perhaps match them with this regex:

    This\s+is\s+sample[^(]+\((\d)\).*info\s+(\w{3})
    

    [^(]+ matches anything except opening parens, \( is to match the opening paren and \) the closing paren.