How to execute ssh-keygen uninvited

I want to automate the generation of an ssh key pair using a shell script on Centos7 and I have tried

yes "y" | ssh-keygen -t rsa
echo "\n\n\n" | ssh-keygen...
echo | ssh-keygen..

      

this whole command doesn't work, just type one 'enter' and the shell script is stopped at "Enter passphrase (empty without passphrase)", I just want to know how to simulate mutiple 'enter' in the shell continuously.

Thanks a lot if anyone can help!

+24


source to share


4 answers


Just introduce a void skip using a flag -N

:

ssh-keygen -t rsa -N ""

      

from the man page:



 -N new_passphrase
         Provides the new passphrase.

      

Overwrite id_rsa

 yes y |ssh-keygen -q -t rsa -N '' >/dev/null

      

+30


source


If you don't want to prompt the user for the file in which to store the key, you can add the file output flag -f

to the command.

ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa



Thus, the user will not be prompted for any input - no id_rsa files exist .

+27


source


For me, I had to use a combination of @Lukasz's answer and @Juan's one, when used in ssh command

ssh -p$SSH_PORT -q joker@$INSTANCE_IP 'yes y | ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa'

      

+1


source


None of the answers do exactly what is expected. Behavior required: run ssh-keygen with default settings (for example, if we just hit Enter) without prompting for input.

Command to run:

yes '' | ssh-keygen -N >/dev/null

      

skip> / dev / null if you want to print the output.

Expalaination:
yes y spam that ssh-keygen takes literally and creates keys in $ PWD / y and $ PWD / y.pub. yes '' spam blank lines (Enter), which is what we want. Specifying the file with -f ~ / .ssh / id_rsa will fail if the .ssh directory does not exist. The -t rsa option is not required if rsa is the default type (we inject spam anyway). The passphrase is not read from standard input (which we spam), but directly from the keyboard, so nothing can intercept it. For this reason, you need to specify -N '' for an empty passphrase.

0


source







All Articles