0

From a bash script:

source ./expect.sh

I am including a expect code:

#!/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

And I am getting this:

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: 

Then I try to connect and I am prompted for a password...

Checking the server, I'm certain no key was uploaded because I would expect to list the "authorized_keys" file:

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

What am I doing wrong?

1

4 Answers 4

3

The problem is that the ssh client is reading directly from the terminal for the password, not from stdin.

The easiest way I know around this is to install 'sshpass', then use this (without Expect):

sshpass -p "thepassword" ssh-copy-id -i /home/user/.ssh/id_rsa.pub [email protected]
2
  • Fixed that to contain the actual username, see @seumasmac's answer.
    – Sven
    Oct 8, 2015 at 6:39
  • This is still missing to continue when getting the ECDSA key message..
    – derHugo
    Dec 5, 2017 at 16:43
2

The following script should do the trick too

#!/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"; }
}

You need to make it executable, and call it as follow:

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

In your case:

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

You are copying the key to /root/.ssh/authorized_keys rather than the user account. Note where it says: [email protected]'s password:

-1

!/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 }
                          }
                      }
                  }
              }
          }"
          }

fingerprints

1
  • 2
    Welcome to ServerFault. If you want your answer to be useful, please take time to format it correctly, and also explain it. Jun 1, 2018 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .