反抗最近,我遇到了一個不錯的小工具,叫做 Parallelssh (PSSH),它可以在多個 Linux / UNIX / BSD 服務器上執行單個命令。使用此 SSHIT 自動化工具,您可以輕鬆提高工作效率。
pssh的詳細信息
pssh 是一個命令行工具,用於在某些主機上並行運行 ssh。其特長如下。
- 向所有進程發送輸入
- 輸入 ssh 密碼
- 將輸出保存到文件
- 自動化 IT / 系統管理員任務,例如修補服務器和搜索錯誤日誌
- 超時等。
讓我們看看如何在 Linux 和類 Unix 系統上安裝和使用 pssh。
並行 ssh (PSSH) 安裝
您可以根據您的 Linux 和 Unix 變體安裝 pssh。您可以通過安裝軟件包獲得併排版的 openssh 工具。安裝中包含的內容:
- pssh 命令並行 ssh
- pscp 命令-並行 scp
- prsync 命令-parallelrsync
- pnuke 命令 – 並行核
- pslurp 命令 – 並行 slurp
在 Debian / Ubuntu Linux 上安裝 pssh
通過輸入以下 apt-get 命令/apt 命令來安裝 pssh。$ sudo apt install pssh
還$ sudo apt-get install pssh
樣本輸出:
在 Apple Mac OS X 上安裝 pssh
首先,在 macOS 上安裝 Homebrew 並輸入以下 brew 命令。$ brew install pssh
樣本輸出:
在 FreeBSD UNIX 上安裝 pssh
輸入以下命令之一:# cd /usr/ports/security/pssh/ && make install clean
還# pkg install pssh
樣本輸出:
在 RHEL / CentOS / 上安裝 psshFedora Linux
首先打開EPEL倉庫,輸入以下yum命令。$ sudo yum install pssh
樣本輸出:
安裝 psshFedora Linux
輸入以下 dnf 命令。$ sudo dnf install 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 andco。看 官方網站在這裡 了解更多信息。