【问题标题】:Tcl expects output from tclsh to shell scriptTcl 期望 tclsh 的输出到 shell 脚本
【发布时间】:2015-01-26 21:13:03
【问题描述】:

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

我必须编写一个应该从 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 "*#"

【问题讨论】:

    标签: linux variables tcl expect cisco


    【解决方案1】:

    我会让它打印出你在标记之间寻找的东西,然后在其 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2020-12-29
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多