批量執行工具PSSH詳解

批量執行工具PSSH詳解

pssh是一個python編寫可以在多臺服務器上執行命令的工具,同時支持拷貝文件,是同類工具中很出色的,使用必須在各個服務器上配置好密鑰認證訪問。

安裝pssh包

yum 安裝:

yum install pssh -y

編譯安裝:

# 安裝setuptools包,如果安裝了跳過
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz --no-check-certificate
tar -xf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install
# 安裝pssh
cd -
wget  http://www.theether.org/pssh/pssh-1.4.3.tar.gz
cd pssh-1.4.3/python setup.py install

pssh包的命令介紹

  • pssh 在多個主機上並行運行命令

  • pscp 把文件並行複製到多個主機上

  • prsync 通過rsync協議把文件高效並行複製到多個主機上

  • pslurp 把文件並行地從多個遠程主機複製到中心主機上

  • pnuke 並行地在多個遠程主機上殺死進程

pssh 命令

pssh: 查詢遠程主機的信息

pssh 
Usage: pssh [OPTIONS] -h hosts.txt prog [arg0] ..
-h --hosts 主機文件列表,格式"host[:port] [user]"
-l --user  用戶名
-p --par 併發線程數
-o --outdir 輸出的文件目錄
-e --errdir 錯誤輸出的文件目錄
-t --timeout 設置命令執行超時時間 
-1表示無限制
 --options 設置ssh的一些選項
 -v --verbose 詳細模式
 -P --print 打印出輸出執行信息
 -i --inline 在每臺host執行完畢後,顯示出輸出信息
 Example: pssh -h nodes.txt -l irb2 -o /tmp/pssh uptime

[root@localhost ~]# cat /etc/pssh_hosts 
web1.jiajie.com
web2.jiajie.com
web3.jiajie.com

“-O”參數,後面跟 的“StrictHostKeyChecking=no”是sshd服務的配置文件ssh_config 中的一個選項,通過設置這個參數,可以讓遠程主機自動接受 本地主機的hostkey。第一次執行必須指定,不然命令不能執行。

[root@localhost ~]# pssh -i -O "StrictHostKeyChecking=no" -h /etc/pssh_hosts 'date'
[1] 09:56:38 [SUCCESS] web1.jiajie.com 222017年 10月 17日 星期二 09:56:38 CST
[2] 09:56:38 [SUCCESS] web2.jiajie.com 222017年 10月 17日 星期二 09:56:38 CST
[3] 09:56:38 [SUCCESS] web3.jiajie.com 222017年 10月 17日 星期二 09:56:37 CST

[root@localhost ~]# pssh -i -h /etc/pssh_hosts 'uptime'
[1] 09:59:50 [SUCCESS] web1.jiajie.com 22
 09:59:50 up  1:20,  1 user,  load average: 0.06, 0.01, 0.00
 [2] 09:59:50 [SUCCESS] web3.jiajie.com 22
 09:59:49 up  1:20,  2 users,  load average: 0.00, 0.01, 0.05
 [3] 09:59:50 [SUCCESS] web2.jiajie.com 22
 09:59:50 up  1:20,  1 user,  load average: 0.06, 0.02, 0.00
 
 [root@localhost ~]# pssh -i -h /etc/pssh_hosts "tar -zxvf pssh-1.4.3.tar.gz"
 [root@localhost ~]# pssh -i -h /etc/pssh_hosts "rpm -qa httpd|wc -l"
 [1] 10:20:18 [SUCCESS] web2.jiajie.com 22
 0
 [2] 10:20:19 [SUCCESS] web1.jiajie.com 22
 0
 [3] 10:20:19 [SUCCESS] web3.jiajie.com 22
 0

pscp 命令

pscp: 把文件或者目錄並行的複製到多個主機上面

Usage: pscp [OPTIONS] -h hosts.txt local remote
-r 遞歸複製目錄
-h 主機文件列表,格式"host[:port] [user]"-l 用戶名
-p --par 併發線程數
-o --outdir 輸出的文件目錄
-e --errdir 錯誤輸出的文件目錄
-t --timeout 設置命令執行超時時間 -1表示無限制
-O --options 設置ssh的一些選項
-v --verbose 詳細模式  
Example: pscp -h hosts.txt -l irb2 foo.txt /home/irb2/foo.txt

