一、简单介绍
1、pssh全称是parallel-ssh,基于Python编写的并发在多台服务器上批量执行命令的工具。包括pssh,pscp,prsync,pnuke和pslurp。该项目包括psshlib,可以在自定义应用程序中使用。它相当于ansible的简化版,执行起来速度比ansible快,支持文件并行复制,远程命令执行,杀掉远程主机上的进程,杀手锏是文件并行复制。
2、相关参数
-h –hosts 主机文件列表,内容格式”[user@]host[:port]”
-H –host 单台主机,内容格式”[user@]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、 创建要登录的ip文本
# vim ip.txt
129.168.40.132(pssh机) 192.168.40.220
192.168.40.155 192.168.40.211
2、免密登录脚本
# 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
另一个版本:
[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -p '' &>/dev/null # 密钥对不存在则创建密钥
while read line;do
ip=`echo $line | cut -d " " -f1` # 提取文件中的ip
user_name=`echo $line | cut -d " " -f2` # 提取文件中的用户名
pass_word=`echo $line | cut -d " " -f3` # 提取文件中的密码
expect <<EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user_name@$ip # 复制公钥到目标主机
expect {
"yes/no" { send "yes\n";exp_continue} # expect 实现自动输入密码
"password" { send "$pass_word\n"}
}
expect eof
EOF
done < /root/host_ip.txt # 读取存储ip的文件
pscp.pssh -h /root/host_ip.txt /root/your_scripts.sh /root # 推送你在目标主机进行的部署配置
pssh -h /root/host_ip.txt -i bash /root/your_scripts.sh # 进行远程配置,执行你的配置脚本
host_ip.txt文件可以通过手动写(当然了这就显得不自动化)你可以使用扫描工具扫描你网络中的主机,然后配合awk等工具生成该文件。ip地址即登录用户名密码的文件实例:
# cat host_ip.txt
172.18.14.123 root 123456
172.18.254.54 root 123456
3、互相能通信上网
三、示例
1、第一次没有登录时(没有做免密操作,需要# ssh 192.168.40.155登录,输入一次yes)
# 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、免密登录后,批量创建用户
# 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
# pssh -h ip.txt -i 'getent passwd pssher' (验证创建用户是否成功)
3、指定输出信息存放文件夹(此文件夹不需要提前创建)
# 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
# cd /data
# ls
192.168.40.132 192.168.40.155 192.168.40.211 192.168.40.220
4、关闭selinux
# 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
# cat /etc/selinux/config (查看结果,不用getenforce,刷新太慢)
5、pscp.pssh远程推送
# 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
# 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
# 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
# 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远程下载(拉取)
# 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
# ls /a
192.168.40.132 192.168.40.155 192.168.40.211 192.168.40.220
[root@localhost ~]# tree /a/
/a/
├── 192.168.40.132
│ └── messages
├── 192.168.40.155
│ └── messages
├── 192.168.40.211
│ └── messages
└── 192.168.40.220
└── messages
# pslurp -h ip.txt -L /aa -r /var/log m (-r 递归下载)
7、pnuke 并行在远程主机杀进程
# 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/目录下
# 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




所有评论(0)