ssh-add without prompt
I am working on some shell and I encountered the following line:
cat /ssh/sshkey | tr -d 'r' | ssh-add -
This code get a key and add it to an agent. This work on its current environment, but when I try it on another VM, I get the following: Error loading key "(stdin)": invalid format
ssh-add expect a prompt with a passphrase, which I believe is what this error is about, but this is in a script and should work without one.
What am I missing ?
It’s quite simple; ssh-add uses /dev/tty and not stdin because it needs to disable the echo of the typed password so you can’t simply redirect to it.
In fact the manual page states:
If any file requires a passphrase, ssh-add asks for the passphrase
from the user. The passphrase is read from the user’s tty.
So you need to run ssh-add attached to a pty, or you can invent a clunky hack with SSH_ASKPASS (I don’t recommend it). Either expect(1) (the canonical behemoth) or something simpler like pdip; zsh has a module called zpty maybe there’s something similar in bash too.