pssh功能
-
在多服务器批量执行命令
-
文件并行复制
-
远程并行执行命令
-
杀掉远程主机进程
-
批量在远程主机行上传、下载文件
pssh的应用
一、安装
[root@web1 ~]# yum install pssh
二、创建本地主机密钥
[root@web1 ~]# mkdir ~/.ssh
[root@web1 ~]# chmod 700 ~/.ssh
[root@web1 ~]# cd ~/.ssh/
[root@web1 .ssh]# ssh-keygen -t rsa
[root@web1 .ssh]# cat id_rsa.pub > authorized_keys
[root@web1 .ssh]# chmod 600 authorized_keys
将公钥上传到远程主机
[root@web1 .ssh]# scp authorized_keys root@node1:/root/.ssh/authorized_keys
[root@web1 .ssh]# scp authorized_keys root@node2:/root/.ssh/authorized_keys
[root@node1 ~]# chmod 700 /root/.ssh/
[root@node1 ~]# chmod 600 /root/.ssh/authorized_keys
[root@web1 .ssh]# ssh node1
Last login: Sun Mar 20 21:44:27 2016 from 192.168.1.105
[root@node1 ~]# exit
三、应用实例
1、pssh指定指定远程主机执行命令(-H指定主机名或IP)
1
2
3
4
|
[root@web1 ~]
# pssh -H node1 date
[1] 04:37:09 [SUCCESS] node1
[root@web1 ~]
# pssh -H node2 date
[1] 04:37:12 [SUCCESS] node2
|
2、创建主机文件列表(在执行批量操作时用-h选项指定主机列表)
[root@web1 ~]# cat /etc/pssh/hosts
node1
node2
1
2
3
4
5
|
[root@web1 ~]
# pssh -i -h /etc/pssh/hosts "uptime"
[1] 04:40:52 [SUCCESS] node2
22:21:44 up 40 min, 1 user, load average: 0.02, 0.04, 0.00
[2] 04:40:52 [SUCCESS] node1
22:21:44 up 40 min, 1 user, load average: 0.02, 0.04, 0.00
|
3、批量执行命令
1
|
[root@web1 ~]
# pssh -i -h /etc/pssh/hosts "netstat -ntlp"
|
4、批量安装软件
[root@web1 ~]# pssh -i -h /etc/pssh/hosts "yum install pssh -y"
5、批量上传
[root@web1 ~]# pscp.pssh -h /etc/pssh/hosts keepalived-1.2.19.tar.gz /root/
6、批量解压
[root@web1 ~]# pssh -i -h /etc/pssh/hosts "tar -zxvf keepalived-1.2.19.tar.gz"
7、批量重启服务
1
2
3
4
5
6
7
|
[root@web1 ~]
# pssh -i -h /etc/pssh/hosts "/etc/init.d/mysqld restart"
[1] 03:43:01 [SUCCESS] node2
Shutting down MySQL...[ OK ]
Starting MySQL.[ OK ]
[2] 03:43:17 [SUCCESS] node1
Shutting down MySQL... SUCCESS!
Starting MySQL........... SUCCESS!
|
8、执行多条命令
1
2
3
4
5
6
7
8
9
10
11
|
[root@web1 ~]
# pssh -i -h /etc/pssh/hosts -o /etc/pssh/info "uptime;uname -a"
[1] 03:46:59 [SUCCESS] node2
22:49:31 up 22 min, 1 user, load average: 0.00, 0.01, 0.05
Linux node2 2.6.32-431.el6.x86_64
#1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[2] 03:47:01 [SUCCESS] node1
22:49:32 up 22 min, 1 user, load average: 0.02, 0.11, 0.12
Linux node1 2.6.32-431.el6.x86_64
#1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@web1 ~]
# cat /etc/pssh/info/node1
22:49:32 up 22 min, 1 user, load average: 0.02, 0.11, 0.12
Linux node1 2.6.32-431.el6.x86_64
#1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
|
9、批量 上传目录
1
2
3
|
[root@web1 ~]
# pscp.pssh -h /etc/pssh/hosts -r /root/shell /root
[1] 03:49:53 [SUCCESS] node2
[2] 03:49:53 [SUCCESS] node1
|
10、下载远程主机的文件到本机(-L表示下载的路径,后面跟远程文件并重命名)
1
2
3
|
[root@web1 ~]
# pslurp -h /etc/pssh/hosts -L /root/ /etc/hosts host.txt
[root@web1 ~]
# ls node1
host.txt
|
10、拷贝目录到本地
1
2
3
4
5
|
[root@web1 ~]
# pslurp -h /etc/pssh/hosts -r -L /root/ /home/hm mmm
[root@web1 ~]
# ls node1/
host.txt mmm
[root@web1 ~]
# ls node2/
host.txt mmm
|
11、同步文件到远程主机-l +用户
1
2
3
4
5
|
[root@web1 ~]
# prsync -h /etc/pssh/hosts -a -r /root/name.txt /tmp
[1] 04:08:44 [SUCCESS] node2
[2] 04:08:44 [SUCCESS] node1
[root@node1 ~]
# ls /tmp/
name.txt yum.log
|
12、加-z使用压缩传输
1
2
3
4
5
|
[root@web1 ~]
# prsync -h /etc/pssh/hosts -az -r /var/log /tmp
[1] 04:11:59 [SUCCESS] node2
[2] 04:11:59 [SUCCESS] node1
[root@node1 ~]
# ls /tmp/
log name.txt yum.log
|
13、批量杀死某个进程
[root@web1 ~]# pnuke -h /etc/pssh/hosts httpd