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
[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/

What am I doing wrong?


3 Answers 3


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'
# .............^...^

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


You can see from the password prompt: [email protected] 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 ... [email protected]. This allows user to login without a password, not root.

You must log in to answer this question.

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