一, 简单介绍
1,pssh 全称是 parallel-SSH, 基于 Python 编写的并发在多台服务器上批量执行命令的工具. 包括 pssh,pscp,prsync,pnuke 和 pslurp. 该项目包括 psshlib, 可以在自定义应用程序中使用. 它相当于 ansible 的简化版, 执行起来速度比 ansible 快, 支持文件并行复制, 远程命令执行, 杀掉远程主机上的进程, 杀手锏是文件并行复制.
2, 相关参数
-h -hosts 主机文件列表, 内容格式 "[[email protected]]host[:port]"
-H -host 单台主机, 内容格式 "[[email protected]]host[:port]"
-l -user 登录使用的用户名
-p -par 并发的线程数 [可选]
-o -outdir 输出的文件目录 [可选]
-e -errdir 错误输入文件 [可选]
-t -timeout TIMEOUT 超时时间设置, 0 无限制 [可选]
-O -option SSH 的选项
-v -verbose 详细模式
-A -askpass 手动输入密码模式
-x -extra-args 额外的命令行参数使用空白符号, 引号, 反斜线处理
-X -extra-arg 额外的命令行参数, 单个参数模式, 同 - x
-i -inline 每个服务器内部处理信息输出
-inline-stdout 每个服务器的内服输出
-P, -print 打印出服务器返回信息
3, 主要用法:
1.pssh 命令 在远程主机上执行本地命令或者脚本
2.pscp 命令 将本地文件拷贝至多个远端主机
3.pslurp 命令 从多台远程机器拷贝文件到本地
4.pnuke 命令 并行在远端主机杀进程
5.prsync 命令 使用 rsync 协议从本地计算机同步到远程主机
二, 环境
- 1,[[email protected] ~]# VIM ip.txt (创建要登录的 ip 文本)
- 129.168.40.132(pssh 机) 192.168.40.220
- 192.168.40.155 192.168.40.211
2, 免密登录脚本
- [[email protected] ~]# VIM ssh_key.sh
- #!/bin/bash
- rpm -q expect &> /dev/null || yum install expect -y
- SSH-keygen -p ""-f"/root/.ssh/id_rsa"
- password=xxxxx (四台机子密码一样)
- while read ipaddr;do
- expect <<EOF
- set timeout 10
- spawn SSH-copy-id $ipaddr
- expect {
- "yes/no" { send "yes\";exp_continue }
- "password" { send "$password\n" }
- }
- expect eof
- EOF
- done < ip.txt
3, 互相能通信上网
三, 示例
1, 第一次没有登录时 (没有做免密操作, 需要# SSH 192.168.40.155 登录, 输入一次 yes)
- [[email protected] ~]# pssh -H "192.168.40.220 192.168.40.155" -A -i hostname
- Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:04:44 [SUCCESS] 192.168.40.155
php2
[2] 09:04:44 [SUCCESS] 192.168.40.220
zjz
2, 免密登录后, 批量创建用户
[[email protected] ~]# pssh -h ip.txt -i 'useradd pssher'
[1] 09:23:06 [SUCCESS] 192.168.40.155
[2] 09:23:07 [SUCCESS] 192.168.40.211
[3] 09:23:07 [SUCCESS] 192.168.40.220
[4] 09:23:07 [SUCCESS] 192.168.40.132
[[email protected] ~]# pssh -h ip.txt -i 'getent passwd pssher' (验证创建用户是否成功)
3, 指定输出信息存放文件夹 (此文件夹不需要提前创建)
[[email protected] ~]# pssh -h ip.txt -o /data/ 'cat /etc/fstab'
[1] 09:40:56 [SUCCESS] 192.168.40.132
[2] 09:40:56 [SUCCESS] 192.168.40.220
[3] 09:40:56 [SUCCESS] 192.168.40.155
[4] 09:40:56 [SUCCESS] 192.168.40.211
- [[email protected] ~]# cd /data
- [[email protected] data]# ls
- 192.168.40.132 192.168.40.155 192.168.40.211 192.168.40.220
4, 关闭 selinux
[[email protected] ~]# pssh -h ip.txt 'sed -i"s/^SELINUX=.*/SELINUX=disabled/"/etc/selinux/config'
[1] 10:00:18 [SUCCESS] 192.168.40.220
[2] 10:00:18 [SUCCESS] 192.168.40.155
[3] 10:00:18 [SUCCESS] 192.168.40.211
[4] 10:00:18 [SUCCESS] 192.168.40.132
[[email protected] ~]# cat /etc/selinux/config (查看结果, 不用 getenforce, 刷新太慢)
5,pscp.pssh 远程推送
[[email protected] ~]# pssh -h ip.txt 'mkdir /data'
[1] 10:13:39 [SUCCESS] 192.168.40.220
[2] 10:13:39 [SUCCESS] 192.168.40.155
[3] 10:13:39 [SUCCESS] 192.168.40.211
[4] 10:13:39 [SUCCESS] 192.168.40.132
[[email protected] ~]# pscp.pssh -h ip.txt /root/zj.sh /data/
[1] 10:13:52 [SUCCESS] 192.168.40.220
[2] 10:13:52 [SUCCESS] 192.168.40.155
[3] 10:13:52 [SUCCESS] 192.168.40.211
[4] 10:13:52 [SUCCESS] 192.168.40.132
[[email protected] ~]# pssh -h ip.txt 'bash /data/zj.sh'
[1] 10:14:25 [SUCCESS] 192.168.40.220
[2] 10:14:25 [SUCCESS] 192.168.40.155
[3] 10:14:25 [SUCCESS] 192.168.40.211
[4] 10:14:25 [SUCCESS] 192.168.40.132
[[email protected] ~]# pssh -h ip.txt -i 'bash /data/zj.sh'
[1] 10:14:31 [SUCCESS] 192.168.40.220
zjz
[2] 10:14:31 [SUCCESS] 192.168.40.211
localhost.localdomain
[3] 10:14:31 [SUCCESS] 192.168.40.155
php2
[4] 10:14:31 [SUCCESS] 192.168.40.132
localhost.localdomain
6,pslurp 远程下载 (拉取)
[[email protected] ~]# pslurp -h ip.txt -L /a /var/log/messages messages (-L 指定文件夹, 最后跟上自定义名, 如 messages)
[1] 10:19:25 [SUCCESS] 192.168.40.155
[2] 10:19:25 [SUCCESS] 192.168.40.211
[3] 10:19:25 [SUCCESS] 192.168.40.220
[4] 10:19:25 [SUCCESS] 192.168.40.132
- [[email protected] ~]# ls /a
- 192.168.40.132 192.168.40.155 192.168.40.211 192.168.40.220
- [[email protected] ~]# tree /a/
- /a/
├── 192.168.40.132
│ └── messages
├── 192.168.40.155
│ └── messages
├── 192.168.40.211
│ └── messages
└── 192.168.40.220
└── messages
[[email protected] ~]# pslurp -h ip.txt -L /aa -r /var/log m (-r 递归下载)
7,pnuke 并行在远程主机杀进程
[[email protected] ~]# pnuke -h ip.txt httpd
[1] 11:18:04 [SUCCESS] 192.168.40.211
[2] 11:18:04 [SUCCESS] 192.168.40.155
[3] 11:18:04 [SUCCESS] 192.168.40.220
[4] 11:18:05 [SUCCESS] 192.168.40.132
8,prsync -r 递归将 / home 传到各主机 /tmp/pssh / 目录下
[[email protected] ~]# prsync -h ip.txt -r /home /tmp/pssh/
[1] 11:24:26 [SUCCESS] 192.168.40.132
[2] 11:24:26 [SUCCESS] 192.168.40.220
[3] 11:24:26 [SUCCESS] 192.168.40.155
[4] 11:24:26 [SUCCESS] 192.168.40.211
四, rsync 工具
-v: 显示 rsync 过程中详细信息.
-P: 显示文件传输的进度信息.
-n : 模拟复制过程.
-a : 归档模式, 表示递归传输并保持文件属性. 等同于 "-rtopgDl".
-t : 保持 mtime 属性. 建议任何时候都加上 "-t", 否则目标文件 mtime 会设置为系统时间, 导致下次更新 : 检查出 mtime 不同从而导致增量传输无效.
-o : 保持 owner 属性 (属主).
-g --group: 保持 group 属性 (属组).
-p --perms: 保持 perms 属性 (权限, 不包括特殊权限).
-D : 是 "--device --specials" 选项的组合, 即也拷贝设备文件和特殊文件.
-l --links: 如果文件是软链接文件, 则拷贝软链接本身而非软链接所指向的对象.
-z : 传输时进行压缩提高效率.
-R : 使用相对路径.
-u : 仅在源 mtime 比目标已存在文件的 mtime 新时才拷贝. 注意, 该选项是接收端判断的, 不会影响删除行为.
-d : 以不递归的方式拷贝目录本身.
-e : 指定所要使用的远程 shell 程序, 默认为 SSH.
-W :rsync 将不再使用增量传输, 而是全量传输.
rsync 做远程传输时比 scp 更高效, rsync 基于增量传输, 更快, 效率更高.
- [[email protected] ~]# rsync -av /root/zjz/* 192.168.40.211:/root/cui/
- [email protected]'s password:
- sending incremental file list
- f1 (/root/zjz / 有 f1,f2,f3, 三个文件, f1 被发送是因为只有 f1 发生改变)
- sent 133907 bytes received 189185 bytes 15027.53 bytes/sec
- total size is 2826960896 speedup is 8749.71
来源: http://www.bubuko.com/infodetail-3224389.html