!/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 客户端直接从终端读取密码,而不是从 stdin 读取。

我知道的解决这个问题的最简单的方法是安装“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 }
                          }
                      }
                  }
              }
          }"
          }

指纹

相关内容