Tcl 期望从 tclsh 到 shell 脚本的输出

Tcl expects output from tclsh to shell script

最近几天我一直在寻找解决我的问题的方法,所以我终于来到了这里。

我必须编写一个应该从 Linux/Debian 系统打开的 Tcl 脚本。问题是,脚本应该登录到 Cisco 路由器,然后进入特殊的 tclsh 模式,它应该在其中获取表示 txload 的 SNMP 值。片刻之后,我在 tclsh 模式(支持 SNMP)中获得了值,但我无法将其返回到我的脚本中。它仍然只存在于路由器 tclsh 模式中。我的脚本应该根据我在 tclsh 中已有的值更改一些其他值,但不再需要 tclsh。

所以这是我的问题:如何从 tclsh 中获取 $ifnum 值,但我仍然可以在该脚本中使用一些循环,因为我需要在 $ifnum 将要执行的位置创建一些循环是条件吗?

如您所见,脚本尚未完成,但正如我所说,我被困在这里并试图弄清楚:

#!/usr/bin/expect -f

set gateway [lindex $argv 0]
set serial [lindex $argv 1]
set timeout 5

spawn telnet $gateway
expect "Password:"
send "cisco\r"
expect "*>"
send "en\r"
expect "Password:"
send "cisco\r"
expect "*#"
send "set ifnumstr \[snmp_getone odczyt 1.3.6.1.4.1.9.2.2.1.1.24.6\]\r"
expect "*}"
send "regexp \{val='(\[0-9\]*)'\} $ifnumstr \{\} ifnum \r"
expect "*#"
send "puts $ifnum \r"
expect "*#"

我会让它打印出你在标记之间寻找的东西,然后在它的 RE 匹配模式下使用 expect 来获取值。

send "puts \"if\>$ifnum<if\"\r"
expect -re {if>(.*?)<if}
# Save the value in a variable in the *local* script, not the remote one
set ifnum $expect_out(1,string)