如何使 ssh-add 从文件中读取密码? [英] How to make ssh-add read passphrase from a file?
问题描述
我正在尝试向 ssh-agent
添加一个密钥,并希望 ssh-add
从我正在使用的密钥文件中读取密码.这怎么可能?
I am trying to add a key to ssh-agent
and want ssh-add
to read the password from the key file I'm using. How is this possible?
如何从 shell 脚本自动执行此过程?
How do I automate this process from the shell script?
推荐答案
根据您的发行版和 ssh-add 的版本,您可以使用或不使用 ssh 的 -p
选项-添加以这种方式从标准输入读取密码:
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
如果这不起作用,您可以使用 Expect,一个 Unix 工具使交互式应用程序非交互式.你必须从你的包管理器安装它.
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.
我已经为您编写了一个工具.只需将内容复制到名为 ssh-add-pass 的文件中并为其设置可执行权限(chmod +x ssh-add-pass
).您也可以将其复制到/usr/bin 或/usr/local/bin 以便从 $PATH 搜索中访问.
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
"
expect eof
EOF
用法很简单:ssh-add-pass keyfile passfile
这篇关于如何使 ssh-add 从文件中读取密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!