Automating "enter" keypresses for bash script generating ssh keys

Bash

Bash Problem Overview


I would like to create script, which simply runs ssh-keygen -t rsa. But how to pass to it 3 times enter?

Bash Solutions


Solution 1 - Bash

Try:

ssh-keygen -t rsa -N "" -f my.key

-N "" tells it to use an empty passphrase (the same as two of the enters in an interactive script)

-f my.key tells it to store the key into my.key (change as you see fit).

The whole thing runs without you needing to supply any enter keys :)

To send enters to an interactive script:

echo -e "\n\n\n" | ssh-keygen -t rsa

Solution 2 - Bash

a version with passphrase is:

$ ssh-keygen -t rsa -b 4096 -C "comment" -P "examplePassphrase" -f "desired pathAndName" -q 
  • the -q is for silent

Source is http://linux.die.net/man/1/ssh-keygen

Solution 3 - Bash

Agree with Michel Marro except that it needs some more: If the file already exists, it will still be interactive asking if it has to overwrite it.

Use the answer of this question.

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

The redirection to null is necessary to silence the overwrite message.

Solution 4 - Bash

echo -e "\n"|ssh-keygen -t rsa -N ""

Solution 5 - Bash

It is recommended to use ed25519 for security and performance.

yes "y" | ssh-keygen -o -a 100 -t ed25519 -C "Bla Bla" -f /mypath/bla -N ""

here

-o OpenSSH key format instead of older PEM (needs OpenSSH 6.5+)

-a Number of primality test while screening DH-GEX candidates

-t Type of key (ed25519, RSA, DSA etc.)

-f /mypath/bla The output file path and name

-N "" Use empty passphase

and yes "y" for no interaction.

It will generate two files

/mypath/bla
/mypath/bla.pub

where the bla file is private and bla.pub is public.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSławoszView Question on Stackoverflow
Solution 1 - BashRuduView Answer on Stackoverflow
Solution 2 - BashMichel MarroView Answer on Stackoverflow
Solution 3 - BashshamoxView Answer on Stackoverflow
Solution 4 - BashMannixView Answer on Stackoverflow
Solution 5 - BashMadan SapkotaView Answer on Stackoverflow