linux ssh执行命令_在Linux上通过SSH在多个节点上并行执行命令的三种方法

linux ssh执行命令

It is common to execute commands on many nodes/hosts via SSH for managing a cluster of Linux servers. On Linux, there are many choices for this task. Generally, to run commands on many nodes, there are two modes: serial mode and parallel mode. In serial mode, the command is executed on the node one by one. In parallel mode, the command is executed on many nodes together. The serial mode is easy to reason about with and debug while the parallel mode is usually much faster.

通常会通过SSH在许多节点/主机上执行命令来管理Linux服务器集群。 在Linux上,此任务有很多选择。 通常,要在许多节点上运行命令,有两种模式:串行模式和并行模式。 在串行模式下,该命令在节点上一个接一个地执行。 在并行模式下,命令将在多个节点上一起执行。 串行模式易于使用和调试,而并行模式通常要快得多。

In this post, I will introduce 3 methods of executing commands on many nodes on Linux: using Bash, clustershell and pdsh. The methods are introduced by an example: execute the command hostname on nodes “lnode31 lnode6 cluster1-1 cluster1-2 … cluster1-8” as user “root”.

在本文中,我将介绍在Linux的许多节点上执行命令的3种方法:使用Bash,clustershell和pdsh。 通过示例介绍这些方法:以用户“ root”的身份在节点“ lnode31 lnode6 cluster1-1 cluster1-2…cluster1-8”上执行命令hostname

ssh-terminal.png

使用bash执行命令 (Execute commands using Bash )

Run commands in serial order (one by one) using Bash over SSH

使用SSH上的Bash以串行顺序(一对一)运行命令

  1. for h in lnode31 lnode6 cluster1-{1..8} ; do
  2. ssh root@$h hostname
  3. done

Run commands in parallel using Bash over SSH

使用SSH上的Bash并行运行命令

  1. for h in lnode31 lnode6 cluster1-{1..8} ; do
  2. ssh root@$h hostname &
  3. done
  4. wait

Pros: Bash is almost always available on Linux nodes. You can do certain checking logic after each ssh invoking.

优点:Bash在Linux节点上几乎总是可用。 您可以在每次ssh调用后执行某些检查逻辑。

Cons: The length of the command is a little bit long.

缺点:命令的长度有点长。

使用执行命令clustershell (Execute commands using clustershell )

clustershell/clush is a program for executing commands in parallel on a cluster. clush can also gather the commands’ results. If you haven’t installed it on the managing node, you can install the package clustershell (on Fedora Linux).

clustershell / clush是用于在集群上并行执行命令的程序。 clush还可以收集命令的结果。 如果尚未在管理节点上安装它,则可以安装软件包clustershell (在Fedora Linux上)。

Run commands in parallel using clustershell over SSH

通过SSH使用clustershell并行运行命令

$ clush -l root -w lnode31,lnode6,cluster1-[1-8] hostname

Pros: clush is designed for parallel execution. clush can also execute commands interactively.

优点: clush专为并行执行而设计。 clush还可以交互执行命令。

Cons: You will need to install the software on the managing node.

缺点:您将需要在管理节点上安装软件

使用执行命令pdsh (Execute commands using pdsh )

pdsh is a variant of the rsh command while pdsh can run multiple remote commands in parallel. pdsh can also run in interactive mode. If you haven’t installed it on the managing node, you need to install the package pdsh abd pdsh-rcmd-ssh (on Fedora Linux) first.

pdsh是rsh命令的变体,而pdsh可以并行运行多个远程命令。 pdsh也可以在交互模式下运行。 如果尚未在管理节点上安装它,则需要首先安装软件包pdsh abd pdsh-rcmd-ssh (在Fedora Linux上)。

Run commands in parallel using pdsh over SSH

通过SSH使用pdsh并行运行命令

$ pdsh -R ssh -l root -w lnode31,lnode6,cluster1-[1-8] hostname

For more usage of pdsh, check the pdsh manual page.

有关pdsh更多用法,请查看pdsh手册页

Pros and Cons: similar to those of clush. In addition, pdsh support other rcmd modules other than ssh such as rsh and exec.

优点和缺点:类似于clush 。 另外, pdsh支持除ssh之外的其他rcmd模块,例如rsh和exec。

These 3 methods should help managing a cluster of Linux nodes easier. Having other favorite tools to execute ssh commands? Share it with us by commenting.

这三种方法应有助于更轻松地管理Linux节点集群。 还有其他喜欢的工具来执行ssh命令吗? 通过评论与我们分享。

翻译自: https://www.systutorials.com/three-methods-execute-commands-many-nodes-parallel-ssh-linux/

linux ssh执行命令

原文地址:https://www.cnblogs.com/lidabo/p/15789152.html