プロンプトを表示せずに ssh-add にパスフレーズを渡すにはどうすればいいですか?

プロンプトを表示せずに ssh-add にパスフレーズを渡すにはどうすればいいですか?

読んでみましたが、あるようなman ssh-addパスフレーズを渡すためのフラグは見当たりません。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

Stephen の回答によると、それがどのように機能するかはわかりません。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.)

それで、これを使って少しごまかすことができるのです。

エージェントに ID がない状態から開始します。

$ 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新しく生成されたパスフレーズを使用します。

答え2

script(1)ミニとしても使えますexpect

Linuxの場合:

{ sleep .1; echo password; } | script -q /dev/null -c 'ssh-add /path/to/identity'

BSD の場合:

{ sleep .1; echo password; } | script -q /dev/null ssh-add /path/to/identity

パイプラインの右側の開始が遅い場合は、遅延(sleep .3または)を増やすとよいでしょう。より複雑な場合は、sleep 1expectsshpass実際には より優れているわけではなくscript、それ自体が の対象であるため、使用しないでください。レース

関連情報