ssh
prompts for and reads password (or passphrase) using the terminal (/dev/tty
), not its stdin. This way you can pipe/redirect data to/from ssh
and still be able to provide a password when asked. But to provide a password not via the terminal, one needs to present a "fake" terminal to ssh
. This is what sshpass
does.
When you sshpass … ssh …
, sshpass
runs ssh
in a dedicated emulated terminal. This means ssh
does not read directly from your terminal, sshpass
does. And ssh
does not print directly to your terminal, sshpass
does. Eventually sshpass
will act as a relay, so it will be as if ssh
used your terminal. But before this happens, sshpass
intercepts what ssh
prints; it also injects the string you specify after -p
, then ssh
"sees" the string as coming from the terminal ssh
is using (which is not your terminal). This way ssh
can be fooled you typed the password, when it's sshpass
who "typed".
By default sshpass
waits for assword:
(or assword
1?) to appear as a part of the prompt for password. E.g. if you didn't use a key and you didn't use sshpass
, ssh
would print:
[email protected]'s password:
and it would wait for you to type your password. If you used sshpass
to provide your password, then sshpass
would intercept this message and "type" the password for you. By waiting for the right prompt sshpass
knows when ssh
expects a password, only then it passes your password.
In your case the prompt was different. ssh
did not ask for the password, it asked for the passphrase using a different prompt. The prompt from ssh
was exactly Enter passphrase for key '/home/user1/.ssh/id_rsa':
, there was nothing matching assword:
, so sshpass
kept waiting for the default prompt that never came.
Use -P
to override the default.
-P
Set the password prompt. Sshpass searched for this prompt in the program's output to the TTY as an indication when to send the password. By default sshpass looks for the string assword:
(which matches both Password:
and password:
). If your client's prompt does not fall under either of these, you can override the default with this option.
(source: man 1 sshpass
)
In your case it may be:
sshpass -P assphrase -p "pass" ssh [email protected]
Now if sshpass
intercepts Enter passphrase …
coming from the ssh
, it will respond with whatever you specified after -p
. Next it will sit as a relay between your terminal and the one ssh
is using; it will become transparent.
In general sshpass
can be used to provide a password (a string in general) to any tool that normally uses the terminal (as opposed to stdin+stdout+stderr) to prompt for and read the password. -P
allows you to adjust the command to the prompt the tool uses.
1 The manual says assword:
, but the output from your sshpass -v
says using match "assword"
. One way or another you need -P
to properly pass a passphrase.
ssh-agent
and loading your key into memory if this is supposed to work interactivelypassword
or to something else? I don't know what they are reffering to? So it was reading my passphrase (same as user password) in thatread
segment. So how it didn't pass through?sshpass
sticks itself between thessh
client program, and the terminal where the user is connected. It reads everything the SSH client prints, and when it sees the string it's expecting, it inserts the password. And the string it's expecting isassword
, generally enough to work with slight variation in the prompt (and without the P so it doesn't need to care if it's in uppercase or lowercase). But with key-based authentication, the prompt is different, and by default it doesn't recognize that prompt.