2

I'm trying to write a script that automatically unlocks all of my rsa keys with ssh-add on Arch Linux.

I use pass to store my passwords (encrypted with gpg keys) and I'm trying to pass its input to ssh-add as follows:

rsa_dir=~/.ssh
cd $rsa_dir
ls -1 id_rsa* |
grep -v '.pub' |
while read file; do
  # example: file == id_rsa_github -> keyname == rsa/github
  key_name=${file/id_rsa_/rsa/}
  # pass will prompt (only once) for the master password, then print out the request password to stdout
  pass $key_name | ssh-add $file
done

However, it gives an error:

ssh_askpass: exec(/usr/lib/ssh/ssh-askpass): No such file or directory

But if I just run ssh-add $file, it prompts for a password and works fine. I don't want to use the default ssh-askpass to get the password. I need to use my own script.

Can someone please help me?

3
  • 1
    This won't fix the issue you're having, but it is far safer and better to use for file in id_rsa* instead of ls | while.
    – terdon
    Commented Mar 24, 2021 at 17:37
  • @terdon, the drawback with the for approach is that it will take both id_rsa and id_rsa.pub files. This approach requires adding a if statement to skip the loop for the id_rsa.pub
    – user462354
    Commented Mar 25, 2021 at 1:40
  • find $rsa_dir ! -iregex '.*\.pub$'-print0 | xargs -0 ... or | while read file Commented Mar 26, 2021 at 21:45

1 Answer 1

1

I was in the same boat as you, so i found two method to do this:

Using xclip

pass yourpassword | xclip -selection clipboard ; ssh-add yourprivatekey

then you just use whatever key you would normally use to paste your password. Bear in mind this use the clipboard, so your password may be noticed by third party if you're not the only user/if you're on untrusted network...

Using Expect

PASS=`pass yourpassword`
/usr/bin/expect <<EOF
set timeout -1
spawn ssh-add privatekey
match_max 100000
expect -exact "Enter passphrase for ~/.ssh/privatekey: "
send -- "$PASS\r"
expect eof
EOF

Should work. Feel free to change the path to your privatekey above, or use autoexpect on a script which have ssh-add yourprivatekey to generate a somewhat working script...

Other idea(s)

The following is what i didn't test or managed to make it work:

  • You could use screen or tmux, which have some ways to send keys to a running session...

  • You could use some command that send your keys, which could be beneficial for the xclip method above. sendkey on github and xdotool come to mind.

4
  • The solution using expect sounds interesting (the program itself seems interesting). I will try that out
    – user462354
    Commented Mar 25, 2021 at 1:37
  • That did not work :( . It showed a prompt for every rsa key and kept waiting for me to insert the password. I'm looking for a way to avoid any manual operation (such as typing the password or pasting it from the clipboard manually)
    – user462354
    Commented Mar 29, 2021 at 13:45
  • 1
    Update: The reason why it wasn't working is because I was doing send -- "$password", but this sends the password without "pressing enter". I changed it to send -- "$password\r" it worked! Thanks!
    – user462354
    Commented Mar 29, 2021 at 14:03
  • Thanks for mentioning this; forgot to add a \r as i usually do when using expect ...Edited. @blackyellow Commented Mar 29, 2021 at 18:18

You must log in to answer this question.