1.PSSH简介

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

2.PSSH使用

2.1 安装PSSH

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

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

安装过程

  1. [root@clsn.io /opt/pssh-2.3.1]
  2. #python setup.py install
  3. running install
  4. running build
  5. running build_py
  6. creating build
  7. creating build/lib
  8. creating build/lib/psshlib
  9. copying psshlib/task.py -> build/lib/psshlib
  10. copying psshlib/askpass_server.py -> build/lib/psshlib
  11. copying psshlib/__init__.py -> build/lib/psshlib
  12. copying psshlib/cli.py -> build/lib/psshlib
  13. copying psshlib/psshutil.py -> build/lib/psshlib
  14. copying psshlib/manager.py -> build/lib/psshlib
  15. copying psshlib/askpass_client.py -> build/lib/psshlib
  16. copying psshlib/version.py -> build/lib/psshlib
  17. copying psshlib/color.py -> build/lib/psshlib
  18. running build_scripts
  19. creating build/scripts-2.6
  20. copying and adjusting bin/pssh -> build/scripts-2.6
  21. copying and adjusting bin/pnuke -> build/scripts-2.6
  22. copying and adjusting bin/prsync -> build/scripts-2.6
  23. copying and adjusting bin/pslurp -> build/scripts-2.6
  24. copying and adjusting bin/pscp -> build/scripts-2.6
  25. copying and adjusting bin/pssh-askpass -> build/scripts-2.6
  26. changing mode of build/scripts-2.6/pssh from 644 to 755
  27. changing mode of build/scripts-2.6/pnuke from 644 to 755
  28. changing mode of build/scripts-2.6/prsync from 644 to 755
  29. changing mode of build/scripts-2.6/pslurp from 644 to 755
  30. changing mode of build/scripts-2.6/pscp from 644 to 755
  31. changing mode of build/scripts-2.6/pssh-askpass from 644 to 755
  32. running install_lib
  33. creating /usr/lib/python2.6/site-packages/psshlib
  34. copying build/lib/psshlib/task.py -> /usr/lib/python2.6/site-packages/psshlib
  35. copying build/lib/psshlib/askpass_server.py -> /usr/lib/python2.6/site-packages/psshlib
  36. copying build/lib/psshlib/__init__.py -> /usr/lib/python2.6/site-packages/psshlib
  37. copying build/lib/psshlib/cli.py -> /usr/lib/python2.6/site-packages/psshlib
  38. copying build/lib/psshlib/psshutil.py -> /usr/lib/python2.6/site-packages/psshlib
  39. copying build/lib/psshlib/manager.py -> /usr/lib/python2.6/site-packages/psshlib
  40. copying build/lib/psshlib/askpass_client.py -> /usr/lib/python2.6/site-packages/psshlib
  41. copying build/lib/psshlib/version.py -> /usr/lib/python2.6/site-packages/psshlib
  42. copying build/lib/psshlib/color.py -> /usr/lib/python2.6/site-packages/psshlib
  43. byte-compiling /usr/lib/python2.6/site-packages/psshlib/task.py to task.pyc
  44. byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_server.py to askpass_server.pyc
  45. byte-compiling /usr/lib/python2.6/site-packages/psshlib/__init__.py to __init__.pyc
  46. byte-compiling /usr/lib/python2.6/site-packages/psshlib/cli.py to cli.pyc
  47. byte-compiling /usr/lib/python2.6/site-packages/psshlib/psshutil.py to psshutil.pyc
  48. byte-compiling /usr/lib/python2.6/site-packages/psshlib/manager.py to manager.pyc
  49. byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_client.py to askpass_client.pyc
  50. byte-compiling /usr/lib/python2.6/site-packages/psshlib/version.py to version.pyc
  51. byte-compiling /usr/lib/python2.6/site-packages/psshlib/color.py to color.pyc
  52. running install_scripts
  53. copying build/scripts-2.6/pscp -> /usr/bin
  54. copying build/scripts-2.6/prsync -> /usr/bin
  55. copying build/scripts-2.6/pnuke -> /usr/bin
  56. copying build/scripts-2.6/pssh -> /usr/bin
  57. copying build/scripts-2.6/pssh-askpass -> /usr/bin
  58. copying build/scripts-2.6/pslurp -> /usr/bin
  59. changing mode of /usr/bin/pscp to 755
  60. changing mode of /usr/bin/prsync to 755
  61. changing mode of /usr/bin/pnuke to 755
  62. changing mode of /usr/bin/pssh to 755
  63. changing mode of /usr/bin/pssh-askpass to 755
  64. changing mode of /usr/bin/pslurp to 755
  65. running install_data
  66. creating /usr/man
  67. creating /usr/man/man1
  68. copying man/man1/pssh.1 -> /usr/man/man1
  69. copying man/man1/pscp.1 -> /usr/man/man1
  70. copying man/man1/prsync.1 -> /usr/man/man1
  71. copying man/man1/pslurp.1 -> /usr/man/man1
  72. copying man/man1/pnuke.1 -> /usr/man/man1
  73. running install_egg_info
  74. Writing /usr/lib/python2.6/site-packages/pssh-2.3.1-py2.6.egg-info

2.2 安装完成

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

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

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

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

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

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

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

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

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

  1. Example: pslurp -h hosts.txt -L /tmp/outdir -l irb2
  2. /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使用帮助

  1. pssh --help

pssh查看所有服务器的uptime

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

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

  1. pscp -h list localfile remote_dir

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

  1. pslurp -h list /etc/hosts local_dir

2.5 常见问题

如果你遇到这样的错误:

  1. 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