1、pssh 簡介
PSSH提供OpenSSH和相關工具的並行版本。包括pssh,pscp,prsync,pnuke和pslurp。該項目包括psshlib,可以在自定義應用程序中使用。
pssh是python編寫,可以並發在多台機器上批量執行命令的工具,用法可以媲美ansible的一些簡單用法,執行起來速度比ansible快,支持文件並行復制,遠程命令。
2、pssh安裝
3、pssh包的命令介紹
- pssh 在多個主機上並行運行命令
- pscp 把文件並行復制到多個主機上
- prsync 通過rsync協議把文件高效並行復制到多個主機上
- pslurp 把文件並行地從多個遠程主機復制到中心主機上
- pnuke 並行地在多個遠程主機上殺死進程
4、pssh包的命令參數說明及使用
為命令執行使用先准備一個主機列表文件,用於連接對應主機執行遠程命令
內容可以是主機名,也可以直接填寫IP, 格式"host[:port] [user]"
如:cat /home/pssh/pssh_hosts
192.168.1.122
xxx.xxx.xxx.xxx
1)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表示無限制 -O --options 設置ssh的一些選項 -v --verbose 詳細模式 -P --print 打印出輸出執行信息 -i --inline 在每台host執行完畢后,顯示出輸出信息 Example: pssh -h nodes.txt -l irb2 -o /tmp/pssh uptime
pssh --help Usage: pssh [OPTIONS] command [...] Options: --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 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
pssh命令使用
pssh -i -O "StrictHostKeyChecking=no" -h /home/pssh/pssh_hosts 'date' //第一次執行時使用 -O 參數 ,“-O”參數,后面跟的“StrictHostKeyChecking=no”是sshd服務的配置文件ssh_config中的一個選項,通過設置這個參數,可以讓遠程主機自動接受本地主機的hostkey。第一次執行必須指定,不然報錯。
pssh -i -l root -h /home/pssh/pssh_hosts 'date' //可以指定命令執行用戶 -l root
pssh -i -v -p 2 -h /home/pssh/pssh_hosts 'date' // 指定並發線程數 -p 2, -v 打印詳細信息
pssh -i -o /tmp/pssh.correct -e /tmp/pssh.error -h /home/pssh/pssh_hosts 'cat /etc/passwd |grep root; ls 3304' //使用-o把正確的信息輸出到/tmp/pssh.correct目錄下的對應以主機名或ip命名的文件中,使用-e把錯誤的信息輸出到/tmp/pssh.error目錄下的對應以主機名或ip命名的文件
2)pscp命令參數及使用:把文件並行復制到多個主機上
pscp命令參數
pscp --help Usage: pscp [OPTIONS] -h hosts.txt local remote Options: --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 -r, --recursive recusively copy directories (OPTIONAL) Example: pscp -h hosts.txt -l irb2 foo.txt /home/irb2/foo.txt
pscp命令使用
pscp -h pssh_hosts -r /home/pssh /tmp/pssh/ //-r遞歸復制將/home/pssh目錄復制到主機的/home/pssh/目錄下
3)prsync 命令參數及使用:通過rsync協議把文件高效並行復制到多個主機上
prsync命令參數
prsync --help Usage: prsync [OPTIONS] -h hosts.txt local remote Options: --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 -r, --recursive recusively copy directories (OPTIONAL) -a, --archive use rsync -a (archive mode) (OPTIONAL) -z, --compress use rsync compression (OPTIONAL) -S ARGS, --ssh-args=ARGS extra arguments for ssh Example: prsync -r -h hosts.txt -l irb2 foo /home/irb2/foo
prsync命令使用
prsync -h pssh_hosts -r /home/pssh /tmp/pssh/ //-r遞歸將/home/pssh傳到各主機 /tmp/pssh/目錄下
4)pslurp 命令參數及使用:把文件並行地從多個遠程主機復制到中心主機上
pslurp 命令參數
pslurp Usage: pslurp [OPTIONS] -h hosts.txt remote local pslurp: error: Paths not specified. [root@c43a02001.cloud.a02.amtest1221 /root] #pslurp --help Usage: pslurp [OPTIONS] -h hosts.txt remote local Options: --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 -r, --recursive recusively copy directories (OPTIONAL) -L LOCALDIR, --localdir=LOCALDIR output directory for remote file copies Example: pslurp -h hosts.txt -L /tmp/outdir -l irb2 /home/irb2/foo.txt foo.txt
pslurp 命令使用
pslurp -h /home/pssh/pssh_hosts -L /tmp/pssh -l root /tmp/pssh.test test1 //-L:指定從遠程主機拷貝文件放置/tmp/pssh目錄下的用遠程主機IP或主機名命名的目錄下,拷貝/tmp/pssh.test文件,並將其重命名為test1,如果拷貝目錄需要使用-r參數
5) pnuke命令參數及使用:並行地在多個遠程主機上殺死進程
pnuke命令參數
pnuke --help Usage: pnuke [OPTIONS] -h hosts.txt pattern Options: --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 Example: pnuke -h hosts.txt -l irb2 java
pnuke命令使用
pnuke -h /home/pssh/pssh_hosts tail //刪除遠程主機上tail 進程
5、pssh外料