7

image.png

parallel-ssh is an asynchronous parallel SSH library designed for small-scale automation!

Parallel-ssh is an asynchronous parallel SSH library designed for small-scale automation, including pssh, pscp, prsync, pslurp and pnuke tools. Its source code is written and developed in Python. The project was originally located on Google Code and was written and maintained by Brent N. Chun, but due to his busy schedule, Brent transferred the maintenance work to Andrew McNabb in October 2009. In 2012, due to the closure of Google Code, the project was abandoned for a while, and now only the current version can be found in the Google Code archive.

But it should be noted that the previous version does not support Python3, but someone on Github Fork a copy, and I modified it to support Python3 and above. At the same time, there is another organization that specifically developed and maintained parallel-ssh, and it hasn’t been updated for a long time after looking at it today. If necessary, you can check it yourself.

https://github.com/lilydjwg/pssh

https://github.com/ParallelSSH/parallel-ssh

  • Scalability

    • Supports expansion to hundreds or even thousands of hosts
  • Easy to use

    • You can run commands on any number of hosts with just two lines of code
  • Efficient execution

    • Claimed to be the fastest Python SSH library available
  • Resource usage

    • Compared to other Python SSH libraries, it consumes the least resources

installation

# Mac系统安装
$ brew install pssh

# CentOS系统安装
$ yum install pssh

# Ubuntu系统安装
$ apt install pssh

# PIP安装
$ pip insall pssh

Source code compilation and installation (2.3.1)

# 官方地址: https://code.google.com/archive/p/parallel-ssh/source/default/source
$ tar zxvf pssh-2.3.1.tar.gz
$ cd pssh-2.3.1
$ python setup.py install

image.png

# 工具对应的子命令子命令
$ ls -lh /usr/local/Cellar/pssh/2.3.1_1/bin/
pnuke -> ../libexec/bin/pnuke
prsync -> ../libexec/bin/prsync
pscp -> ../libexec/bin/pscp
pslurp -> ../libexec/bin/pslurp
pssh -> ../libexec/bin/pssh
pssh-askpass -> ../libexec/bin/pssh-askpass

image.png

pssh

Run commands in parallel on multiple hosts via ssh protocol

Command parameter usage

image.png

Application example

# Usage: pssh [OPTIONS] command [...]

# 在两个主机上运行命令并在每个服务器上打印其输出
$ pssh -i -H "host1 host2" hostname -i

# 运行命令并将输出保存到单独的文件中
$ pssh -H host1 -H host2 -o path/to/output_dir hostname -i

# 在多个主机上运行命令并在新行分隔的文件中指定
$ pssh -i -h path/to/hosts_file hostname -i

# 以root运行命令(要求输入root用户密码)
$ pssh -i -h path/to/hosts_file -A -l root_username hostname -i

# 运行带有额外SSH参数的命令
$ pssh -i -h path/to/hosts_file -x "-O VisualHostKey=yes" hostname -i

# 运行并行连接数量限制为10的命令
$ pssh -i -h path/to/hosts_file -p 10 'cd dir; ./script.sh; exit'

pscp

Copy files to multiple hosts in parallel via ssh protocol

Command parameter usage

上传中![image.png

# Usage: pscp [OPTIONS] local remote

# 将本地文件复制到远程机器上
$ pscp -h hosts.txt -l root foo.txt /home/irb2/foo.txt
[1] 23:00:08 [SUCCESS] 172.18.10.25
[2] 09:52:28 [SUCCESS] 172.18.10.24

3. prsync

Copy files efficiently to multiple hosts in parallel through the rsync protocol

Command parameter usage

image.png

Application example

# Usage: prsync [OPTIONS] local remote

# 使用rsync协议进行本地文件复制操作
$ prsync -r -h hosts.txt -l root foo /home/irb2/foo

4. pslurp

Copy files from multiple remote hosts to the central host in parallel through the ssh protocol

Command parameter usage

image.png

Application example

# Usage: pslurp [OPTIONS] remote local

# 将远程主机上面的文件复制到本地
$ pslurp -h hosts.txt -l root -L /tmp/outdir /home/irb2/foo.txt foo.txt

5. pnuke

Kill processes on multiple remote hosts in parallel via ssh protocol

Command parameter usage

image.png

Application example

# Usage: pnuke [OPTIONS] pattern

# 结束远程主机上面的进程任务
$ pnuke -h hosts.txt -l root java

Author: Escape
Link: https://www.escapelife.site/posts/8c0f83d.html


民工哥
26.4k 声望56.7k 粉丝

10多年IT职场老司机的经验分享,坚持自学一路从技术小白成长为互联网企业信息技术部门的负责人。2019/2020/2021年度 思否Top Writer