How to make ssh-add read passphrase from a file?

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I am trying to add key in ssh-agent and want ssh-add read password from the file using. How its possible?

How do I automate this process from the shell script?

回答1:

Depending on your distribution and on the version of ssh-add you may be able or not to use the -p option of ssh-add that reads the passphrase from stdin in this way:

cat passfile | ssh-add -p keyfile

If this is not working you can use Expect, a Unix tool to make interactive applications non-interactive. You'll have to install it from your package manager.

I have written a tool for you in expect. Just copy the content in a file named ssh-add-pass and set executable permissions on it (chmod +x ssh-add-pass). You can also copy it to /usr/bin or /usr/local/bin to be accessible from the $PATH search.

#!/bin/bash  if [ $# -ne 2 ] ; then   echo "Usage: ssh-add-pass keyfile passfile"   exit 1 fi  eval $(ssh-agent) pass=$(cat $2)  expect << EOF   spawn ssh-add $1   expect "Enter passphrase"   send "$pass\r"   expect eof EOF

The usage is simply: ssh-add-pass keyfile passfile



回答2:

Here is some workaround for systems not supporting -p:

$ PASS="my_passphrase" $ install -vm700 <(echo "echo $PASS") "$PWD/ps.sh" $ cat id_rsa | SSH_ASKPASS="$PWD/ps.sh" ssh-add - && rm -v "$PWD/ps.sh"

where ps.sh is basically your script printing your passphrase. See: man ssh-add.

To make it more secure (to not keep it in the same file), use mktemp to generate a random private file, make it executable (chmod) and make sure it prints the passphrase to standard output once executed.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!