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
。

使用bash執行命令∞ (Execute commands using Bash ∞)
Run commands in serial order (one by one) using Bash over SSH
使用SSH上的Bash以串行順序(一對一)運行命令
-
for h
in lnode31 lnode6 cluster1-{1..8} ;
do
-
ssh root@
$h hostname
-
done
Run commands in parallel using Bash over SSH
使用SSH上的Bash並行運行命令
-
for h
in lnode31 lnode6 cluster1-{1..8} ;
do
-
ssh root@
$h hostname &
-
done
-
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執行命令