Linux下批量管理工具pssh

来源:互联网 发布:google tensorflow 编辑:程序博客网 时间:2024/05/02 00:34

pssh是一款开源的软件,使用python实现,用于批量ssh操作大批量机器;pssh是一个可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的;
比起for循环的做法,我更推荐使用pssh!使用pssh的前提是:必须在本机与其他服务器上配置好密钥认证访问(即ssh信任关系)。

下面就说下使用pssh进行批量操作的记录:

1)安装pssh
可以yum直接安装:
[root@bastion-IDC ~]# yum install -y pssh

2)pssh用法
[root@bastion-IDC ~]# pssh --help
-h 执行命令的远程主机列表文件 
-H user@ip:port 文件内容格式[user@]host[:port] 
-l 远程机器的用户名 
-p 一次最大允许多少连接 
-o 输出内容重定向到一个文件 
-e 执行错误重定向到一个文件 
-t 设置命令执行的超时时间 
-A 提示输入密码并且把密码传递给ssh(注意这个参数添加后只是提示作用,随便输入或者不输入直接回车都可以) 
-O 设置ssh参数的具体配置,参照ssh_config配置文件 
-x 传递多个SSH 命令,多个命令用空格分开,用引号括起来 
-X 同-x 但是一次只能传递一个命令 
-i 显示标准输出和标准错误在每台host执行完毕后 
-I 读取每个输入命令,并传递给ssh进程 允许命令脚本传送到标准输入

3)pssh实例说明
[root@bastion-IDC ~]# cat hosts.txt    //列表文件内的信息格式是“ip:端口”,如果本机和远程机器使用的ssh端口一致,则可以省去端口,直接用ip就行。不过建议还是将端口都带上为好。
192.168.1.101:22
192.168.1.109:22
192.168.1.118:25791
192.168.1.105:25791
如上四台机器放在一个列表文件hosts.txt内,本机已经和这四台机器做了ssh无密码登陆的信任关系
注意:列表文件内的机器必须提前和本机做好ssh信任关系,如果没有做的话,那么pssh批量执行时,轮到这台没有做信任关系的机器时就不会执行。

a)批量执行命令
[root@bastion-IDC ~]# pssh -h hosts.txt -l root -i 'uptime'
[1] 16:05:48 [SUCCESS] 192.168.1.105
03:03:25 up 79 days, 13:44, 0 users, load average: 0.04, 0.01, 0.00
[2] 16:05:48 [SUCCESS] 192.168.1.118
03:03:32 up 75 days, 15:27, 4 users, load average: 0.96, 0.74, 0.45
Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[3] 16:05:48 [SUCCESS] 192.168.1.109
03:03:25 up 61 days, 21:56, 2 users, load average: 0.02, 0.06, 0.18
Stderr: Address 192.168.1.102 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[4] 16:05:48 [SUCCESS] 192.168.1.101
16:03:17 up 35 days, 23:45, 1 user, load average: 0.03, 0.04, 0.01
Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

如果添加-A参数,那么即使提前做了ssh信任关系,还是会提示输入密码!
[root@bastion-IDC ~]# pssh -h hosts.txt -l root -i -A 'uptime'
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:                          //注意这个参数添加后只是提示作用,可以在此随便输入或者不输入直接回车都可以
[1] 16:08:25 [SUCCESS] 192.168.1.105
03:06:03 up 79 days, 13:46, 0 users, load average: 0.00, 0.00, 0.00
[2] 16:08:25 [SUCCESS] 192.168.1.109
03:06:03 up 61 days, 21:59, 2 users, load average: 0.00, 0.04, 0.15
Stderr: Address 192.168.1.102 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[3] 16:08:25 [SUCCESS] 192.168.1.101
16:05:54 up 35 days, 23:47, 1 user, load average: 0.00, 0.02, 0.00
Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[4] 16:08:25 [SUCCESS] 192.168.1.118
03:06:10 up 75 days, 15:29, 4 users, load average: 0.85, 0.78, 0.51
Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

