检查安装环境
pssh需要python2.4或者以上版本,首先查询一下自己CentOS中的python版本,查询命令如下:
[root@hadoop-master ~]# python --version
Python 2.7.5
如果还没有安装python可以自行安装《Linux下初学python之基本安装》
安装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
pssh命令包
命令包 功能介绍
pssh 多主机并行运行命令
pscp 传输文件到多个hosts,他的特性和scp差不多
prsync 使用rsync协议从本地计算机到远程主机
pslurp 从远程主机拷贝文件到本地
pnuke kill远程机器的进程
命令具体使用
最好设置服务器之间免密登录,设置免密登录参考《CentOS7虚拟机之间设置免密登录》
首先,编辑一个hosts.txt文件记录要操作的主机地址,下面的是我主机配置的,其中hadoop-slave2和
hadoop-slave3处于关机状态
[root@hadoop-master pssh]# cat hosts.text
hadoop-slave1
hadoop-slave2
hadoop-slave3
pssh 远程批量执行命令
#-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
如果是执行长shell命令,可以类似下面这种方式
[root@hadoop-master pssh]# pssh -h hosts.text "source /etc/profile"
[1] 21:01:46 [SUCCESS] hadoop-slave3
[2] 21:01:46 [SUCCESS] hadoop-slave2
[3] 21:01:46 [SUCCESS] hadoop-slave1
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
对比
[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从远程拉取到本地
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
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
原文链接:https://blog.csdn.net/u011047968/article/details/107937572