之前远程查看或传输文件都用ssh或rsync后来听人介绍了pssh,从网上查到它是并行传输的,因此速度更快,使用python写的。html
安装完后会有5个命令python
用途linux
pssh 在多个主机上并行地运行命令。
pscp 把文件并行地复制到多个主机上。
prsync 经过 rsync 协议把文件高效地并行复制到多个主机上。
pslurp 把文件并行地从多个远程主机复制到中心主机上。
pnuke 并行地在多个远程主机上杀死进程。git
#yum -y install pythonvim
#-t 时间,默认60秒服务器
下面内容来自网络网络
2.配置密钥,免密码访问管理机器
在使用以前须要配置密钥访问,以下:
#ssh-keygen #一直回车
#复制公钥到远端服务器
#ssh-copy-id -i .ssh/id_rsa.pub root@192.168.16.X
ps.若是端口不是默认22 ,须要使用:ssh-copy-id -i .ssh/id_rsa.pub ”-p 4567 yin@192.168.16.X”app
3:运行第一条命令,查看启动时间ssh
]# pssh -h servers.txt -l root -P uptime
173.16.0.102: 13:14:57 up 1 day, 2:00, 1 user, load average: 0.00, 0.00, 0.00
173.16.0.100: 13:23:34 up 21:40, 1 user, load average: 0.00, 0.00, 0.00
[1] 13:13:52 [SUCCESS] 173.16.0.102
[2] 13:13:52 [SUCCESS] 173.16.0.100ide
4:删除文件
我须要删除之前yum.repo 文件
pssh -h servers.txt -P "rm -r /etc/yum.repos.d/*.repo"
5:传输单个文件
pscp -h servers.txt /etc/yum.repos.d/ftp.repo /etc/yum.repos.d/
6:运行yum update
]# pssh -h servers.txt -l root -P "yum update"
173.16.0.100: Loaded plugins: fastestmirror
173.16.0.102: Loaded plugins: fastestmirror
173.16.0.100: Loading mirror speeds from cached hostfile
173.16.0.102: Loading mirror speeds from cached hostfile
173.16.0.100: Setting up Update Process
173.16.0.102: Setting up Update Process
173.16.0.100: No Packages marked for Update
173.16.0.102: No Packages marked for Update
[1] 13:38:13 [SUCCESS] 173.16.0.100
[2] 13:38:13 [SUCCESS] 173.16.0.102
7:让机器加载Ext4
]# pssh -h servers.txt -l root -P "modprobe ext4"
[1] 13:49:42 [SUCCESS] 173.16.0.100
[2] 13:49:42 [SUCCESS] 173.16.0.102
8:安装e4fsprogs, 支持ext4分区
[root@gluster02b ~]# pssh -h servers.txt -l root -P "yum -y install e4fsprogs"
173.16.0.102: Loaded plugins: fastestmirror
173.16.0.100: Loaded plugins: fastestmirror
173.16.0.100: Complete!
[1] 13:53:11 [SUCCESS] 173.16.0.100
173.16.0.102:
Installed:
e4fsprogs.x86_64 0:1.41.12-2.el5
173.16.0.102: Complete!
[2] 13:53:11 [SUCCESS] 173.16.0.102
9:建立一个目录
# pssh -h servers.txt -l root -P "mkdir /data01"
[1] 09:53:27 [SUCCESS] 173.16.0.100
[2] 09:53:27 [SUCCESS] 173.16.0.102
# pscp -h servers.txt /etc/fstab /etc/
[1] 09:53:51 [SUCCESS] 173.16.0.102
[2] 09:53:51 [SUCCESS] 173.16.0.100
10:复制文件夹到远程
# pscp -h servers.txt -r /root/tools/ /root/
[1] 10:23:27 [SUCCESS] 173.16.0.100
[2] 10:23:28 [SUCCESS] 173.16.0.102
11:关闭Selinux
# pssh -h servers.txt -l root -P "sed -i '/SELINUX=enforcing/s/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux"
[1] 11:47:15 [SUCCESS] 173.16.0.100
[2] 11:47:15 [SUCCESS] 173.16.0.102
# pssh -h servers.txt -l root -P init 6
[1] 11:47:28 [SUCCESS] 173.16.0.102
[2] 11:47:28 [SUCCESS] 173.16.0.100
# pssh -h servers.txt -l root -P sestatus
[1] 11:49:37 [FAILURE] 173.16.0.100 Exited with error code 255
[2] 11:49:37 [FAILURE] 173.16.0.102 Exited with error code 255
# pssh -h servers.txt -l root -P sestatus
173.16.0.100: SELinux status: disabled
[1] 11:52:32 [SUCCESS] 173.16.0.100
173.16.0.102: SELinux status: disabled
[2] 11:52:32 [SUCCESS] 173.16.0.102
http://sheriy.com/?p=748
http://www.theether.org/pssh/docs/0.2.3/pssh-HOWTO.html
== 简介 ==
pssh是一个python编写能够在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的,相似pdsh,我的认为相对pdsh更为简便,使用必须在各个服务器上配置好密钥认证访问。
项目地址:https://code.google.com/p/parallel-ssh/
PSSH provides parallel versions of OpenSSH and related tools. Included are pssh, pscp, prsync, pnuke, and pslurp. The project includes psshlib which can be used within custom applications. The source code is written in Python and can be cloned from:
git clone http://code.google.com/p/parallel-ssh/
PSSH is supported on Python 2.4 and greater (including Python 3.1 and greater). It was originally written and maintained by Brent N. Chun. Due to his busy schedule, Brent handed over maintenance to Andrew McNabb in October 2009.
== 下载安装 ==
下载
wget http://parallel-ssh.googlecode.com/files/pssh-2.3.1.tar.gz
tar xf pssh-2.3.1.tar.gz
cd pssh-2.3.1/
python setup.py install
-l 远程机器的用户名
-P 执行时输出执行信息
-p 一次最大容许多少链接pscp 传输文件到多个hosts,相似scp
pslurp 从多台远程机器拷贝文件到本地
pnuke 并行在远程主机杀进程
prsync 使用rsync协议从本地计算机同步到远程主机
== 实例 ==$ pssh -h ip.txt -l root chkconfig --level 2345 snmpd on
[1] 10:59:29 [SUCCESS] ... ...
[2] 10:59:29 [SUCCESS] ... ...
[3] 10:59:29 [SUCCESS] ... ...
... ...
$ pscp -h ip.txt -l root /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf
[1] 11:00:42 [SUCCESS] ... ...
[2] 11:00:42 [SUCCESS] ... ...
[3] 11:00:42 [SUCCESS] ... ...
... ...
参考:http://my.oschina.net/guol/blog/59977
(一) 使用前提
中心主机链接远程主机能够经过ssh密钥无密码链接
(二) 命令格式
pssh 老是经过清单 文件指定主机
其中的每行采用 [user] host[:port] 形式。
(三) 建立servers.txt文件
文件的内容为远程主机的ip,和用于链接ssh的用户名和端口
# vim /home/server.txt
192.168.0.177
(四) pssh用法-在多个主机上并行地运行命令
# pssh -P -h /home/server.txt hostname
在默认状况下,每一个命令实例的输出出如今 stdout 中。输出划分为每一个主机一段。可是,能够指定一个目录来捕捉每一个实例的输出。例如,若是运行前面的命令并添加 --outdir=/opt/output/,那么会把每一个主机的命令输出捕捉到/opt/output/ 中单独的文件中
# pssh -P -h /home/server.txt --outdir=/opt/output/ hostname
# ll /opt/output/
# cat /opt/output/192.168.0.177
Standby
pssh能够生成最多 32 个进程,并行地链接各个节点。若是远程命令在 60 秒内没有完成,链接会终止。若是命令须要更多处理时间,可使用 -t 设置更长的到期时间。(parallel-scp 和 parallel-rsync 没有默认的到期时间,可是能够用 -t 指定到期时间。)
(五) pscp用法-把文件或者目录并行地复制到多个主机上
# pscp -h /home/server.txt /home/server.txt /opt /qubaoquan
其中/home/server.txt 为本地目录 /opt/qubaoquan为文件名,即server.txt复制到远端后重名为qubaoquan,若是不指定远端文件名只有目录则保持原文件名
也能够复制整个目录到远端
# ll /srv/test
# pscp --recursive -h /home/server.txt /srv/test /opt
(六) pslurp用法-把文件或者目录并行地从多个远程主机复制到中心主机上
它从每台远程计算机收集指定的文件,可是并不覆盖文件的本地版本。pslurp 为每台远程计算机建立一个子目录并把指定的文件复制到此位置
# # pslurp --recursive -h /home/server.txt /etc/passwd /qubaoquan
/etc/passwd 为远程文件 /qubaoquan为本地目录,也能够为拷贝到本地后的文件名.
# tree /qubaoquan/
# pslurp --recursive -h /home/server.txt -L /srv/test/ /srv llll
其中 --recursive表示递归子目录,-L 选项指定建立子目录的位置,/srv为远程目录,lll为拷贝到本地后的目录名,看目录结构就知道了
#tree /srv/test/
(七) pnuke用法-并行地在多个远程主机上杀死进程
pnuke 至关于运行 ssh host killall。pnuke 的参数是一个模式。在远程计算机上运行的名称符合这个模式的全部进程都被杀死。能够用此命令方便地在一组服务器上中止同一守护进程。
# pnuke -h /home/server.txt --user=root cron
命令的结果则是在server.txt文件中指定的主机上执行killall cron命令 其中cron为匹配到cron的进程,只要ps进程中出现相关词语 都能杀死
192.168.12.4
vi host_list.txt
magic@shopex ~]$ pscp -h host_list.txt iptables /home/magic/.
[magic@shopex ~]$ pscp -h host_list.txt squid.conf /home/magic/.
[magic@shopex ~]$ pscp -h host_list.txt zabbix_agentd.conf /home/magic/.
[magic@shopex ~]$ pscp -h host_list.txt replace.sh /home/magic/.
[magic@shopex ~]$ pssh -x "-t -t" -h host_list.txt -P "/home/magic/squid.sh"
pscp -h host_list.txt crontab /home/magic/.
pscp -h host_list.txt crontab /root/.
pssh -h host_list.txt -P "crontab /root/crontab"
pssh -x "-t -t" -h host_list.txt -P "sudo crontab /home/magic/crontab"
pssh -x "-t -t" -h host_list.txt_bk -P "cp /home/magic/squid.conf /usr/local/squid-3.1.16/etc/."
pssh -x "-t -t" -h host_list.txt_bk -P "sudo /usr/local/squid-3.1.16/sbin/squid -k reconfigure"
pssh -x "-t -t" -h host_list.txt -P "cp /root/squid.conf /usr/local/squid-3.1.16/etc/."
pssh -x "-t -t" -h host_list.txt -P "/usr/local/squid-3.1.16/sbin/squid -k reconfigure"