2

First, I do know this is a security risk. I do know about ssh keys and sshpass. This belongs to a fun project.

I wondered if a bash Here-Document could be used as a password input for ssh.

I unseccessfully tried something like

ssh localhost <<!
mytestpasswd
/bin/bash
!

Sadly the bash terminates. I belive it's because the Here-Document closes stdin.

Did someone figure out how to achieve this?

1 Answer 1

3

You're right that a simple here-document won't work for SSH password input because SSH reads passwords directly from the terminal /dev/tty, not from stdin.

You can use expect, a tool designed for automation to achieve this in a fun project.

If you don't want to write an expect script, you can use sshpass in a one-liner.

sshpass -ffilename ssh user@host                                # prefer this
SSHPASS=$(secret-tool lookup mypasswd) sshpass -e ssh user@host # or this
sshpass -pPa5sw0rd ssh user@host                                # avoid this

Both sshpass and expect are external tools and not part of Bash itself. The original point was more about syntax. But as you mentioned, you're aware of the security risks.

The best method is always to work with SSH keys, whether for fun or production.

2
  • 1
    this answer really should contain some content on how to use expect or sshpass, and not just links.
    – ilkkachu
    Commented Apr 22 at 11:05
  • 1
    This answer is more or less a "link-only answer". The links are likely correct (at the moment), but it would be better if the actual solution were part of the text on this page. For example, the use of sshpass with a here-document (the way to provide the password mentioned in the question) could be shown.
    – Kusalananda
    Commented Apr 22 at 11:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.