自动化运维之PSSH

1.PSSH简介

PSSH提供OpenSSH和相关工具的并行版本。包括pssh,pscp,prsync,pnuke和pslurp。该项目包括psshlib,可以在自定义应用程序中使用。 pssh是python写的可以并发在多台机器上批量执行命令的工具,它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。杀手锏是文件并行复制,,当进行再远程主机批量上传下载的时候,最好使用它。 项目地址: https://code.google.com/archive/p/parallel-ssh/

2.PSSH使用

2.1 安装PSSH

前提条件:注意需要安装 python 2.4 或以上版本

wget https://clsn.io/files/pssh/pssh-2.3.1.tar.gz
tar zxf pssh-2.3.1.tar.gz
cd pssh-2.3.1
python setup.py install

安装过程

[root@clsn.io /opt/pssh-2.3.1] 
#python setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/psshlib
copying psshlib/task.py -> build/lib/psshlib
copying psshlib/askpass_server.py -> build/lib/psshlib
copying psshlib/__init__.py -> build/lib/psshlib
copying psshlib/cli.py -> build/lib/psshlib
copying psshlib/psshutil.py -> build/lib/psshlib
copying psshlib/manager.py -> build/lib/psshlib
copying psshlib/askpass_client.py -> build/lib/psshlib
copying psshlib/version.py -> build/lib/psshlib
copying psshlib/color.py -> build/lib/psshlib
running build_scripts
creating build/scripts-2.6
copying and adjusting bin/pssh -> build/scripts-2.6
copying and adjusting bin/pnuke -> build/scripts-2.6
copying and adjusting bin/prsync -> build/scripts-2.6
copying and adjusting bin/pslurp -> build/scripts-2.6
copying and adjusting bin/pscp -> build/scripts-2.6
copying and adjusting bin/pssh-askpass -> build/scripts-2.6
changing mode of build/scripts-2.6/pssh from 644 to 755
changing mode of build/scripts-2.6/pnuke from 644 to 755
changing mode of build/scripts-2.6/prsync from 644 to 755
changing mode of build/scripts-2.6/pslurp from 644 to 755
changing mode of build/scripts-2.6/pscp from 644 to 755
changing mode of build/scripts-2.6/pssh-askpass from 644 to 755
running install_lib
creating /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/task.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/askpass_server.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/__init__.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/cli.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/psshutil.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/manager.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/askpass_client.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/version.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/color.py -> /usr/lib/python2.6/site-packages/psshlib
byte-compiling /usr/lib/python2.6/site-packages/psshlib/task.py to task.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_server.py to askpass_server.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/cli.py to cli.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/psshutil.py to psshutil.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/manager.py to manager.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_client.py to askpass_client.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/version.py to version.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/color.py to color.pyc
running install_scripts
copying build/scripts-2.6/pscp -> /usr/bin
copying build/scripts-2.6/prsync -> /usr/bin
copying build/scripts-2.6/pnuke -> /usr/bin
copying build/scripts-2.6/pssh -> /usr/bin
copying build/scripts-2.6/pssh-askpass -> /usr/bin
copying build/scripts-2.6/pslurp -> /usr/bin
changing mode of /usr/bin/pscp to 755
changing mode of /usr/bin/prsync to 755
changing mode of /usr/bin/pnuke to 755
changing mode of /usr/bin/pssh to 755
changing mode of /usr/bin/pssh-askpass to 755
changing mode of /usr/bin/pslurp to 755
running install_data
creating /usr/man
creating /usr/man/man1
copying man/man1/pssh.1 -> /usr/man/man1
copying man/man1/pscp.1 -> /usr/man/man1
copying man/man1/prsync.1 -> /usr/man/man1
copying man/man1/pslurp.1 -> /usr/man/man1
copying man/man1/pnuke.1 -> /usr/man/man1
running install_egg_info
Writing /usr/lib/python2.6/site-packages/pssh-2.3.1-py2.6.egg-info

2.2 安装完成

使用yum安装后,只能使用pssh,但是编译安装后会包括其他命令也安装了(pscp prsync pnuke pslurp) pssh:在远程多台主机上并行运行命令

Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime

pscp :把文件并行复制到多台远程主机上

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

prsync:使用rsync协议本地文件同步到远程多台主机上。

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

pnuke:在远程多台主机上并行killall某一进程

Example: pnuke -h hosts.txt -l irb2 java

