TCL期望正则表达式

问题描述 投票:8回答:2

我正在尝试编写一个脚本,该脚本通过TCL / Expect从一个系统爬升到另一个系统。它为我工作。我需要一个正则表达式,其中将expect“ $”expect“#”组合在一起,以便可以包含路径中具有任何prompt的任何系统。

#!/usr/bin/expect
# Using ssh from expect

log_user 0
spawn ssh [email protected]
expect "sword: "
send "test\r"
expect "$ "
send "ssh beta\r"
expect "# "
send "uptime\r"
expect "# "

set igot $expect_out(buffer)
puts $igot
regex tcl expect
2个回答
13
投票

使用此:

expect -re {[$#] }

关键是:添加-re标志以便我们可以匹配RE,并将RE放入{braces}以免被替换。


8
投票

更通用的解决方案:

set prompt "(%|#|\\\$) $"
expect -re $prompt

此匹配$。第二个美元符号确保仅在输入结束时才匹配该模式。

© www.soinside.com 2019 - 2024. All rights reserved.