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

查看:593
本文介绍了如何通过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(由于yum/up2date,我可以保证Python可以在RHEL环境中的所有盒子上使用,所以Python可能是首选)?如果有人可以向我指出任何有用的信息,将不胜感激,尤其是在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天全站免登陆