pslurp:把文件从远程多台主机上复制到本地主机上

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

2.3 pssh参数说明

-h HOST_FILE 后边跟远程主机列表(ip) -H HOST_STRING 后边跟远程主机名或者ip地址 -l USER 指定远程主机的用户名-p PAR 指定pssh最大的并行线程数。 -o 将输出的内容重定向到一个指定的文件中 -O 指定ssh参数的具体配置 -e 将执行错误重定向到一个指定的文件中 -t 设定命令执行超时时间 -x 传递ssh命令的一些参数 -i 在远程主机上执行命令完成后显示标准输出和标准错误 -P 在执行远程命令时,输出执行结果

2.4 其他常用的方法

pssh使用帮助

pssh --help

pssh查看所有服务器的uptime

    -h list 指定了执行命令的机器列表
    -A表示提示输入密码(如果机器都是ssh key打通的则无需加-A)
pssh -i -A -h list 'uptime'

使用pscp向一堆机器分发文件

pscp -h list  localfile   remote_dir

从一堆机器中拷贝文件到中心机器

pslurp -h list /etc/hosts local_dir

2.5 常见问题

如果你遇到这样的错误:

IOError: [Errno 4] Interrupted system call

建议升级python版本到python2.7

3.附录

3.1 pssh CLSN站点镜像地址

本站将pssh软件包镜像到本地,以防丢失,有需求的可以执行下载,下载后注意校验MD5值。

File

Summary + Labels

Uploaded

Size

pssh-2.3.1.tar.gz

PSSH 2.3.1 Source

Feb 3, 2012

22.88KB

pssh-2.3.tar.gz

PSSH 2.3 Source

Jan 25, 2012

21.62KB

pssh-2.2.2.tar.gz

PSSH 2.2.2 Source

Feb 3, 2011

20.94KB

pssh-2.2.1.tar.gz

PSSH 2.2.1 Source

Jan 27, 2011

20.88KB

pssh-2.2.tar.gz

PSSH 2.2 Source

Jan 27, 2011

20.84KB

pssh-2.1.1.tar.gz

PSSH 2.1.1 Source

Mar 5, 2010

17.94KB

pssh-2.1.tar.gz

PSSH 2.1 Source

Feb 26, 2010

17.65KB

pssh-2.0.tar.gz

PSSH 2.0 Source

Oct 21, 2009

15.03KB

3.2 pssh GOOGLE源

File

Summary + Labels

Uploaded

Size

pssh-2.3.1.tar.gz​

PSSH 2.3.1 Source

Feb 3, 2012

22.88KB

​pssh-2.3.tar.gz​

PSSH 2.3 Source

Jan 25, 2012

21.62KB

​pssh-2.2.2.tar.gz​

PSSH 2.2.2 Source

Feb 3, 2011

20.94KB

​pssh-2.2.1.tar.gz​

PSSH 2.2.1 Source

Jan 27, 2011

20.88KB

​pssh-2.2.tar.gz​

PSSH 2.2 Source

Jan 27, 2011

20.84KB

​pssh-2.1.1.tar.gz​

PSSH 2.1.1 Source

Mar 5, 2010

17.94KB

​pssh-2.1.tar.gz​

PSSH 2.1 Source

Feb 26, 2010

17.65KB

​pssh-2.0.tar.gz​

PSSH 2.0 Source

Oct 21, 2009

15.03KB

3.3 文件包MD5值

下载完成后务必校验MD5值

MD5值

文件名

dbdbfda5681b82d4c64abdb3668e88bc

pssh-2.3.tar.gz

53e70d53064f658bf8f44880340e8e5c

pssh-2.3.1.tar.gz

689207677b260a7e4178174295b1389e

pssh-2.2.tar.gz

865305ae39647884bc54a42e9f9554bb

pssh-2.2.2.tar.gz

f7583259976ef64bf3cae957a0b66e41

pssh-2.2.1.tar.gz

725f902bb7e128cc7aa1322551f7ca37

pssh-2.1.tar.gz

4b355966da91850ac530f035f7404cd5

pssh-2.1.1.tar.gz

07474f9f9dbefedc809e997f04e1dee5

pssh-2.0.tar.gz

4.参考文献

https://code.google.com/archive/p/parallel-ssh/ http://opstool.com/article/266 https://www.cnblogs.com/Dicky-Zhang/p/5988415.html https://help.aliyun.com/knowledge_detail/38835.html

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券