一:简介
pssh是python写的可以并发在多台机器上批量执行命令的工具。
它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快。
它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。杀手锏是文件并行复制,当进行再远程主机批量上传下载的时候,最好使用它。
在使用pssh之前,必须要保证管理主机和本地主机进行过密钥的认证。
二:安装
wget https://pypi.python.org/packages/60/9a/8035af3a7d3d1617ae2c7c174efa4f154e5bf9c24b36b623413b38be8e4a/pssh-2.3.1.tar.gz
tar xf pssh-2.3.1.tar.gz -C /usr/local/pssh/
cd /usr/local/pssh/pssh-2.3.1/
python setup.py install
验证:
[root@hadoop-master pssh-2.3.1]# pssh --help
Usage: pssh [OPTIONS] command [...]
Options:
--version show program's version number and exit
--help show this help message and exit
-h HOST_FILE, --hosts=HOST_FILE
hosts file (each line "[user@]host[:port]")
-H HOST_STRING, --host=HOST_STRING
additional host entries ("[user@]host[:port]")
-l USER, --user=USER username (OPTIONAL)
-p PAR, --par=PAR max number of parallel threads (OPTIONAL)
-o OUTDIR, --outdir=OUTDIR
output directory for stdout files (OPTIONAL)
-e ERRDIR, --errdir=ERRDIR
output directory for stderr files (OPTIONAL)
-t TIMEOUT, --timeout=TIMEOUT
timeout (secs) (0 = no timeout) per host (OPTIONAL)
-O OPTION, --option=OPTION
SSH option (OPTIONAL)
-v, --verbose turn on warning and diagnostic messages (OPTIONAL)
-A, --askpass Ask for a password (OPTIONAL)
-x ARGS, --extra-args=ARGS
Extra command-line arguments, with processing for
spaces, quotes, and backslashes
-X ARG, --extra-arg=ARG
Extra command-line argument
-i, --inline inline aggregated output and error for each server
--inline-stdout inline standard output for each server
-I, --send-input read from standard input and send as input to ssh
-P, --print print output as we get it
Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime
三:pssh命令包
- pssh:在远程多台主机上并行运行命令
- pscp :把文件并行复制到多台远程主机上
- prsync:使用rsync协议本地文件同步到远程多台主机上。
- pnuke:在远程多台主机上并行killall某一进程
- pslurp:把文件从远程多台主机上复制到本地主机上
四:pssh实战
常用参数:
-h HOST_FILE 后边跟远程主机列表(ip)
-H HOST_STRING 后边跟远程主机名或者ip地址
-l USER 指定远程主机的用户名
-p PAR 指定pssh最大的并行线程数。
-o 将输出的内容重定向到一个指定的文件中
-O 指定ssh参数的具体配置
-e 将执行错误重定向到一个指定的文件中
-t 设定命令执行超时时间
-x 传递ssh命令的一些参数
-i 在远程主机上执行命令完成后显示标准输出和标准错误
-P 在执行远程命令时,输出执行结果
[root@hadoop-master pssh]# cat hosts.text
hadoop-slave1
hadoop-slave2
hadoop-slave3
pssh 远程批量执行命令
[root@hadoop-master pssh]# pssh -P -h hosts.text uptime
hadoop-slave1: 13:55:14 up 1 day, 4:03, 0 users, load average: 0.00, 0.01, 0.05
[1] 13:55:31 [SUCCESS] hadoop-slave1
[2] 13:55:33 [FAILURE] hadoop-slave2 Exited with error code 255
[3] 13:55:33 [FAILURE] hadoop-slave3 Exited with error code 255
pscp 并行传输文件到远程
【传输文件,但是不支持远程新建目录】
[root@hadoop-master pssh]# pscp -r -h hosts.text ./* /usr/local
[1] 14:10:31 [SUCCESS] hadoop-slave1
[2] 14:10:34 [FAILURE] hadoop-slave2 Exited with error code 1
[3] 14:10:34 [FAILURE] hadoop-slave3 Exited with error code 1
prsync 并行传输文件到远程
【传文件,并且支持远程新建目录】
[root@hadoop-master pssh]# pscp -r -h hosts.text hosts.text /usr/local/pssh/
[1] 14:17:18 [FAILURE] hadoop-slave1 Exited with error code 1
[2] 14:17:21 [FAILURE] hadoop-slave2 Exited with error code 1
[3] 14:17:21 [FAILURE] hadoop-slave3 Exited with error code 1
pslurp从远程拉取到本地
pslurp从远程拉取文件到本地,在本地自动创建目录名为远程主机ip的目录,将拉取的文件放在对应主机IP目录下
格式:pslurp -h ip.txt -L <本地目录> <远程目录/文件> <本地重命名>
[root@hadoop-master pssh]# pslurp -h hosts.text -L /usr/local/test /usr/local/pssh/hosts.text psshtest
[1] 14:26:49 [SUCCESS] hadoop-slave1
[2] 14:26:52 [FAILURE] hadoop-slave2 Exited with error code 1
[3] 14:26:52 [FAILURE] hadoop-slave3 Exited with error code 1
pnuke远程批量killall
[root@hadoop-master pssh]# pnuke -h hosts.text java
[1] 14:32:04 [SUCCESS] hadoop-slave1
[2] 14:32:06 [FAILURE] hadoop-slave2 Exited with error code 255
[3] 14:32:06 [FAILURE] hadoop-slave3 Exited with error code 255