如何通过 SSH 自动将命令远程并行运行到多个服务器? [英] How can I automate running commands remotely over SSH to multiple servers in parallel?

查看:23
本文介绍了如何通过 SSH 自动将命令远程并行运行到多个服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一些类似的问题,但除了运行一个命令或可能包含以下项目的几个命令:

I've searched around a bit for similar questions, but other than running one command or perhaps a few command with items such as:

ssh user@host -t sudo su -

但是,如果我基本上需要同时在(假设)15 个服务器上运行一个脚本怎么办.这在 bash 中可行吗?在一个完美的世界中,如果可能的话,我需要避免安装应用程序来实现这一点.为了论证起见,我们假设我需要在 10 台主机上执行以下操作:

However, what if I essentially need to run a script on (let's say) 15 servers at once. Is this doable in bash? In a perfect world I need to avoid installing applications if at all possible to pull this off. For argument's sake, let's just say that I need to do the following across 10 hosts:

  1. 部署一个新的 Tomcat 容器
  2. 在容器中部署应用程序,并对其进行配置
  3. 配置 Apache 虚拟主机
  4. 重新加载 Apache

我有一个脚本可以完成所有这些,但它依赖于我登录所有服务器,从存储库中提取脚本,然后运行它.如果这在 bash 中不可行,您有什么替代方案?我是否需要一个更大的锤子,比如 Perl(Python 可能是首选,因为我可以保证 Python 在 RHEL 环境中的所有机器上都使用 yum/up2date)?如果有人可以向我指出任何有用的信息,我将不胜感激,尤其是如果它在 bash 中可行的话.我会选择 Perl 或 Python,但我也不知道这些(正在研究).谢谢!

I have a script that does all of that, but it relies on me logging into all the servers, pulling a script down from a repo, and then running it. If this isn't doable in bash, what alternatives do you suggest? Do I need a bigger hammer, such as Perl (Python might be preferred since I can guarantee Python is on all boxes in a RHEL environment thanks to yum/up2date)? If anyone can point to me to any useful information it'd be greatly appreciated, especially if it's doable in bash. I'll settle for Perl or Python, but I just don't know those as well (working on that). Thanks!

推荐答案

通常,我只会使用 Expect 的原始 Tcl 版本.你只需要在本地机器上拥有它.如果我在使用 Perl 的程序中,我会使用 Net::SSH::期待.其他语言也有类似的期望"工具.

Often, I'll just use the original Tcl version of Expect. You only need to have that on the local machine. If I'm inside a program using Perl, I do this with Net::SSH::Expect. Other languages have similar "expect" tools.

这篇关于如何通过 SSH 自动将命令远程并行运行到多个服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