天天看点

通过pssh同时管理多台服务器,批量管理服务器

运维人员往往会头疼,要在10多台服务器上去执行同样的操作。

在linux下有一款这样的工具(pssh),解决了这个问题。

这款工具的几个主要功能:

pssh 在多个主机上并行地运行命令。

pscp 把文件并行地复制到多个主机上。

prsync 通过 rsync 协议把文件高效地并行复制到多个主机上。

pslurp 把文件并行地从多个远程主机复制到中心主机上。

pnuke 并行地在多个远程主机上杀死进程。

--

1:安装

wget http://parallel-ssh.googlecode.com/files/pssh-2.2.2.tar.gz

tar zxvf pssh-2.2.2.tar.gz

cd pssh-2.2.2

python setup.py install

---

2.配置密钥,免密码访问管理机器

在使用之前需要配置密钥访问,如下:

#ssh-keygen          【回车】

#复制公钥到远端服务器

#ssh-copy-id -i .ssh/id_rsa.pub [email protected]

ps.如果端口不是默认22 ,需要使用:ssh-copy-id -i .ssh/id_rsa.pub  ”-p 4567 [email protected]

编辑 服务器列表文件 servers.txt  

# vim /home/server.txt

[email protected]:22031

hosts file (each line "[user@]host[:port]")

3:运行第一条命令,查看启动时间

]# 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.100

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.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

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