我读过man ssh-add
,但没有看到像 is用于传递密码的标志。ssh
那样-p
我试过
# the -K is for macOS's ssh-add but it's not relevant
ssh-add -q -K ~/.ssh/id_rsa <<< $passphrase
但我得到以下输出:
ssh_askpass: exec(/usr/X11R6/bin/ssh-askpass): No such file or directory
如何传递密码而不触发提示
更新
为我正在做的事情添加更多上下文,我正在创建一个脚本来为我创建 SSH 密钥。它将生成密码、使用该密码的 SSH 密钥,并将其添加到代理中。
# ...
passphrase=$(generate_password)
ssh-keygen -t rsa -b 4096 -C $email -f $filename -N $passphrase -q
ssh-add -q -K $filename
根据斯蒂芬的回答,我不确定这会如何运作。似乎我必须创建一个临时脚本并将其保存到磁盘,以便 SSH_ASKPASS 工作。有任何想法吗?
答案1
从ssh-add
联机帮助页:
DISPLAY and SSH_ASKPASS
If ssh-add needs a passphrase, it will read the passphrase from
the current terminal if it was run from a terminal. If ssh-add
does not have a terminal associated with it but DISPLAY and
SSH_ASKPASS are set, it will execute the program specified by
SSH_ASKPASS (by default ``ssh-askpass'') and open an X11 window
to read the passphrase. This is particularly useful when calling
ssh-add from a .xsession or related script. (Note that on some
machines it may be necessary to redirect the input from /dev/null
to make this work.)
所以我们可以用它来作一点小作弊。
我们从代理中没有身份开始:
$ ssh-add -l
The agent has no identities.
所以现在我们需要一个提供密码的程序:
$ cat x
#!/bin/sh
echo test123
然后说服 ssh-add 使用该脚本:
$ DISPLAY=1 SSH_ASKPASS="./x" ssh-add test < /dev/null
Identity added: test (sweh@godzilla)
就是这样:
$ ssh-add -l
2048 SHA256:07qZby7TafI10LWAMSvGFreY75L/js94pFuNcbhfSC0 sweh@godzilla (RSA)
根据修改后的问题编辑添加:
密码可以作为变量传递,askpass 脚本使用该变量。
例如:
$ cat /usr/local/sbin/auto-add-key
#!/bin/sh
echo $SSH_PASS
$ SSH_PASS=test123 DISPLAY=1 SSH_ASKPASS=/usr/local/sbin/auto-add-key ssh-add test < /dev/null
Identity added: test (sweh@godzilla)
在所呈现的工作流程中,您将SSH_PASS=$passphrase
使用新生成的密码。