[root@bastion-IDC ~]# pssh -h hosts.txt -l root -i -t 10 -o /root/pssh.log 'uptime && date'
[1] 17:01:02 [SUCCESS] 192.168.1.109
03:58:33 up 79 days, 5:58, 1 user, load average: 0.00, 0.00, 0.00
Wed Feb 8 03:58:33 EST 2017
[2] 17:01:02 [SUCCESS] 192.168.1.105
03:58:40 up 79 days, 14:39, 1 user, load average: 0.00, 0.00, 0.00
Wed Feb 8 03:58:40 EST 2017
[3] 17:01:02 [SUCCESS] 192.168.1.101
16:58:31 up 36 days, 40 min, 1 user, load average: 0.10, 0.03, 0.01
Wed Feb 8 16:58:31 CST 2017
Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[4] 17:01:02 [SUCCESS] 192.168.1.118
03:58:47 up 75 days, 16:22, 3 users, load average: 0.20, 0.21, 0.31
Wed Feb 8 03:58:47 EST 2017
Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[root@bastion-IDC ~]# ll /root/pssh.log/
total 16
-rw-r--r--. 1 root root 100 Feb 8 17:01 192.168.1.101
-rw-r--r--. 1 root root 99 Feb 8 17:01 192.168.1.105
-rw-r--r--. 1 root root 99 Feb 8 17:01 192.168.1.109
-rw-r--r--. 1 root root 100 Feb 8 17:01 192.168.1.118

