!/usr/bin/env bash

!/usr/bin/env bash

來自 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

問題在於 ssh 用戶端直接從終端機讀取密碼,而不是從標準輸入讀取。

我知道的最簡單的方法是安裝“sshpass”,然後使用它(不帶 Expect):

sshpass -p "thepassword" ssh-copy-id -i /home/user/.ssh/id_rsa.pub [email protected]

答案2

下面的腳本也應該可以解決這個問題

#!/usr/bin/expect -f
#
# Install RSA SSH KEY with no passphrase
#

set user [lindex $argv 0]
set host [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh-copy-id -i /path/to/your/.ssh/id_rsa.pub $user@$host

expect {
    "continue" { send "yes\n"; exp_continue }
    "assword:" { send "$password\n"; }
}

您需要使其可執行,並按如下方式調用它:

./ssh-copy-id.exp <user> <host> <password>

在你的情況下:

./ssh-copy-id.exp root 111.111.111.111 thepassword

答案3

您正在將密鑰複製到/root/.ssh/authorized_keys而不是使用者帳戶。注意它所說的地方:[email protected]'s password:

答案4

!/usr/bin/env bash

          fingerprints(){
          /usr/bin/expect -c "set timeout 50; spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -p\ <port num>\ root@$<your server>;
          
          expect {
                  \"assword: \" {
                  send \<your pwd>\n\"
                  expect {
                      \"again.\"     { exit 1 }
                      \"expecting.\" { }
                      timeout      { exit 1 }
                  }
              }
              \"(yes/no)? \" {
                  send \"yes\n\"
                  expect {
                      \"assword: \" {
                          send \"<your pwd>\n\"
                          expect {
                              \"again.\"     { exit 1 }
                              \"expecting.\" { }
                              timeout      { exit 1 }
                          }
                      }
                  }
              }
          }"
          }

指紋

相關內容