在 Bash 中自动按 Enter 继续
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11190004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically press Enter to continue in Bash
提问by Simons0n
I want to change RSA Keys to open ssh connections without any problems. It works fine, if I use this tutorialbut I want to have it done in a bash script. Unfortunately I am asked to enter a passphrase (I want to enter nothing). How can I achieve to automatically press Enterthree times in a row, when the script reaches this point?
我想更改 RSA 密钥以毫无问题地打开 ssh 连接。它工作正常,如果我使用本教程但我想在 bash 脚本中完成它。不幸的是,我被要求输入密码(我什么都不输入)。Enter脚本到了这个点,如何实现自动连续按三下?
This threaddid not help, because I am not allowed to install new programs on my work PC and the echo | commandtrick seems only to work for one Enter. Also I need to enter "n" and Enter, if the procedure was already made, to not overwrite any files. How do i achieve that?
这个线程没有帮助,因为我不允许在我的工作 PC 上安装新程序,而且这个echo | command技巧似乎只对一个Enter. 此外,我需要输入“n” Enter,如果程序已经完成,则不要覆盖任何文件。我如何做到这一点?
回答by smocking
If you just need to press Entera bunch of times this will do it:
如果您只需要按Enter几次就可以了:
yes "" | command
yes "" | command
For anything more complicated than that you might want to use expectas suggested in the other thread, which you can install in your homedir without root priviliges.
对于比这更复杂的任何事情,您可能希望expect按照另一个线程中的建议使用,您可以在没有 root 权限的情况下将其安装在您的 homedir 中。
PS: Please avoid re-posting questions in the future. If you don't like an answer for some reason, just comment on it.
PS:请避免以后重新发布问题。如果您出于某种原因不喜欢某个答案,请对其发表评论。
回答by mrb
If you want to just create ssh keys in a bash script without requiring any user input, you can specify arguments to ssh-keygen:
如果您只想在 bash 脚本中创建 ssh 密钥而不需要任何用户输入,您可以指定参数ssh-keygen:
# rsa type (default), no passphrase, write to file id_rsa and id_rsa.pub
captain:~> ssh-keygen -t rsa -N "" -f id_rsa
Generating public/private rsa key pair.
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
fe:4a:82:08:0e:ab:b7:02:62:11:4d:3e:79:a4:d3:98 [email protected]