b)批量上传文件或目录(pscp.pssh命令)
批量上传本地文件/mnt/test.file到远程服务器上的/tmp目录:
[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt /mnt/test.file /tmp/
[1] 16:18:05 [SUCCESS] 192.168.1.105
[2] 16:18:05 [SUCCESS] 192.168.1.109
[3] 16:18:05 [SUCCESS] 192.168.1.101
[4] 16:18:05 [SUCCESS] 192.168.1.118

批量上传本地文件/mnt/test.file、/mnt/aa.file、/mnt/bb.file到远程服务器上的/tmp目录:
[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt /mnt/test.file /mnt/aa.file /mnt/bb.file /tmp/
[1] 16:22:50 [SUCCESS] 192.168.1.109
[2] 16:22:50 [SUCCESS] 192.168.1.105
[3] 16:22:50 [SUCCESS] 192.168.1.118
[4] 16:22:50 [SUCCESS] 192.168.1.101
或者:
[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt /mnt/{test.file,aa.file,bb.file} /tmp/
[1] 16:23:44 [SUCCESS] 192.168.1.109
[2] 16:23:44 [SUCCESS] 192.168.1.105
[3] 16:23:44 [SUCCESS] 192.168.1.101
[4] 16:23:44 [SUCCESS] 192.168.1.118

批量上传本地目录/mnt/zhong到远程服务器上的/tmp目录(上传目录需要添加-r参数)
[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt -r /mnt/zhong /tmp/
[1] 16:19:36 [SUCCESS] 192.168.1.109
[2] 16:19:36 [SUCCESS] 192.168.1.105
[3] 16:19:36 [SUCCESS] 192.168.1.101
[4] 16:19:36 [SUCCESS] 192.168.1.118

批量上传本地目录/mnt/zhong、/mnt/aa、/mnt/vv到远程服务器上的/tmp目录
[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt -r /mnt/zhong /mnt/aa /mnt/vv /tmp/
[1] 16:21:02 [SUCCESS] 192.168.1.105
[2] 16:21:02 [SUCCESS] 192.168.1.109
[3] 16:21:02 [SUCCESS] 192.168.1.101
[4] 16:21:02 [SUCCESS] 192.168.1.118
或者:
[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt -r /mnt/{zhong,aa,vv} /tmp/
[1] 16:22:00 [SUCCESS] 192.168.1.109
[2] 16:22:00 [SUCCESS] 192.168.1.105
[3] 16:22:00 [SUCCESS] 192.168.1.101
[4] 16:22:00 [SUCCESS] 192.168.1.118

c)批量下载文件或目录(pslurp命令)
批量下载服务器上的某文件到本地,不用担心重名问题,因为pssh已经建立了以文件列表内的ip为名称的目录来存放下载的文件
[root@bastion-IDC ~]# pslurp -l root -h hosts.txt /etc/hosts .
[1] 16:32:01 [SUCCESS] 192.168.1.109
[2] 16:32:01 [SUCCESS] 192.168.1.105
[3] 16:32:01 [SUCCESS] 192.168.1.101
[4] 16:32:01 [SUCCESS] 192.168.1.118
[root@bastion-IDC ~]# ll
total 123
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.101
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.105
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.109
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.118
[root@bastion-IDC ~]# ll 192.168.1.101
total 4
-rw-r--r--. 1 root root 224 Feb 8 16:32 hosts
[root@bastion-IDC ~]# ll 192.168.1.109
total 4
-rw-r--r--. 1 root root 252 Feb 8 16:32 hosts
[root@bastion-IDC ~]# ll 192.168.1.105
total 4
-rw-r--r--. 1 root root 252 Feb 8 16:32 hosts
[root@bastion-IDC ~]# ll 192.168.1.118
total 4
-rw-r--r--. 1 root root 212 Feb 8 16:32 hosts

另外特别注意:
上面的批量下载操作,只能下载到本地的当前目录下,不能在命令中跟指定的路径:
[root@bastion-IDC ~]# pslurp -l root -h hosts.txt /etc/hosts /mnt/
[1] 16:34:14 [FAILURE] 192.168.1.109 Exited with error code 1
[2] 16:34:14 [FAILURE] 192.168.1.105 Exited with error code 1
[3] 16:34:14 [FAILURE] 192.168.1.101 Exited with error code 1
[4] 16:34:14 [FAILURE] 192.168.1.118 Exited with error code 1

要想下载到本机的/mnt目录下,正确的做法是先切换到/mnt目录下,然后再执行下载命令:(列表文件要跟全路径)
[root@bastion-IDC ~]# cd /mnt/
[root@bastion-IDC mnt]# pslurp -l root -h /root/hosts.txt /etc/hosts ./
[1] 16:34:34 [SUCCESS] 192.168.1.109
[2] 16:34:34 [SUCCESS] 192.168.1.105
[3] 16:34:34 [SUCCESS] 192.168.1.118
[4] 16:34:34 [SUCCESS] 192.168.1.101
[root@bastion-IDC mnt]# ll
total 16
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.101
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.105
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.109
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.118

上面是批量下载文件,要是批量下载目录,只需要添加一个-r参数即可!
[root@bastion-IDC mnt]# pslurp -l root -h /root/hosts.txt -r /home/ ./
[1] 16:39:05 [SUCCESS] 192.168.1.109
[2] 16:39:05 [SUCCESS] 192.168.1.105
[3] 16:39:05 [SUCCESS] 192.168.1.101
[4] 16:39:05 [SUCCESS] 192.168.1.118

[root@bastion-IDC mnt]# ll 192.168.1.101
total 8
drwxr-xr-x. 6 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 224 Feb 8 16:38 hosts
[root@bastion-IDC mnt]# ll 192.168.1.*
192.168.1.101:
total 8
drwxr-xr-x. 6 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 224 Feb 8 16:38 hosts

192.168.1.105:
total 8
drwxr-xr-x. 4 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 252 Feb 8 16:38 hosts

192.168.1.109:
total 8
drwxr-xr-x. 4 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 252 Feb 8 16:38 hosts

192.168.1.118:
total 8
drwxr-xr-x. 3 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 212 Feb 8 16:38 hosts

d)批量同步(prsync命令)
同步本机/mnt/test目录下的文件或目录到远程机器的/mnt/test路径下
[root@bastion-IDC ~]# prsync -l root -h hosts.txt -r /mnt/test/ /mnt/test/
[1] 16:46:41 [SUCCESS] 192.168.1.109
[2] 16:46:41 [SUCCESS] 192.168.1.105
[3] 16:46:41 [SUCCESS] 192.168.1.118
[4] 16:46:41 [SUCCESS] 192.168.1.101

同步本机/mnt/test目录下的文件或目录到远程机器的/mnt路径下
[root@bastion-IDC ~]# prsync -l root -h hosts.txt -r /mnt/test/ /mnt/
[1] 16:47:40 [SUCCESS] 192.168.1.109
[2] 16:47:40 [SUCCESS] 192.168.1.105
[3] 16:47:45 [SUCCESS] 192.168.1.101
[4] 16:47:46 [SUCCESS] 192.168.1.118

注意:
上面批量同步目录操作是将本机对应目录数据同步到远程机器上,远程机器上对于目录下多余的文件也会保留(不会删除多余文件)

同理,批量同步文件操作,去掉-r参数,
注意:同步文件的时候,其实就是完全覆盖,远程机器对应文件内的文件会被全部替换!
如下:
同步本机的/mnt/test/file文件内容到远程服务器/mnt/test/file文件内
[root@bastion-IDC ~]# prsync -l root -h hosts.txt /mnt/test/file /mnt/test/file 
[1] 16:53:54 [SUCCESS] 192.168.1.109
[2] 16:53:54 [SUCCESS] 192.168.1.105
[3] 16:53:54 [SUCCESS] 192.168.1.101
[4] 16:53:54 [SUCCESS] 192.168.1.118
[root@bastion-IDC ~]# prsync -l root -h hosts.txt /mnt/test/file /mnt/aaa
[1] 16:54:03 [SUCCESS] 192.168.1.109
[2] 16:54:03 [SUCCESS] 192.168.1.105
[3] 16:54:03 [SUCCESS] 192.168.1.101
[4] 16:54:04 [SUCCESS] 192.168.1.118

e)批量kill远程机器上的进程(pnuke命令)
比如批量kill掉远程机器上的nginx进程
[root@bastion-IDC ~]# pnuke -h hosts.txt -l root nginx
[1] 17:09:14 [SUCCESS] 192.168.1.109 
[2] 17:09:14 [SUCCESS] 192.168.1.105 
[3] 17:09:15 [SUCCESS] 192.168.1.118 
[4] 17:09:15 [SUCCESS] 192.168.1.101


1 简介

PSSH是一个可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的。使用是必须在各个服务器上配置好密钥认证访问。


pssh 包安装 5 个实用程序:


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


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


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


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


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



2 安装(系统:Centos7.2)


[root@57135test pssh-1.4.3]# wget http://www.theether.org/pssh/pssh-1.4.3.tar.gz

[root@57135test pssh-1.4.3]# tar -xzf pssh-1.4.3.tar.gz

[root@57135test pssh-1.4.3]# cd pssh-1.4.3 

[root@57135test pssh-1.4.3]# python setup.py install

 报错[如未报错,可跳过]:

  [root@57135test pssh-1.4.3]# python setup.py install

  Traceback (most recent call last):

    File "setup.py", line 1, in <module>

      from setuptools import setup, find_packages

  ImportError: No module named setuptools

 解决:

  [root@57135test pssh-1.4.3]# wget 

  [root@57135test setuptools-0.6c11]# tar xzf setuptools-0.6c11.tar.gz 

  [root@57135test setuptools-0.6c11]# cd setuptools-0.6c11

  [root@57135test setuptools-0.6c11]# python setup.py build

  [root@57135test setuptools-0.6c11]# python setup.py install


[root@57135test pssh-1.4.3]# python setup.py build

[root@57135test pssh-1.4.3]# python setup.py install

3 建立ssh密钥登陆,脚本批量创建


[root@57135test pssh-1.4.3]# ssh-keygen -t rsa

创建rsa.sh脚本 test.txt中写入要管理的IP

[root@192 sh]# more rsa.sh 

#!/bin/sh

#by authors chy 2016

for i in $(cat test.txt)

do

        ssh-copy-id -i /root/.ssh/id_rsa.pub $i

        echo $i"设置密码登录成功"

done

注:可以将IP写在test.txt文件中

4 使用


a 查看主机名

[root@57135test .ssh]# pssh -h test.txt hostname   

[1] 11:58:23 [SUCCESS] 192.168.57.131 22

[2] 11:58:23 [SUCCESS] 192.168.57.132 22


b 查看查看启动时间

[root@57135test .ssh]# pssh -h test.txt -l root -P uptime  

192.168.57.131:  09:47:19 up  2:26,  2 users,  load average: 0.02, 0.02, 0.05

192.168.57.131: [1] 12:00:04 [SUCCESS] 192.168.57.131 22

192.168.57.132:  10:04:48 up  2:40,  2 users,  load average: 0.00, 0.01, 0.05

192.168.57.132: [2] 12:00:04 [SUCCESS] 192.168.57.132 22


c 删除文件

[root@57135test .ssh]# pssh -h test.txt -P "rm -rf /root/test.sh"


d 传输文件,可传输脚本

[root@57135test .ssh]# pscp -h test.txt  rsa.sh  /root


e 关闭selinux

[root@57135test .ssh]# pssh -h test.txt -l root -P "sed -i 's/SELINUX=enforcing/SELINUX=disables/g' /etc/sysconfig/selinux"


f 重启虚拟机

[root@57135test .ssh]# pssh -h test.txt -l root -P reboot

5 用法讲解


      -h 执行命令的远程主机列表  或者 -H user@ip:port  文件内容格式[user@]host[:port]

      -l 远程机器的用户名

      -P 执行时输出执行信息

      -p 一次最大允许多少连接

      -o 输出内容重定向到一个文件

      -e 执行错误重定向到一个文件

      -t 设置命令执行的超时时间

      -A 提示输入密码并且把密码传递给ssh

      -O 设置ssh参数的具体配置,参照ssh_config配置文件

      -x 传递多个SSH 命令,多个命令用空格分开,用引号括起来

      -X 同-x 但是一次只能传递一个命令

      -i 显示标准输出和标准错误在每台host执行完毕后

      '''其他命令'''


       pscp     传输文件到多个hosts,类似scp


       pslurp   从多台远程机器拷贝文件到本地


       pnuke    并行在远程主机杀进程


       prsync   使用rsync协议从本地计算机同步到远程主机


6 注意


a 使用前提

中心主机连接远程主机可以通过ssh密钥无密码连接

b 命令格式

pssh 总是通过清单 文件指定主机

其中的每行采用 [user] host[:port] 形式。


c 创建servers.txt文件


文件的内容为远程主机的ip,和用于连接ssh的用户名和端口


# vim /home/server.txt

192.168.0.177

root@183.62.138.82:22031


pssh用法-在多个主机上并行地运行命令


pssh -P -h /home/server.txt hostname






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)


[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


[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、批量重启服务


[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、执行多条命令


[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、批量 上传目录


[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表示下载的路径,后面跟远程文件并重命名


[root@web1 ~]# pslurp -h /etc/pssh/hosts -L /root/  /etc/hosts host.txt

[root@web1 ~]# ls node1

host.txt



10、拷贝目录到本地


[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 +用户


[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使用压缩传输


[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



管理上千服务器而且要并发执行要么字写工具用开源的也不错,

这类工具比如 pdsh,mussh,cssh,dsh等还有这里提到的pssh:

1  安装:

#wget http://peak.telecommunity.com/dist/ez_setup.py

python ez_setup.py

#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


pssh   多主机并行运行命令

pscp   传输文件到多个hosts,他的特性和scp差不多

pslurp   从多台远程机器拷贝文件

pnuke    kill远程机器的进程

pslurp   从远程主机考本文件到本地

prsync   使用rsync协议从本地计算机同步到远程主机

 

pssh:

-h 执行命令的远程主机列表  或者 -H user@ip:port  文件内容格式[user@]host[:port]

-l 远程机器的用户名

-p 一次最大允许多少连接

-o 输出内容重定向到一个文件

-e 执行错误重定向到一个文件

-t 设置命令执行的超时时间

-A 提示输入密码并且把密码传递给ssh

-O 设置ssh参数的具体配置,参照ssh_config配置文件

-x 传递多个SSH 命令,多个命令用空格分开,用引号括起来

-X 同-x 但是一次只能传递一个命令

-i 显示标准输出和标准错误在每台host执行完毕后

-I 读取每个输入命令,并传递给ssh进程 允许命令脚本传送到标准输入

-P 打印输出



2  pssh使用 (假设ssh已做好SSH信任,ssh信任请参看:关于ssh命令研究以及SSH信任详解)

pssh工具包主要有5个程序:

1 pssh  多主机并行运行命令

[root@server pssh-2.2.2]# pssh -P -h test.txt uptime

192.168.9.102:  14:04:58 up 26 days, 17:05,  0 users,  load average: 0.07, 0.02, 0.00

192.168.9.102: [1] 14:04:58 [SUCCESS] 192.168.9.102 9922

192.168.8.171:  14:04:59 up 35 days,  2:01,  6 users,  load average: 0.00, 0.00, 0.00

192.168.8.171: [2] 14:04:59 [SUCCESS] 192.168.8.171 22

192.168.9.104:  14:04:59 up 7 days, 20:59,  0 users,  load average: 0.10, 0.04, 0.01

192.168.9.104: [3] 14:04:59 [SUCCESS] 192.168.9.104 9922

[root@server pssh-2.2.2]# cat test.txt

192.168.9.102:9922

192.168.9.104:9922

192.168.8.171:22   //注意我的端口号不仅是默认的22


假如想将输出重定向到一个文件 加-o file 选项

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


注意 是从服务器端给客户端传送文件:


[root@server pssh-2.2.2]# pscp -h test.txt  /etc/sysconfig/network  /tmp/network   //标示将本地的/etc/sysconfig/network传到目标服务器的/tmp/network


3 prsync 使用rsync协议从本地计算机同步到远程主机

[root@server ~]# pssh -h test.txt -P mkdir  /tmp/etc


[root@server ~]# prsync -h test.txt -l dongwm -a -r  /etc/sysconfig   /tmp/etc 

#标示将本地的/etc/sysconfig目录递归同步到目标服务器的 /tmp/etc目录下,并保持原来的时间戳,使用用户 dongwm


4  pslurp 将文件从远程主机复制到本地,和pscp方向相反:

[root@server ~]# pslurp -h test.txt   -L /tmp/test -l root /tmp/network test  


#标示将目标服务器的/tmp/network文件复制到本地的/tmp/test目录下,并更名为test


[1] 14:53:54 [SUCCESS] 192.168.9.102 9922

[2] 14:53:54 [SUCCESS] 192.168.9.104 9922


[root@server ~]# ll /tmp/test/192.168.9.10

192.168.9.102/ 192.168.9.104/

[root@server ~]# ll /tmp/test/192.168.9.102/

总计 4.0K

-rw-r--r-- 1 root root 60 2011-04-22 14:53 test

[root@server ~]# ll /tmp/test/192.168.9.104/

总计 4.0K

-rw-r--r-- 1 root root 60 2011-04-22 14:53 test

5  pnuke 并行在远程主机杀进程:

[root@server ~]# pnuke -h test.txt   syslog //杀死目标服务器的syslog进程,只要ps进程中出现相关词语 都能杀死

[1] 15:05:14 [SUCCESS] 192.168.9.102 9922

[2] 15:05:14 [SUCCESS] 192.168.9.104 9922



(三)  pssh应用实例   (和pssh这个姑娘相处过程,好好学,好好相处)

$ pssh   -H user02@192.168.100.100  -P date     #-H 后面跟主机名或者IP, -P 是在执行命令时,输出命令结果


$ pssh -H  user02@192.168.100.100 -i   date     #-H 后面跟主机名或者IP,-i 也是显示命令结果。


$ pssh -i -O "StrictHostKeyChecking=no"  -h  /etc/pssh/hosts   "uptime"  #-O 后面跟SSH的具体配置参数,


“StrictHostKeyChecking=no”是sshd_config里面的一个配置选项,可以让远程主机自动接 受本地主机的hostkey,而不用每次都输入yes.  


-h  是跟一个服务器列表,/etc/pssh/hosts这个文件是我自己建的,在里面写上所有要远程的主机名或者IP。比如:


#more /etc/pssh/hosts

user02@192.168.100.100


$  pssh -i -h /etc/pssh/hosts "sudo "tar -zxvf /root/hadoop-2.0.0.tar.gz ""       #这个命令用sudo了,解压hadoop-2.0.0.tar.gz这个压缩包。            类似的命令:


$  pssh -i -h /etc/pssh/hosts   "sudo "yum install pssh -y""


$ pssh -i -h /etc/pssh/hosts "sudo "/etc/init.d/httpd restart""         一个pssh的综合英语实例:


$  pssh -i -x "-l user02"   "-p 9529"   -h   /etc/pssh/hosts    -o /etc/pssh/info "uptime;ls"


#上面的-x参数,分别调用了“-l" "-p",用户指定在远程主机上登录的用户名和SSH端口,而"-o" 参数将指定结果存放到/etc/pssh/info目录中。在这个实例的最后,指定了2个命令,它们之间用分号隔开


    

(四)  认识pscp  与  pslurp  



pscp  的作用是将本地文件并行复制到远程多台主机上,而pslurp是把文件从远程多台主机复制到本地主机!在运维工作中,要进行文件批量传送时,pssh的姐姐和pssh的哥哥非常管用。


$  pscp -h  /etc/pssh/hosts  /etc/ssh/sshd_config   /tmp    # 这个命令把本地的sshd_config 复制到了远程主机的/tmp目录下,远程主机的名字在/etc/pssh/hosts里面记录着。



$  pscp -h /etc/pssh/hosts/   -r /etc/httpd/conf   /tmp     # -r的目的是递归复制,把目录中的文件全部复制过去。



$  pslurp  -h /etc/pssh/hosts   -L  /home/user01/bendi     /home/user02/gmond.conf 


gmond1.conf           #-L  是指定本地的主机路径,这句话的意思是把远程主机上的gmond.conf复制到本地的/home/user01/bendi目录下,并且改名为 gmond1.conf.



$  pslurp  -h /etc/pssh/hosts   -r    -L  /home/user01/bendi     /home/user02/gmond.conf 


gmond1.conf     #-r   是递归复制,作用和上面命令的作用基本相同,-r 和-L 顺序不能颠倒


(五)  认识prsync  和pnuke  (pssh的姑姑和pssh的叔叔)


            pssh的姑姑---prsync,可以通过rsync协议将文件或者目录从本地主机远程同步到多个主机


            pssh的叔叔---pnuke ,可以在远程主机上并行杀掉某一进程,相当于killall,它的叔叔比较狠!


$  prsync  -h  /etc/pssh/hosts   -l  user02  -a   -r  /etc/httpd   /tmp     //-l   是指定远程主机上的用


户,-r的递归复制,-a  可以保持复制过去的文件属性不变,比如创建时间,修改时间,读取权限等


$  prsync  -h  /etc/pssh/hosts   -l  user02  -az  -r  /etc/httpd   /tmp     //-az  ,-a是保持文件属性不


变,-z是一个压缩传输参数。在低宽带环境下,或者对网络宽带有要求,传输文件压缩比大时可以使


用。但是在带宽充足或者传输的文件比较大,不推荐使用。


$  pnuke -h /etc/pssh/hosts   httpd

    这个命令将在远程主机上并行关闭httpd,类似与killall,后面跟的是httpd是服务名,只要通过killall命令能关闭的服务,都可以通过pnuke来批量完成


   就是这些,说完了。  

附:

     -h      后面跟 远程主机列表文件,内容格式为[user@]host[:port]

     -H      后面跟一个远程主机名或IP地址。

     -l        后面跟远程主机上的用户名

     -p      指定pssh最大并行线程数,比如:pssh -p 10

     -o      指定输入的内容重定向到一个制定的文件中

     -O     指定SSH参数 具体配置

     -e      将执行错误冲定向到指定一个文件中

     -t       设置命令执行的超时时间

     -A      提示输入密码,并 把密码传递给SSH服务

     -x      用于传递ssh命令的一些参数,每个参数用引号引起来,例如: pssh -x "-l user02" "-p 22"

     -i       命令完成后显示标准输出和标准错误

    -P       输出执行结果


0 0
原创粉丝点击