1つのホストにsshを使用し、いくつかのコマンドを適用してから、デバイスをシャットダウンし、別のデバイスとしてsshを使用して別のコマンドを適用します。

1つのホストにsshを使用し、いくつかのコマンドを適用してから、デバイスをシャットダウンし、別のデバイスとしてsshを使用して別のコマンドを適用します。

こんにちは。 Expectを使用して2つのデバイス間のネットワーク問題を解決しようとしています。とにかく、私が達成したいのは、Expectを使用して2つのデバイスをインポートしてコマンドを適用するスクリプトを実行することです。

  1. スクリプトでは、ユーザーは2つのホスト名を入力する必要があります。
  2. 変数ユーザー、パスワード、およびプロンプトの設定
  3. 最初のデバイスの作成プロセスSSH
  4. トラブルシューティングのためのコマンドの表示

上記はすべてうまくいきます....別のホスト名でsshを試みると問題が発生します。デフォルトでは、私のスクリプトは10秒後にタイムアウトし、他のデバイスにsshをしません。

素晴らしい仕事と助けをしてくれたこのコミュニティに感謝します。

これはコードです

#!/usr/local/bin/expect -f

exec tput clear >@ stdout
set timeout 10

puts "Enter hostname to connect"
set c [gets stdin]

puts "which is the destination hostname?"
set dhostname [gets stdin]

#setting password variable, reading it from file
set pa [open "pass.txt" r]
set pass [read $pa]
close $pa

#setting user variable
set user xxxx

#setting prompt variable
set prompt "(%|#|\\$|>|~|:) $"

spawn -noecho ssh $user@$c;

expect {
"Are you sure you want to continue connecting (yes/no)" {send {yes};
send "\r"
exp_continue}

"assword:" {
        send "$pass\r"
        expect -re $prompt

}
}
##commands to execute on the device
send "show system uptime\r";

expect "$prompt"

send "exit\r";

expect -re $prompt

send "ssh $user@$dhostname";

ベストアンサー1

おすすめ記事