bash 如何自动输入密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5514405/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:43:28  来源:igfitidea点击:

How to automate password entry?

bashshellsshautomation

提问by StackedCrooked

I want to install a software library (SWIG) on a list of computers (Jenkins nodes). I'm using the following script to automate this somewhat:

我想在计算机列表(Jenkins 节点)上安装软件库 (SWIG)。我正在使用以下脚本来自动执行此操作:

NODES="10.8.255.70 10.8.255.85 10.8.255.88 10.8.255.86 10.8.255.65 10.8.255.64 10.8.255.97 10.8.255.69"
for node in $NODES; do 
  scp InstallSWIG.sh root@$node:/root/InstallSWIG.sh
  ssh root@$node sh InstallSWIG.sh
done

This way it's automated, except for the password request that occur for both the scp and ssh commands.

这种方式是自动化的,除了 scp 和 ssh 命令的密码请求。

Is there a way to enter the passwords programmatically?

有没有办法以编程方式输入密码?

Security is not an issue. I'm looking for solutions that don't involve SSH keys.

安全不是问题。我正在寻找不涉及 SSH 密钥的解决方案。

采纳答案by Wes Hardaker

With SSH the right way to do it is to use keys instead.

使用 SSH 的正确方法是使用密钥。

# ssh-keygen

and then copy the *~/.ssh/id_rsa.pub* file to the remote machine (root@$node) into the remote user's .ssh/authorized_keys file.

然后将 *~/.ssh/id_rsa.pub* 文件复制到远程机器(root@$node)到远程用户的 .ssh/authorized_keys 文件中。

回答by Mathias Bynens

Here's an expectexample that sshs in to Stripe's Capture The Flag serverand enters the password automatically.

这是一个进入Stripe 的 Capture The Flag 服务器并自动输入密码的expect示例。ssh

expect <<< 'spawn ssh [email protected]; expect "password:"; send "e9gx26YEb2\r";'

回答by Micha? Kosmulski

You can perform the task using empty, a small utility from sourceforge. It's similar to expect but probably more convenient in this case. Once you have installed it, your first scpwill be accomplished by following two commands:

您可以使用来自 sourceforge 的小实用程序empty来执行任务。它类似于期望,但在这种情况下可能更方便。安装后,您的第一个scp将通过以下两个命令完成:

./empty -f scp InstallSWIG.sh root@$node:/root/InstallSWIG.sh
echo YOUR_SECRET_PASSWORD | ./empty -s -c

The first one starts your command in the background, tricking it into thinking it's running in interactive mode on a terminal. The other one sends it data from stdin. Of course, putting your password anywhere on command line is risky due to shell history being preserved, users being able to see it in psresults etc. Not secure either, but a bit better thing would be to store the password in a file and redirect the second command's input from that file instead of using echo and a pipe.

第一个在后台启动您的命令,诱使它认为它在终端上以交互模式运行。另一个从标准输入发送数据。当然,将你的密码放在命令行的任何地方是有风险的,因为 shell 历史被保留,用户能够在ps结果中看到它等等。也不安全,但更好的做法是将密码存储在文件中并重定向来自该文件的第二个命令的输入,而不是使用 echo 和管道。

After copying to the server, you can run the script in a similar manner:

复制到服务器后,您可以以类似的方式运行脚本:

./empty -f ssh root@$node sh InstallSWIG.sh
echo YOUR_SECRET_PASSWORD | ./empty -s -c

回答by Mat

You could look into setting up passwordless ssh keys for that. Establishing Batch Mode Connections between OpenSSH and SSH2is a starting point, you'll find lots of information on this topic on the web.

您可以考虑为此设置无密码 ssh 密钥。在 OpenSSH 和 SSH2 之间建立批处理模式连接是一个起点,您可以在网络上找到有关此主题的大量信息。

回答by Noufal Ibrahim

Wes' answer is the correct one but if you're keen on something dirty and slow, you can use expectto automate this.

Wes 的答案是正确的,但是如果您热衷于一些肮脏和缓慢的事情,您可以使用expect来自动化这一点。