一、簡介
pssh全稱是parallel-ssh,是一個Python編寫的可以併發在多臺服務器上批量執行命令的工具,
它支持文件並行複製、遠程並行執行命令、殺掉遠程主機上的進程等。
前提:使用pssh工具包,必須保證本地主機和管理的遠程主機之間的單向信任,
也就是要在本地主機和所有遠程服務器上配置密鑰認證訪問。
項目地址:https://code.google.com/p/parallel-ssh/
pssh 包安裝5個實用程序:parallel-ssh、parallel-scp、parallel-rsync、parallel-slurp、parallel-nuke 每個實用程序都並行地操作多個主機。
-
parallel-ssh 在多個主機上並行地運行命令。
-
parallel-scp 把文件並行地複製到多個主機上。(全量拷貝)
-
parallel-rsync 通過 rsync 協議把文件高效地並行複製到多個主機上。(增量拷貝)
-
parallel-slurp 把文件並行地從多個遠程主機複製到中心主機上。
-
parallel-nuke 並行地在多個遠程主機上殺死進程。
二、安裝
yum -y install pssh
設置別名:
alias pscp=pscp.pssh
三、pssh
1、參數
pssh --help
可以查看命令參數選項:
[cuadmin@cdh68 ~]$ pssh --help
Usage: pssh [OPTIONS] command [...]
Options:
--version show program's version number and exit
--help show this help message and exit
-h HOST_FILE, --hosts=HOST_FILE
hosts file (each line "[user@]host[:port]")
-H HOST_STRING, --host=HOST_STRING
additional host entries ("[user@]host[:port]")
-l USER, --user=USER username (OPTIONAL)
-p PAR, --par=PAR max number of parallel threads (OPTIONAL)
-o OUTDIR, --outdir=OUTDIR
output directory for stdout files (OPTIONAL)
-e ERRDIR, --errdir=ERRDIR
output directory for stderr files (OPTIONAL)
-t TIMEOUT, --timeout=TIMEOUT
timeout (secs) (0 = no timeout) per host (OPTIONAL)
-O OPTION, --option=OPTION
SSH option (OPTIONAL)
-v, --verbose turn on warning and diagnostic messages (OPTIONAL)
-A, --askpass Ask for a password (OPTIONAL)
-x ARGS, --extra-args=ARGS
Extra command-line arguments, with processing for
spaces, quotes, and backslashes
-X ARG, --extra-arg=ARG
Extra command-line argument
-i, --inline inline aggregated output and error for each server
--inline-stdout inline standard output for each server
-I, --send-input read from standard input and send as input to ssh
-P, --print print output as we get it
Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime
中文翻譯:
-l 遠程機器的用戶名
-p 一次最大允許多少連接
-o 輸出內容重定向到一個文件
-e 執行錯誤重定向到一個文件
-t 設置命令執行的超時時間
-A 提示輸入密碼並且把密碼傳遞給ssh(注意這個參數添加後只是提示作用,隨便輸入或者不輸入直接回車都可以,可以結合sshpass -p password使用)
-O 設置ssh參數的具體配置,參照ssh_config配置文件
-x 傳遞多個SSH 命令,多個命令用空格分開,用引號括起來
-X 同-x 但是一次只能傳遞一個命令
-i 顯示標準輸出和標準錯誤在每臺host執行完畢後
-I 讀取每個輸入命令,並傳遞給ssh進程 允許命令腳本傳送到標準輸入
2、使用
在使用工具前,確保主機間做了密鑰認證,否則無法實現自動化,當然我們可以使用sshpass(yum install sshpass)配合pssh -A參數實現自動輸入密碼,但這要保證多臺主機的密碼相同,同時還要注意如果known_hosts沒有信任遠程主機,那麼命令執行會失敗,可以加上-O StrictHostKeyChecking=no參數解決,ssh能用的選項pssh也能用
# 集羣剛裝好系統處於原始狀態,可以使用下面命令來生成其他機器的ssh祕鑰並將各機器的rsa公鑰添加到本機
sshpass -p password pssh -I -A -O StrictHostKeyChecking=no -h ip.txt -l cuadmin -i "ssh-ketgen" # 然後本機回車n次幫助各機器生成ssh祕鑰,password爲其它機器的統一密碼
sshpass -p password pssh -A -O StrictHostKeyChecking=no -h ip.txt -l cuadmin -i "ssh-copy-id localhost-ip" # localhost-ip改成你本機的ip
ip.txt
10.172.54.68
10.172.54.69
10.172.54.70
批量執行命令:
[cuadmin@cdh68 ~]$ pssh -h ip.txt uptime
[1] 17:09:09 [SUCCESS] 10.172.54.68
[2] 17:09:09 [SUCCESS] 10.172.54.69
[3] 17:09:09 [SUCCESS] 10.172.54.70
[cuadmin@cdh68 ~]$ pssh -h ip.txt -P uptime
10.172.54.68: 17:10:02 up 549 days, 23:50, 4 users, load average: 0.17, 0.12, 0.19
[1] 17:10:02 [SUCCESS] 10.172.54.68
10.172.54.70: 17:10:02 up 549 days, 23:43, 2 users, load average: 0.11, 0.17, 0.22
[2] 17:10:02 [SUCCESS] 10.172.54.70
10.172.54.69: 17:10:02 up 549 days, 23:47, 2 users, load average: 0.00, 0.04, 0.05
[3] 17:10:02 [SUCCESS] 10.172.54.69
保存每臺服務器運行的結果:
20201013 ambariashell cling-master CTR_Mobile dfs download hosts.txt ip.txt password.sh password.txt software tencent2019Rank1st test.sh yarn
[cuadmin@cdh68 ~]$ mkdir /tmp/pssh
[cuadmin@cdh68 ~]$ pssh -h ip.txt -P -o /tmp/pssh/ uptime
10.172.54.68: 17:14:41 up 549 days, 23:54, 4 users, load average: 0.07, 0.09, 0.16
[1] 17:14:41 [SUCCESS] 10.172.54.68
10.172.54.69: 17:14:41 up 549 days, 23:52, 2 users, load average: 0.28, 0.12, 0.08
[2] 17:14:41 [SUCCESS] 10.172.54.69
10.172.54.70: 17:14:41 up 549 days, 23:48, 2 users, load average: 0.10, 0.13, 0.20
[3] 17:14:41 [SUCCESS] 10.172.54.70
[cuadmin@cdh68 ~]$ ll /tmp/pssh/
total 12
-rw-r----- 1 cuadmin cuadmin 72 Nov 5 17:14 10.172.54.68
-rw-r----- 1 cuadmin cuadmin 72 Nov 5 17:14 10.172.54.69
-rw-r----- 1 cuadmin cuadmin 72 Nov 5 17:14 10.172.54.70
[cuadmin@cdh68 ~]$ cat /tmp/pssh/*
17:14:41 up 549 days, 23:54, 4 users, load average: 0.07, 0.09, 0.16
17:14:41 up 549 days, 23:52, 2 users, load average: 0.28, 0.12, 0.08
17:14:41 up 549 days, 23:48, 2 users, load average: 0.10, 0.13, 0.20
四、pscp
pscp
用來將文件並行的複製到多個主機上。
pscp
和pssh
的很多參數都是一樣的,就不在多說了,舉一個簡單的例子來說明一下pscp
的簡單實用。
基本使用方法:
pscp -h ip文件 本地文件 遠程目錄
例子:將本機的/tmp/i.txt
文件複製到ip.txt
列出的主機上的/tmp
目錄
[cuadmin@cdh68 ~]$ cat /tmp/i.txt
hello pssh!
[cuadmin@cdh68 ~]$ pscp -h ip.txt /tmp/i.txt /tmp/
[1] 17:24:00 [SUCCESS] 10.172.54.70
[2] 17:24:00 [SUCCESS] 10.172.54.68
[3] 17:24:00 [SUCCESS] 10.172.54.69
#傳目錄
[cuadmin@cdh68 ~]$ pscp -r -h ip.txt test/ /tmp/dir1/
特別參數說明:
-
-r:用於遞歸的複製目錄
傳文件,不支持遠程新建目錄
五、pslurp
pslurp
把文件並行地從多個遠程主機複製到中心主機上,從遠程拉取文件到本地,在本地自動創建目錄名爲遠程主機ip的目錄,將拉取的文件放在對應主機IP目錄下
基本用法:
pslurp -h ip.txt -L <本地目錄> <遠程目錄/文件> <本地重命名>
例子:將ip.txt
列出的主機上的/tmp/i.txt
文件複製到本機的/data1/tmp/ip/i.txt
[cuadmin@cdh68 ~]$ pslurp -h ip.txt -l cuadmin -L /data1/tmp /tmp/i.txt i.txt
[1] 17:34:33 [SUCCESS] 10.172.54.70
[2] 17:34:33 [SUCCESS] 10.172.54.68
[3] 17:34:33 [SUCCESS] 10.172.54.69
[cuadmin@cdh68 ~]$ ll /data1/tmp/
total 12
drwxr-x--- 2 cuadmin cuadmin 4096 Nov 5 17:34 10.172.54.68
drwxr-x--- 2 cuadmin cuadmin 4096 Nov 5 17:34 10.172.54.69
drwxr-x--- 2 cuadmin cuadmin 4096 Nov 5 17:34 10.172.54.70
[cuadmin@cdh68 ~]$ ll /data1/tmp/10.172.54.68
total 4
-rw-r----- 1 cuadmin cuadmin 12 Nov 5 17:34 i.txt
[cuadmin@cdh68 ~]$ cat /data1/tmp/10.172.54.68/i.txt
hello pssh!
#拉取目錄
[cuadmin@cdh68 ~]$ pslurp -r -h ip.txt -L /root/ /root/test temp
特別參數說明:
-L 指定本地存放文件的目錄
雖然幫助文檔中說 -L
參數是可選的,但是經過我試驗 -L
參數是必須的,要不然會報錯。
增加 -L
參數後,會在 -L
指定的本地目錄下創建[用戶@]主機IP[:端口]
目錄,對應的目錄下用於存放從改主機複製過來的文件。
六、pnuke
pnuke
並行地在多個遠程主機上殺死進程。
pnuke
的參數與pssh
一樣,只是最後的字符串爲要結束的進程名稱
例子:將ip.txt
列出的主機上的httpd
進程殺死
[cuadmin@cdh68 ~]$ pnuke -h ip.txt -l cuadmin httpd
[1] 09:55:28 [SUCCESS] 10.172.54.68
[2] 09:55:28 [SUCCESS] 10.172.54.69
[3] 09:55:28 [SUCCESS] 10.172.54.70
7、prsync
並行傳輸文件到遠端
#傳文件,支持遠程新建目錄,即目錄不存在則新建
prsync -h ip.txt test.py /tmp/dir2/
#傳目錄
prsync -r -h ip.txt test/ /tmp/dir3/
本文分享自微信公衆號 - 大數據AI(songxt1990)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。