I'm trying to send special characters (password) which contains #?!k;|C . these password is dynamic and cannot be hardcoded in script. so I'm passing through variable as below

$ expect ./passwordlessSSH.exp $hostname "$password"

the code of this script is as below

#!/usr/bin/expect -f
 set host [lindex $argv 0]
set pass [lindex $argv 1]

spawn ssh-copy-id -i /home/hdpsvcs/.ssh/id_rsa.pub hdpsvcs@$host
expect {
  "yes" {
    send "yes\r"
    "assword" {
    send "$pass \r"
exit 0

But still it is not running the script properly. I don't see any error but pass variable is not sent properly. PLease help me

Output is as below

$ expect ./passwordlessSSH.exp $hostname "$password"

 spawn ssh-copy-id -i /home/hdpsvcs/.ssh/id_rsa.pub test@otesthost
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
/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
Kernel \r on an \m
test@test's password: 
  • IN simple words I'm not able to run below command send ";c39#?!K|;p;abc\r" and I cannot use back slashes as the password is going to be a variable – user2925298 Jul 17 at 12:34
  • 1
    Make sure you write "$pass\r" without a space, not "$pass \r". Also, add a short sleep before the send to allow the remote script to switch off echo (and flush the tty). – meuh Jul 17 at 12:44
  • Hi meuh, I gave without space. Still the same issue – user2925298 Jul 17 at 13:04
  • 1
    I see no evidence that the password is sent incorrectly. However, you exit right after you log in. Perhaps you want to change exit 0 to interact so you can remain logged in. – glenn jackman Jul 17 at 13:30
  • @meuh, a sleep should not be necessary. – glenn jackman Jul 17 at 13:32

Your Answer

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

Browse other questions tagged or ask your own question.