阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux下批量管理工具pssh使用方法

100次阅读
没有评论

共计 8554 个字符,预计需要花费 22 分钟才能阅读完成。

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

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

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

2)pssh 用法
[root@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# ll 192.168.1.101
total 4
-rw-r–r–. 1 root root 224 Feb 8 16:32 hosts
[root@linuxidc ~]# ll 192.168.1.109
total 4
-rw-r–r–. 1 root root 252 Feb 8 16:32 hosts
[root@linuxidc ~]# ll 192.168.1.105
total 4
-rw-r–r–. 1 root root 252 Feb 8 16:32 hosts
[root@linuxidc ~]# ll 192.168.1.118
total 4
-rw-r–r–. 1 root root 212 Feb 8 16:32 hosts

另外特别注意:
上面的批量下载操作,只能下载到本地的当前目录下,不能在命令中跟指定的路径:
[root@linuxidc ~]# 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@linuxidc ~]# cd /mnt/
[root@linuxidc 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@linuxidc 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@linuxidc 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@linuxidc 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@linuxidc 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140838.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计8554字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中