Bashスクリプトとssh-copy-idでExpectを使用する方法

Bashスクリプトとssh-copy-idでExpectを使用する方法

Bashスクリプトでは:

source ./expect.sh

期待されるコードが含まれています。

#!/bin/bash
/usr/bin/expect <<EOL
spawn ssh-copy-id -i /home/user/.ssh/id_rsa.pub 111.111.111
expect '*?assword*'
send 'thepassword'
interact
EOL

私はこれを得ます:

spawn ssh-copy-id -i /home/user/.ssh/id_rsa.pub 111.111.111.111
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

その後、接続しようとすると、パスワードの入力を求められます。

サーバーを確認した結果、「authorized_keys」ファイルがリストされると予想したため、キーがアップロードされていないことを確認します。

root@server: ls /home/user/.ssh/
known_hosts

私は何が間違っていましたか?

ベストアンサー1

「root」の説明に加えて、次の内容があります。

  1. 一重引用符は予想される一般的な文字です。二重引用符を使用してください
  2. パスワードを送信するときに「Enter」を押すことを忘れないでください。
expect "*?assword*"
send "thepassword\r"

heredocの本文に変数やコマンドの置換を挿入する必要がない限り、通常はheredocの末尾の単語を引用することをお勧めします。

some command <<'END'
# .............^...^
...
END

おすすめ記事