2015-01-26 48 views
0

我正在尋找最後幾天解決我的問題,所以最後我來到了這裏。Tcl期望從tclsh到shell腳本的輸出

我得編寫一個應該從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 "*#" 

回答

0

我會讓它打印出來的東西你'正在尋找標記之間,然後在其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)