2012-04-13 57 views
0

我想使用期望登錄的設備,獲取配置,並將其寫入一個文件(我不能使用SSH密鑰,設備不支持,而事實上有兩個登錄)。使用期望遍歷輸出發送到文件

的問題是,當我用這個,數據被截斷(我只得到了配置文件的最後〜100線):

...snip... 
match_max 100000 
set timeout 150 
set output [open "outputfile.txt" "w"] 
set config $expect_out(buffer) 
puts $output $config 
close $output 
...snip... 

所以現在,每一個建議,我在其他地方見過,我是試圖使用期望來循環輸出,一次一行,但我無法像沒有循環一樣獲得數據輸出。這裏是不工作的代碼。配置是〜700行。

#!/usr/bin/expect 
match_max 50000 
spawn ssh [email protected] 
expect "password" 
send "password1\r" 
expect "user" 
send "root\r" 
expect "password" 
send "password2\r" 

set outcome {} 
set writeout [open "outputfile.txt" "w"] 

expect "device" 
exp_send "show running\r" 
expect { 
     -regexp {.*}{ 
     set outcome "${outcome}$expect_out(0,string)" 
     exp_continue 
     } 
} 
puts $writeout $outcome 
close $writeout 

expect "device" 
send "exit\r" 
send "yes\r" 

任何幫助將不勝感激。讓我知道你是否需要更多信息。

謝謝!

回答

0

您提供的循環相當於等待超時(這是默認10秒作爲腳本不設置的話)。

多久是show running打印它的輸出?這可能是值得等待的是在命令結束後表現出一定的模式(我想這是"device",但可能是更好的提供更具體的終端條件)。

exp_send "show running\n" 
expect "show running" ;#don't want the command present in captured output 
set timeout 60 
expect { 
    "device" { set raw_outcome $expect_out(0,string) } 
    timeout { error "command didn't finish in 60 sec?' } 
} 
#now the raw_outcome contains everything what was displayed up-to the "device" 
#including this string, so filter out the terminal condition: 
regexp {(.*)device} $raw_outcome match outcome 
puts $writeout $outcome 
... 
0

此錯誤原因的根源在於Expect的多線程:期望在一個單獨的線程開始與遠程系統進行通信,並且不同步其與MAINT線程的輸出端輸出。所以,如果你嘗試在期待一個dauther線程完成,你很可能滿足僵局之前寫的東西到主線程的sdtout。