运维自动化轻量级工具pssh

1pssh介绍

pssh是python写的能够并发在多台机器上批量执行命令的工具,它的用法能够媲美ansible的一些简单用法,执行起来速度比ansible快它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。杀手锏是文件并行复制,,当进行再远程主机批量上传下载的时候,最好使用它。python

2pssh的使用

在使用pssh以前,必需要保证管理主机和本地主机进行过密钥的认证,或者是在进行批量时,没有作过密钥认证,可是必须保证被管理的多台主机的密码相同。关于如何作密钥认证,这里就很少说了,能够自行百度。api

2.1安装并发

官网地址:https://code.google.com/archive/p/parallel-ssh/downloads(须要能打开谷歌)ssh

或者是yum 来安装ide

wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/parallel-ssh/pssh-2.3.1.tar.gz
tar -zxvf pssh-2.3.1.tar.gz
cd pssh-2.3.1
python setup.py install
或者
yum install -y pssh

使用yum安装后,只能使用pssh,可是编译安装后会包括其余命令也安装了(pscp  prsync  pnuke  pslurp)工具

pssh:在远程多台主机上并行运行命令this

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

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

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

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

2.2 pssh经常使用参数

 1 pssh  --help
 2 
 3 Usage: pssh [OPTIONS] command [...]
 4 
 5 Options:
 6   --version             show program's version number and exit
 7   --help                show this help message and exit
 8   -h HOST_FILE, --hosts=HOST_FILE
 9                         hosts file (each line "[user@]host[:port]")
10   -H HOST_STRING, --host=HOST_STRING
11                         additional host entries ("[user@]host[:port]")
12   -l USER, --user=USER  username (OPTIONAL)
13   -p PAR, --par=PAR     max number of parallel threads (OPTIONAL)
14   -o OUTDIR, --outdir=OUTDIR
15                         output directory for stdout files (OPTIONAL)
16   -e ERRDIR, --errdir=ERRDIR
17                         output directory for stderr files (OPTIONAL)
18   -t TIMEOUT, --timeout=TIMEOUT
19                         timeout (secs) (0 = no timeout) per host (OPTIONAL)
20   -O OPTION, --option=OPTION
21                         SSH option (OPTIONAL)
22   -v, --verbose         turn on warning and diagnostic messages (OPTIONAL)
23   -A, --askpass         Ask for a password (OPTIONAL)
24   -x ARGS, --extra-args=ARGS
25                         Extra command-line arguments, with processing for
26                         spaces, quotes, and backslashes
27   -X ARG, --extra-arg=ARG
28                         Extra command-line argument
29   -i, --inline          inline aggregated output and error for each server
30   --inline-stdout       inline standard output for each server
31   -I, --send-input      read from standard input and send as input to ssh
32   -P, --print           print output as we get it
33 
34 Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime
View Code

具体经常使用介绍:

-h   HOST_FILE   后边跟远程主机列表(ip)

-H   HOST_STRING   后边跟远程主机名或者ip地址

-l   USER  指定远程主机的用户名

-p  PAR   指定pssh最大的并行线程数。

-o  将输出的内容重定向到一个指定的文件中

-O 指定ssh参数的具体配置

-e  将执行错误重定向到一个指定的文件中

-t  设定命令执行超时时间

-x  传递ssh命令的一些参数

-i  在远程主机上执行命令完成后显示标准输出和标准错误

-P  在执行远程命令时,输出执行结果

 用法实例:

pssh -P  -i   -h  sz_vpc.txt  "w" 

3pscp拷贝文件到远程主机

pscp  -h  ip.txt /etc/wenjian.txt   /tmp/

4pnuke杀掉某一进程

这个命令相似yu  killall命令

pnuke  -h  iplist.txt   httpd

上边的意思是在远程主机上批量关闭httpd服务

能经过killall关闭的服务,均可以经过pnuke来批量完成

 5 pslurp 远程主机拷贝文件到本地主机

pslurp -h iplist.txt -L /home/ /hose/wenjian/yuanc.conf open.conf

上边是,将全部远程主机/hose/wenjian/yuanc.conf复制到本地主机/home/目录下,而且从新命名为open.conf  -L 来指定本地文件路径

下面这个是拷贝目录

 pslurp -h iplist.txt -r -L /home/ /hose/wenjian/ open

ps:建议分发文件,执行命令,批量杀死进程,使用pssh,pscp,pnuke,速度很快的

相关文章
相关标签/搜索