1

Expect スクリプトを使用して Linkproof デバイスをバックアップしようとしましたが、問題が発生しました。これは私の最初のスクリプトであり、限界に達しました;)

#!/usr/bin/expect
spawn ssh @IPADDRESS
expect "username:"
# Send the username, and then wait for a password prompt.
send "@username\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "@password\r"
expect "#"
# Send the prebuilt command, and then wait for another shell prompt.
send "system config immediate\r"
#Send space to pass the pause
expect -re "^ *--More--\[^\n\r]*"
send ""
expect -re "^ *--More--\[^\n\r]*"
send ""
expect -re "^ *--More--\[^\n\r]*"
send ""
# Capture the results of the command into a variable. This can be displayed, or written to disk.
sleep 10
expect -re .*
set results $expect_out(buffer)
# Copy buffer in a file
set config [open linkproof.txt w]
puts $config $results
close $config
# Exit the session.
expect "#"
send "logout\r"
expect eof

出力ファイルの内容:

ホスト '@IP (XXX.XXX.XXX.XXX)' の信頼性を確立できません。

RSA キーのフィンガープリントは XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX です。

接続を続行しますか (はい/いいえ)? @ユーザー名

「はい」または「いいえ」を入力してください: @password

「はい」または「いいえ」と入力してください: システム構成即時

「はい」または「いいえ」を入力してください:


ご覧のとおり、コマンドの結果はファイルにありません。理由を理解するのを手伝ってくれませんか? ご協力いただきありがとうございます。

ロムアルド

4

2 に答える 2

3

待機しているテキストが実際に表示されるテキストと一致しないため、すべての「expect」ステートメントがタイムアウトになります。最初の 1 つまたは 2 つを調べてみましょう。他はすべて同じです。

あなたは言う:

expect "username:"

しかし、実際に ssh から受け取るのは次のとおりです。

The authenticity of host '@IP (XXX.XXX.XXX.XXX)' can't be established.
RSA key fingerprint is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no)?

これには文字列「username:」が含まれていないため、expect コマンドはタイムアウトになり、スクリプトは次のコマンドに進みます。

send "@username\r"

次のように送信されていることがわかります。

Are you sure you want to continue connecting (yes/no)? @username

しかし、それはこの質問に対する有効な答えではありません。

そして、出力の残りの部分は何度も同じ考えです。

于 2010-06-08T16:35:25.423 に答える