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

查看:35
本文介绍了如何通过 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!

推荐答案

通常,我只会使用原始的 Tcl 版本的 Expect.您只需要在本地计算机上安装它.如果我在使用 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天全站免登陆