I already know that there are other questions like this, but their answers don't help me. The suggestion in other forums was to use sshpass or pre install the ssh keys, but I can't use sshpass and I can't pre install ssh keys in the system.

I want to copy my ssh key to a remote server using stdin in ssh-copy-id or another method where I can pass the password by stdin using echo.

Something like this:

echo -e "1234" | ssh developer@ "echo "my_key" >> ~/.ssh/authorized_keys"

This is on a CentOS system.

  • What's wrong with the command you show? Doesn't that work? – terdon Oct 20 '20 at 12:19
  • When I run this comand it ask by the password. – Ziliom Brom Oct 20 '20 at 12:22
  • Well yes, of course. You're setting up passwordless ssh, right? So the first time you do this, it will ask for a password. – terdon Oct 20 '20 at 12:28
  • Hang on. What are you trying to do here exactly? Is the 1234 supposed to be the ssh password? – terdon Oct 20 '20 at 12:28
  • Yes, when I try to connect to a remote server in the first time it will ask the password, but the problem is that it is not accepting the password typed in the echo command. – Ziliom Brom Oct 20 '20 at 12:30

You can use the variable SSH_ASKPASS for that.




if [ ! -t 1 ]; then
   # The output is not going to stdout, assume the invoke is from SSH_ASKPASS
   printf "%s\n" "$pwd"
   exit 0

# SSH_ASKPASS will be used only if DISPLAY is defined
export DISPLAY=:0

# Set the SSH_ASKPASS program to THIS script+
export SSH_ASKPASS="$0"

# setsid is required (Run a program in a new session)
setsid ssh-copy-id user@localhost
  • What is the -t flag in if? – Ziliom Brom Oct 21 '20 at 19:16

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.