Expect TCL シェルスクリプトでパスワードが正しくない場合はループを終了する

Expect TCL シェルスクリプトでパスワードが正しくない場合はループを終了する

SSHを使用してサーバー1からサーバー2に接続するようにTCLにコードスニペットを作成しました。ユーザーがサーバー1とサーバー2の間にパスワードのない通信を確立したか、パスワードが必要な場合があります。コードは、以下の3つの可能な状況をすべて処理する必要があります。

  1. サーバー1とサーバー2の間でパスワードのないログインが有効になっている場合
  2. パスワードのないログインが有効にならず、パスワードが表示されます。プロンプト
  3. ユーザーが間違ったパスワードを入力すると、2番目のパスワード:プロンプトが表示され、ここでctrl + cと入力して終了します。

以下のコードを見つけてください

#!/usr/bin/expect -f
lassign $argv 1 2 
spawn ssh -o StrictHostKeyChecking=no $1@$2

expect {
      "*]#" { send -- "sleep 0\n" }  #Prompt when passwordless is active and user have logged in
      "Password: " {send -- "$2\n"   #Password prompt when no passwordless is active.
             exp_continue }
      "Password: " {send -- "^C" }   # Second Password Prompt when the wrong password is entered 
                                       in previous step. From here code should exit with a message
       }
 expect "*]#"                # After SSH is successful

2番目のパスワードを処理できません。間違ったパスワードの入力を求められます。ここで、コードはユーザーに適切なメッセージで終了する必要があります。 ./testssh.exp ユーザー名パスワードスクリプトを実行しています。

ベストアンサー1

おそらく:

set count 0
expect {
    "Password:" {
        if {[incr count] == 2} {
            # this is the 2nd time, send Ctrl+C
            send -- \x3
            expect eof
            error "incorrect password"
        } else {
            send -- "$2\r"
            exp_continue
        }
    }
    "$prompt"
}
# successfully logged in.
send -- something 
...

おすすめ記事