pssh需要python2.4或者以上版本,首先查询一下自己CentOS中的python版本,查询命令如下:
[root@hadoop-master ~]# python --version
Python 2.7.5
如果还没有安装python可以自行安装《Linux下初学python之基本安装》
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 | 多主机并行运行命令 |
pscp | 传输文件到多个hosts,他的特性和scp差不多 |
prsync | 使用rsync协议从本地计算机到远程主机 |
pslurp | 从远程主机拷贝文件到本地 |
pnuke | kill远程机器的进程 |
参数说明:
参数 | 参数解释 |
---|---|
-h | 执行命令的远程主机列表,是一个文本文件,主机ip或主机名以回车符分隔。 |
-l | 远程机器的用户名 |
-p | 一次最大允许多少连接(小写) |
-o | 输出内容重定向到一个文件(小写) |
-e | 执行错误重定向到一个文件 |
-t | 设置命令执行的超时时间 |
-A | 提示输入密码并且把密码传递给ssh |
-O | 设置ssh参数的具体配置,参照ssh_config配置文件(大写) |
-x | 传递多个SSH 命令,多个命令用空格分开,用引号括起来 |
-X | 同-x 但是一次只能传递一个命令 |
-i | 显示标准输出和标准错误在每台host执行完毕后 |
-I | 读取每个输入命令,并传递给ssh进程 允许命令脚本传送到标准输入 |
-P | 打印输出(大写) |
最好设置服务器之间免密登录,设置免密登录参考《CentOS7虚拟机之间设置免密登录》
首先,编辑一个hosts.txt文件记录要操作的主机地址,下面的是我主机配置的,其中hadoop-slave2和
hadoop-slave3处于关机状态
[root@hadoop-master pssh]# cat hosts.text
hadoop-slave1
hadoop-slave2
hadoop-slave3
#-h 后面接主机ip文件,文件数据格式[user@]host[:port]
#-P 显示输出内容
具体使用
[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
如果要输入密码可以使用如下命令:
[root@hadoop-master pssh]# pssh -A -h hosts.text uptime
传输文件,但是不支持远程新建目录
[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
传文件,并且支持远程新建目录
[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
对比
[root@hadoop-master pssh]# prsync -r -h hosts.text hosts.text /usr/local/pssh/
[1] 14:18:08 [SUCCESS] hadoop-slave1
[2] 14:18:11 [FAILURE] hadoop-slave2 Exited with error code 255
[3] 14:18:11 [FAILURE] hadoop-slave3 Exited with error code 255
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
这个会根据配置的ip或者host创建文件夹目录
drwxr-xr-x. 2 root root 22 8月 11 14:26 hadoop-slave1
drwxr-xr-x. 2 root root 6 8月 11 14:24 hadoop-slave2
drwxr-xr-x. 2 root root 6 8月 11 14:24 hadoop-slave3
[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