自动化运维之 PSSH

Posted 51reboot运维开发

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化运维之 PSSH相关的知识,希望对你有一定的参考价值。


01
PSSH 简介

PSSH 提供 OpenSSH 和相关工具的并行版本。包括 pssh,pscp,prsync,pnuke 和 pslurp。该项目包括 psshlib,可以在自定义应用程序中使用。


自动化运维之 PSSH


02
PSSH 使用


2.1
安装 PSSH



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

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

安装过程

[root@clsn.io /opt/pssh-2.3.1] #python setup.py installrunning installrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/psshlibcopying psshlib/task.py -> build/lib/psshlibcopying psshlib/askpass_server.py -> build/lib/psshlibcopying psshlib/__init__.py -> build/lib/psshlibcopying psshlib/cli.py -> build/lib/psshlibcopying psshlib/psshutil.py -> build/lib/psshlibcopying psshlib/manager.py -> build/lib/psshlibcopying psshlib/askpass_client.py -> build/lib/psshlibcopying psshlib/version.py -> build/lib/psshlibcopying psshlib/color.py -> build/lib/psshlibrunning build_scriptscreating build/scripts-2.6copying and adjusting bin/pssh -> build/scripts-2.6copying and adjusting bin/pnuke -> build/scripts-2.6copying and adjusting bin/prsync -> build/scripts-2.6copying and adjusting bin/pslurp -> build/scripts-2.6copying and adjusting bin/pscp -> build/scripts-2.6copying and adjusting bin/pssh-askpass -> build/scripts-2.6changing mode of build/scripts-2.6/pssh from 644 to 755changing mode of build/scripts-2.6/pnuke from 644 to 755changing mode of build/scripts-2.6/prsync from 644 to 755changing mode of build/scripts-2.6/pslurp from 644 to 755changing mode of build/scripts-2.6/pscp from 644 to 755changing mode of build/scripts-2.6/pssh-askpass from 644 to 755running install_libcreating /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/task.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/askpass_server.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/__init__.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/cli.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/psshutil.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/manager.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/askpass_client.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/version.py -> /usr/lib/python2.6/site-packages/psshlibcopying build/lib/psshlib/color.py -> /usr/lib/python2.6/site-packages/psshlibbyte-compiling /usr/lib/python2.6/site-packages/psshlib/task.py to task.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_server.py to askpass_server.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/__init__.py to __init__.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/cli.py to cli.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/psshutil.py to psshutil.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/manager.py to manager.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_client.py to askpass_client.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/version.py to version.pycbyte-compiling /usr/lib/python2.6/site-packages/psshlib/color.py to color.pycrunning install_scriptscopying build/scripts-2.6/pscp -> /usr/bincopying build/scripts-2.6/prsync -> /usr/bincopying build/scripts-2.6/pnuke -> /usr/bincopying build/scripts-2.6/pssh -> /usr/bincopying build/scripts-2.6/pssh-askpass -> /usr/bincopying build/scripts-2.6/pslurp -> /usr/binchanging mode of /usr/bin/pscp to 755changing mode of /usr/bin/prsync to 755changing mode of /usr/bin/pnuke to 755changing mode of /usr/bin/pssh to 755changing mode of /usr/bin/pssh-askpass to 755changing mode of /usr/bin/pslurp to 755running install_datacreating /usr/mancreating /usr/man/man1copying man/man1/pssh.1 -> /usr/man/man1copying man/man1/pscp.1 -> /usr/man/man1copying man/man1/prsync.1 -> /usr/man/man1copying man/man1/pslurp.1 -> /usr/man/man1copying man/man1/pnuke.1 -> /usr/man/man1running install_egg_infoWriting /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)

-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


03
附录


3.1
pssh CLSN 站点镜像地址


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

自动化运维之 PSSH


3.2
pssh GOOGLE 源


自动化运维之 PSSH


3.3
文件包MD5 值


下载完成后务必校验 MD5 值

自动化运维之 PSSH


04
参考文献


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





51Reboot 运维开发

Golang 课程

K8S 课程

Python 自动化进阶课程

Python 基础实战课程

运维前端课程

课程试听预约请扫码>>>



你点的每个赞,我都认真当成了喜欢



以上是关于自动化运维之 PSSH的主要内容,如果未能解决你的问题,请参考以下文章

超简单:Centos7部署轻量级自动化运维工具pssh

Centos7部署轻量级自动化运维工具pssh

轻量级自动化运维工具Pssh

puppet自动化运维之tag标签puppet自动化运维之tag标签

tree老师:PSSH自动化运维实战

运维自动化轻量级工具pssh