프롬프트를 표시하지 않고 ssh-add에 암호를 전달하는 방법은 무엇입니까?

프롬프트를 표시하지 않고 ssh-add에 암호를 전달하는 방법은 무엇입니까?

읽어보니 is 에 있는 것과 같은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.

리눅스의 경우:

{ 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 1더 복잡한 경우에는 다음을 사용하세요.expect. sshpass실제로는 그 script자체 보다 나을 것이 없으므로 사용하지 마십시오 .경마 대회.

관련 정보