【问题标题】:Expect to multiple process in Tcl with Regex期望使用正则表达式在 Tcl 中进行多个进程
【发布时间】:2014-05-20 11:20:27
【问题描述】:

我正在尝试同时生成 2 个进程,期望同时发送命令。

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

我无法获得正则表达式的匹配项,并且

中没有内容
put $expect_out(buffer)

我匹配的数据如下,

This is sample-2(1), info ACT

我正在尝试从此匹配中获取值“2”和“ACT”。

我是否必须像我为 expect 和 send 所做的那样针对特定的生成过程指定它?如果是,该怎么做?

谢谢

【问题讨论】:

  • 你能发布你匹配的数据是什么吗?
  • 我已经用我匹配的数据更新了我的问题。

标签: regex tcl expect spawn multiple-processes


【解决方案1】:

您的正则表达式根本没有尝试匹配数字周围的-2( 和)。你也许可以用这个正则表达式来匹配它们:

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

[^(]+ 匹配除左括号外的任何内容,\( 匹配左括号,\) 匹配右括号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2023-03-22
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    相关资源
    最近更新 更多