2012-04-13 19 views
0

私はアプライアンスにログインしてconfigをフェッチしてファイルに書き込もうとしています(私はsshキーを使用できません。実際には2つのログインがあります)。expectを使用して出力をループしてファイルに送信

問題は(私は、configファイルの最後〜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... 

ので、今、私はどこかで読ん提案ごとに、私は一度に1行ずつ出力をループさせるためにexpectを使用しようとしていますが、ループなしでデータを出力することはできません。ここで動作していないコードです。設定は〜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

このエラーの原因のルートはの期待マルチスレッドである:期待別のスレッドでリモートシステムとの通信を開始してのmaintスレッドの出力とその出力を同期しません。だから、もしdautherスレッドが終了する前にメインスレッドのsdtoutに何かを書き込もうとすると、Expectのデッドロックに遭遇する可能性が高いです。

関連する問題