[root@localhost ~]# pscp -h /etc/pssh_hosts /etc/hosts /tmp/
[1] 10:29:11 [SUCCESS] web2.jiajie.com 22
[2] 10:29:11 [SUCCESS] web1.jiajie.com 22
[3] 10:29:11 [SUCCESS] web3.jiajie.com 22
[root@localhost ~]# pssh -i -h /etc/pssh_hosts "ls -l /tmp/hosts"
[1] 10:29:57 [SUCCESS] web1.jiajie.com 22
-rw-r--r-- 1 root root 202 10月 17 10:29 /tmp/hosts
[2] 10:29:57 [SUCCESS] web2.jiajie.com 22
-rw-r--r-- 1 root root 202 10月 17 10:29 /tmp/hosts
[3] 10:29:57 [SUCCESS] web3.jiajie.com 22
-rw-r--r-- 1 root root 202 10月 17 10:29 /tmp/hosts

[root@localhost ~]# pscp -r -h /etc/pssh_hosts /root/shell.sh/ /tmp/
[1] 10:33:18 [SUCCESS] web1.jiajie.com 22
[2] 10:33:18 [SUCCESS] web2.jiajie.com 22
[3] 10:33:18 [SUCCESS] web3.jiajie.com 22

pslurp 命令(從遠程主機複製到本地。)

Usage: pslurp [OPTIONS] 
-h hosts.txt 
-o outdir remote local  
-r --recursive recusively copy directories (OPTIONAL)  
-L --localdir  output directory for remote file copies  
-h --hosts     hosts file (each line "host[:port] [login]")  
-l --user      username (OPTIONAL)  
-p --par       max number of parallel threads (OPTIONAL) 
-o --outdir    output directory for stdout files (OPTIONAL)  
-e --errdir    output directory for stderr files (OPTIONAL)  
-t --timeout   timeout (secs) (-1 = no timeout) per host (OPTIONAL)  
-v --verbose   turn on warning and diagnostic messages (OPTIONAL)  
-O --options   SSH options (OPTIONAL)

Example: pslurp -h hosts.txt -L /tmp/outdir -l irb2  /home/irb2/foo.txt foo.txt

-L:指定從遠程主機拷貝文件放置的目錄,拷貝/root/expect.sh文件,並將其重命名爲test1. 如果拷貝目錄需要使用-r參數:

[root@localhost ~]# pslurp -h /etc/pssh_hosts -L /tmp/ /root/expect.sh test1 
[1] 10:44:55 [FAILURE] web1.jiajie.com 22 Received error code of 1
[2] 10:44:56 [FAILURE] web3.jiajie.com 22 Received error code of 1
[3] 10:44:56 [SUCCESS] web2.jiajie.com 22
[root@localhost ~]# ll /tmp/web2.jiajie.com/
總用量 4-rw-r--r-- 1 root root 139 10月 17 10:44 test1

[root@localhost ~]# pslurp -r -h /etc/pssh_hosts -L /tmp/ /root/shell.sh      test2

prsync 命令:

Usage: prsync [OPTIONS] 
-h hosts.txt local remote  
-r --recursive recusively copy directories (OPTIONAL)  
-a --archive   use rsync -a (archive mode) (OPTIONAL)  
-z --compress  use rsync compression (OPTIONAL)  
-h --hosts     hosts file (each line "host[:port] [login]")  
-l --user      username (OPTIONAL)  
-p --par       max number of parallel threads (OPTIONAL)  
-o --outdir    output directory for stdout files (OPTIONAL)  
-e --errdir    output directory for stderr files (OPTIONAL)  
-t --timeout   timeout (secs) (-1 = no timeout) per host (OPTIONAL)  
-v --verbose   turn on warning and diagnostic messages (OPTIONAL)  
-O --options   SSH options (OPTIONAL)

Example: prsync -r -h hosts.txt -l irb2 foo /home/irb2/foo

prsync的主要作用是通過rsync協議將文件或目錄從本地主 機同步到遠程多個主機上。

[root@localhost ~]# prsync -h /etc/pssh_hosts -a -r /root/shell.sh /tmp
[1] 11:33:26 [SUCCESS] web3.jiajie.com 22
[2] 11:33:26 [SUCCESS] web1.jiajie.com 22
[3] 11:33:27 [SUCCESS] web2.jiajie.com 22

-r:遞歸複製。

-a:保持文件的屬性不變。

-z:壓縮文件,一般不推薦。

pnuke 命令:

Usage: pnuke [OPTIONS] 
-h hosts.txt pattern  
-h --hosts   hosts file (each line "host[:port] [user]")  
-l --user    username (OPTIONAL)  
-p --par     max number of parallel threads (OPTIONAL)  
-o --outdir  output directory for stdout files (OPTIONAL)  
-e --errdir  output directory for stderr files (OPTIONAL)  
-t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL)  
-v --verbose turn on warning and diagnostic messages (OPTIONAL)  
-O --options SSH options (OPTIONAL)
Example: pnuke -h hosts.txt -l irb2 java

pnuke的主要作用是在遠程多主機上並行殺掉某一進程, 相當於killall命令。

可以直接使用pssh 來傳遞killall命令,這裏就不做介紹了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章