I'm using this command on Server1

~# ssh root@Server2 /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

to append the contents of Server2's id_rsa.pub to the authorized_keys of Server1.

It works if I do it manually but when I do it in an expect script:


set timeout 60

spawn ssh root@[lindex $argv 0] cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

expect "yes/no" { send "yes\r" 
expect "*?assword" { send "[lindex $argv 1]\r" }
    } "*?assword" { send "[lindex $argv 1]\r" }

What happens when I use the script is that the id_rsa.pub of Server2 is appended over the authorized_keys of Server2.

What would be the correct syntax?

  • you forgot the word cat in first example. Please paste working code (or broken where appropriate, but paste the code you tested). Commented Jun 16, 2014 at 21:36

1 Answer 1


It looks like expect is not a shell, so it passes >> as an argument to ssh instead of interpreting it.

Try spawn bash -c "ssh root@[lindex $argv 0] cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"


You must log in to answer this question.

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