我想在 Centos7 上使用 shell 脚本自动生成一对 ssh 密钥,我已经尝试过
yes "y" | ssh-keygen -t rsa
echo "\n\n\n" | ssh-keygen...
echo | ssh-keygen..
所有这些命令都不起作用,只需输入一个 ‘enter’ 并且 shell 脚本在“Enter passphrase (empty for no passphrase)”处停止,我只想知道如何在 shell 中连续模拟多个 ‘enter’。
如果有人可以提供帮助,非常感谢!
原文由 Jeremy Wang 发布,翻译遵循 CC BY-SA 4.0 许可协议
我们需要自动完成 两个步骤:
-N
标志(此示例为空字符串):ssh-keygen -t rsa -N ''
使用
-f
输入路径(在本例中为 ---id_rsa
)加上 此处的字符串 来回答以下问题:或者,在
bash
类似 shell 下,如果您 确实想覆盖前一个,只需使用 here-string 来 为命令 提供所有需要的 _输入_:来自
ssh-keygen
手册 页:$ ssh-keygen -t rsa -f ~/.ssh/id_rsa Generating public/private rsa key pair. /home/klashxx/.ssh/id_rsa already exists. Overwrite (y/n)?
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa <<< y Generating public/private rsa key pair. /home/klashxx/.ssh/id_rsa already exists. Overwrite (y/n)? Enter passphrase (empty for no passphrase):
$ ssh-keygen -t rsa -N “ -f ~/.ssh/id_rsa <<< y Generating public/private rsa key pair. /home/klashxx/.ssh/id_rsa already exists. Overwrite (y/n)? Your identification has been saved in /home/klashxx/.ssh/id_rsa. Your public key has been saved in /home/klashxx/.ssh/id_rsa.pub. The key fingerprint is: SHA256:Xo0t6caMB/8TSsigxfY28JIfqYjyqxRZrFrPncx5yiU klashxx@server The key’s randomart image is: +—[RSA 2048]—-+ | | | . | | o . | | + * = | | +. + BSo= o | |…o.+o+XO… | |.. .o.E==+B. . | |o . …=.o… | |.+o. o .. | +—-[SHA256]—–+
\( ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa <<<y >/dev/null 2>&1 \) echo $? 0
cat - <<< $‘\ny’ | od -c 0000000 \n y \n
ssh-keygen -q -t rsa <<< $‘\ny’
\( ssh-keygen -q -t rsa -N '' <<< \)’\ny’ >/dev/null 2>&1 \( echo \)? 0
”`
荣誉
@lukasz-dynowski、@redochka、@mellow-yellow、@yeti 和该线程中的其他人。