0

アプライアンスにログインして構成を取得し、ファイルに書き込むことを期待して使用しようとしています (ssh キーを使用できず、アプライアンスはそれをサポートしておらず、実際には 2 つのログインがあります)。

問題は、これを使用すると、データが切り捨てられることです (構成ファイルの最後の ~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 admin@192.168.1.10
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"

どんな助けでも大歓迎です。さらに情報が必要な場合はお知らせください。

ありがとう!

4

2 に答える 2

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
...
于 2013-02-20T10:37:33.183 に答える