我讀過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
使用新產生的密碼。