From a bash script:

source ./expect.sh

I am including a expect code:

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

And I am getting this:

spawn ssh-copy-id -i /home/user/.ssh/id_rsa.pub
/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
root@'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/

What am I doing wrong?


In addition to the comment about "root":

  1. single quotes are just plain characters in expect. use double quotes
  2. don't forget to "hit enter" when you send the password
expect "*?assword*"
send "thepassword\r"

It's generally good practice to quote the heredoc end-word, unless you need to interpolate variables or command substitutions in the heredoc body:

some command <<'END'
# .............^...^
| improve this answer | |

You run spawn ssh-copy-id -i /home/user/.ssh/id_rsa.pub 111.111.111. The ip address seems to be wrong.

| improve this answer | |

You can see from the password prompt: root@ that you are setting up root's key on the remote, i.e. file /root/.ssh/authorized_keys. Either run the script as user, not root, or provide an explicit ssh-copy-id ... user@ This allows user to login without a password, not root.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.