如何通过SSH远程运行命令自动化? [英] How can I automate running commands remotely over 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:
- 部署新的Tomcat容器
- 在容器中部署应用程序并对其进行配置
- 配置Apache vhost
- 重新加载Apache
- Deploy a new Tomcat container
- Deploy an application in the container, and configure it
- Configure an Apache vhost
- Reload 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: :期望。其他语言也有类似的expect工具。
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屋!