gpt4 book ai didi

linux - Tcl 期望从 tclsh 输出到 shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:03 25 4
gpt4 key购买 nike

过去几天我一直在寻找问题的解决方案,所以最后我来到了这里。

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

关于linux - Tcl 期望从 tclsh 输出到 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com