Linuxシステムでは、スクリプトは実行されないと予想されます。

Linuxシステムでは、スクリプトは実行されないと予想されます。

マシンの1つにsshを接続する必要がある場合は、次のコマンドが表示され、「yes」と入力すると正常に動作し、以下のようにログインできます。

ssh [email protected]
The authenticity of host '192.168.1.177 (192.168.1.177)' can't be established.
ED25519 key fingerprint is SHA256:KqI6oKKY1JOH+OJZzCYObPdkMVNNwhkaMGTYgx/fDxE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.177' (ED25519) to the list of known hosts.
localhost ~ # 

期待されるスクリプトで同じことを試していますが、次のエラーが発生します。

 ./expectscriptssh.sh 192.168.1.177
spawn ssh [email protected]
invalid command name "fingerprint"
    while executing
"fingerprint"
    invoked from within
"expect "Are you sure you want to continue connecting (yes/no/[fingerprint])? ""
    (file "./expectscriptssh.sh" line 4).

以下は予想されるスクリプトです。

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect "Are you sure you want to continue connecting (yes/no/[fingerprint])? "
send "yes\r"

誰でもこの問題を解決する方法を提案できますか?

ベストアンサー1

TCLの[]ペアは、POSIXシェルの$()のようにコマンド置換を実行する呼び出しです。

利用可能なリソースがある場合、autoexpectExpectスクリプトを作成する最も簡単な方法は、自動期待を使用して特定のタスクを実行することを観察し、結果のスクリプトを編集して不要なコンテンツを削除することです。

文字列の評価を避けるため、「...」を{...}に変更できます。

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect {Are you sure you want to continue connecting (yes/no/[fingerprint])? }
send "yes\r"

多くの場合、より多くのことを望み、プロンプトを選択的に許可します。

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect {
    {Are you sure you want to continue connecting (yes/no/[fingerprint])? } {
        exp_send "yes\r"
        exp_continue
    }
    {Password:} {send "secret\r"}
    {# }
}

おすすめ記事