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

时间:2022-01-26 13:53:29

I'm searching for last few days for solution to my problem so finally I came here.

我正在寻找解决问题的最后几天所以最后我来到这里。

I got to write a Tcl script that should be opened from a Linux/Debian system. The problem is, that the script should log into a Cisco router and then go into the special tclsh mode, where it should get the SNMP value which represents txload. After a moment I get the value in tclsh mode (which supports SNMP), but I can't get it back to my script. It still exists only in router tclsh mode. My script should change some other values depending on the value I already have in tclsh, but tclsh is not needed anymore.

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

So here's my question: How can I get the $ifnum value from tclsh but I could still use in that script for some loops, because I need to make some loops where the $ifnum will be the condition?

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

As you can see, the script is not finished yet, but as I said, I'm stuck here and trying to figure it out:

正如你所看到的,脚本还没有完成,但正如我所说,我被困在这里并试图弄明白:

#!/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 "*#"

1 个解决方案

#1


0  

I'd make it print out the thing you're looking for between markers, and then use expect in its RE matching mode to grab the value.

我打算在标记之间打印你想要的东西,然后在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)

#1


0  

I'd make it print out the thing you're looking for between markers, and then use expect in its RE matching mode to grab the value.

我打算在标记之间打印你想要的东西,然后在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)