如何使用並行 ssh (PSSH) 在多個 Linux/Unix/BSD 服務器上並行運行命令


反抗最近,我遇到了一個名為 Parallel ssh (PSSH) 的漂亮小工具,它允許您在多個 Linux/UNIX/BSD 服務器上運行單個命令。使用此 SSH IT 自動化工具,您可以輕鬆提高工作效率。

更多關於 pssh

pssh 是一個命令行工具,用於在某些主機上並行運行 ssh。它的特長是:

  1. 向所有進程發送輸入
  2. 輸入 ssh 密碼
  3. 將輸出保存到文件
  4. 自動化 IT/系統管理員任務,例如修補服務器和搜索錯誤日誌
  5. 超時等

讓我們看看如何在 Linux 和類 Unix 系統上安裝和使用 pssh。

安裝並行 ssh (PSSH)

根據您的 Linux 和 Unix 變體,您可以安裝 pssh。您可以通過安裝軟件包獲得併排版的 openssh 工具。安裝包括:

  1. pssh 命令 – 並行 ssh
  2. pscp 命令 – 並行 scp
  3. prsync 命令 – 並行 rsync
  4. pnuke 命令 – 平行核
  5. pslurp 命令 – 並行 Slurp

在 Debian/Ubuntu Linux 上安裝 pssh

通過輸入以下 apt-get 命令/apt 命令來安裝 pssh:
$ sudo apt install pssh


$ sudo apt-get install pssh

示例輸出:

在 Apple MacOS X 上安裝 pssh

首先,在 macOS 上安裝 Homebrew 並輸入以下 brew 命令:
$ brew install pssh

示例輸出:

安裝 pssh 以在多個遠程 Linux 和 Unix 服務器上運行命令

在 FreeBSD unix 上安裝 pssh

輸入以下命令之一:
# cd /usr/ports/security/pssh/ && make install clean


# pkg install pssh

示例輸出:

圖 03:在 FreeBSD 上安裝 pssh

在 RHEL/CentOS/ 上安裝 psshFedora Linux。

首先打開EPEL倉庫,輸入以下yum命令:
$ sudo yum install pssh

示例輸出:

圖 04:在 RHEL/CentOS/Red Hat Enterprise Linux 上安裝 pssh

安裝 pssh Fedora Linux。

輸入以下 dnf 命令:
$ sudo dnf install pssh

示例輸出:

圖 05:在 Fedora 上安裝 pssh

在 Arch Linux 上安裝 pssh

輸入以下命令:
$ sudo pacman -S python-pip
$ pip install pssh

使用公鑰/私鑰對設置無密碼 SSH

我們建議設置 ssh 密鑰以實現自動化。要生成密鑰對,請運行 ssh-keygen 命令。
ssh-keygen

然後使用 ssh-copy-id 命令將公鑰傳輸到遠程主機。
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]

使用 ssh 命令進行測試。
ssh [email protected]
ssh [email protected]

有關無密碼 SSH 主題的更多信息,請參閱我們之前的教程在 Linux/Unix 服務器上基於 SSH 公鑰的身份驗證和在 Linux/Unix 系統上設置 SSH 密鑰。

如何使用 pssh 命令

首先,我們需要創建一個名為 hosts 文件的文本文件,pssh 將從該文件中讀取主機名。語法非常簡單。 hosts 文件中的每一行都採用以下形式: [[email protected]]主持人[:port] 它還可以包含以“#”開頭的空白行和註釋行。 這是一個名為 ~/.pssh_hosts_files 的示例文件。
$ cat ~/.pssh_hosts_files
[email protected]
[email protected]
[email protected]
[email protected]

在所有主機上運行 date 命令。
$ pssh -i -h ~/.pssh_hosts_files date

示例輸出:

[1] 18:10:10 [SUCCESS] [email protected]
Sun Feb 26 18:10:10 IST 2017
[2] 18:10:10 [SUCCESS] [email protected]
Sun Feb 26 18:10:10 IST 2017
[3] 18:10:10 [SUCCESS] [email protected]
Sun Feb 26 18:10:10 IST 2017
[4] 18:10:10 [SUCCESS] [email protected]
Sun Feb 26 18:10:10 IST 2017

在每台主機上運行 uptime 命令。
$ pssh -i -h ~/.pssh_hosts_files uptime

示例輸出:

[1] 18:11:15 [SUCCESS] [email protected]
 18:11:15 up  2:29,  0 users,  load average: 0.00, 0.00, 0.00
[2] 18:11:15 [SUCCESS] [email protected]
 18:11:15 up 19:06,  0 users,  load average: 0.13, 0.25, 0.27
[3] 18:11:15 [SUCCESS] [email protected]
 18:11:15 up  1:55,  0 users,  load average: 0.00, 0.00, 0.00
[4] 18:11:15 [SUCCESS] [email protected]
 6:11PM  up 1 day, 21:38, 0 users, load averages: 0.12, 0.14, 0.09

您現在可以自動執行常見的系統管理任務,例如修補所有服務器。
$ pssh -h ~/.pssh_hosts_files -- sudo yum -y update


$ pssh -h ~/.pssh_hosts_files -- sudo apt-get -y update
$ pssh -h ~/.pssh_hosts_files -- sudo apt-get -y upgrade

如何使用 pssh 將文件複製到所有服務器?

語法是:
pscp -h ~/.pssh_hosts_files src dest

要將 $HOME/demo.txt 複製到所有服務器上的 /tmp/,請輸入:
$ pscp -h ~/.pssh_hosts_files $HOME/demo.txt /tmp/

示例輸出:

[1] 18:17:35 [SUCCESS] [email protected]
[2] 18:17:35 [SUCCESS] [email protected]
[3] 18:17:35 [SUCCESS] [email protected]
[4] 18:17:35 [SUCCESS] [email protected]

或者,使用 prsync 命令有效地複製文件。
$ prsync -h ~/.pssh_hosts_files /etc/passwd /tmp/
$ prsync -h ~/.pssh_hosts_files *.html /var/www/html/

如何在多個主機上並行殺死進程?

使用 pnuke 命令並行殺死多個主機上的進程。語法是:
$ pnuke -h .pssh_hosts_files process_name

退出主機上的 nginx 和 firefox。
$ pnuke -h ~/.pssh_hosts_files firefox
$ pnuke -h ~/.pssh_hosts_files nginx

有關更多信息,請參見 pssh/pscp 命令的手冊頁。

結論是

pssh 是一個非常好的工具,可以在許多服務器上並行運行 SSH 命令。如果您有 5 或 7 個服務器或虛擬機,這很有用。不過,如果你需要做一些複雜的事情,你應該考慮像 Ansible 這樣的東西。看 官方網站在這裡 了解更多信息。