1、pssh介绍
pssh是python写的可以并发在多台机器上批量执行命令的工具,它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。杀手锏是文件并行复制,,当进行再远程主机批量上传下载的时候,最好使用它。
2、pssh的使用
在使用pssh之前,必须要保证管理主机和本地主机进行过密钥的认证,或者是在进行批量时,没有做过密钥认证,但是必须保证被管理的多台主机的密码相同。
关于如何做密钥认证,ssh无密码登录配置可以参考博客:https://blog.csdn.net/change_can/article/details/85318286
2.1 安装
方法一:yum 安装:
yum install -y pssh
方法二:编译安装
官网地址:https://code.google.com/archive/p/parallel-ssh/downloads(需要能打开谷歌)
wget http://parallel-ssh.googlecode.com/files/pssh-2.3.1.tar.gz
# 或wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/parallel-ssh/pssh-2.3.1.tar.gz
tar -zxvf pssh-2.3.1.tar.gz
cd pssh-2.3.1
python setup.py install
说明:
使用yum安装后,只能使用pssh,但是编译安装后会包括其他命令也安装了(pscp prsync pnuke pslurp)
- pssh:在远程多台主机上并行运行命令 pscp :把文件并行复制到多台远程主机上
- prsync:使用rsync协议本地文件同步到远程多台主机上。
- pnuke:在远程多台主机上并行killall某一进程
- pslurp:把文件从远程多台主机上复制到本地主机上
2.2 pssh常用参数
# pssh --help
Usage: pssh [OPTIONS] command [...]
Options:
--version show program's version number and exit
--help show this help message and exit
-h HOST_FILE, --hosts=HOST_FILE
hosts file (each line "[user@]host[:port]")
-H HOST_STRING, --host=HOST_STRING
additional host entries ("[user@]host[:port]")
-l USER, --user=USER username (OPTIONAL)
-p PAR, --par=PAR max number of parallel threads (OPTIONAL)
-o OUTDIR, --outdir=OUTDIR
output directory for stdout files (OPTIONAL)
-e ERRDIR, --errdir=ERRDIR
output directory for stderr files (OPTIONAL)
-t TIMEOUT, --timeout=TIMEOUT
timeout (secs) (0 = no timeout) per host (OPTIONAL)
-O OPTION, --option=OPTION
SSH option (OPTIONAL)
-v, --verbose turn on warning and diagnostic messages (OPTIONAL)
-A, --askpass Ask for a password (OPTIONAL)
-x ARGS, --extra-args=ARGS
Extra command-line arguments, with processing for
spaces, quotes, and backslashes
-X ARG, --extra-arg=ARG
Extra command-line argument
-i, --inline inline aggregated output and error for each server
--inline-stdout inline standard output for each server
-I, --send-input read from standard input and send as input to ssh
-P, --print print output as we get it
Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime
具体常用介绍:
-h HOST_FILE 后边跟远程主机列表(ip)
-H HOST_STRING 后边跟远程主机名或者ip地址
-l USER 指定远程主机的用户名
-p PAR 指定pssh最大的并行线程数。
-o 将输出的内容重定向到一个指定的文件中
-O 指定ssh参数的具体配置
-e 将执行错误重定向到一个指定的文件中
-t 设定命令执行超时时间
-x 传递ssh命令的一些参数
-i 在远程主机上执行命令完成后显示标准输出和标准错误
-P 在执行远程命令时,输出执行结果
2.3 用法实例:
pssh -P -h hosts.txt “cat /etc/redhat-release”
# cat hosts.txt
172.16.92.31
172.16.92.32
# pssh -P -h hosts.txt "cat /etc/redhat-release"
172.16.92.31: CentOS Linux release 7.5.1804 (Core)
[1] 11:25:30 [SUCCESS] 172.16.92.31
172.16.92.32: CentOS Linux release 7.5.1804 (Core)
[2] 11:25:30 [SUCCESS] 172.16.92.32
3、pscp拷贝文件到远程主机
pscp -h hosts.txt /root/test.txt /tmp/
[1] 11:36:21 [SUCCESS] 172.16.92.31
[2] 11:36:21 [SUCCESS] 172.16.92.32
4、pnuke杀掉某一进程
这个命令类似于 killall命令
# pnuke -h hosts.txt httpd
说明:在远程主机上批量关闭httpd服务
能通过killall关闭的服务,都可以通过pnuke来批量完成
5 、pslurp 远程主机拷贝文件到本地主机
# pslurp -h hosts.txt -L /root/ /tmp/test.txt aaaa.txt
[1] 11:43:45 [SUCCESS] 172.16.92.32
[2] 11:43:45 [SUCCESS] 172.16.92.31
说明:将所有远程主机/tmp/test.txt复制到本地主机/root/目录下,并且重新命名为aaaa.txt
-L 来指定本地文件路径
下面这个是拷贝目录
# pslurp -h hosts.txt -r -L /home/ /home/wenjian/ open
ps:建议分发文件,执行命令,批量杀死进程,使用pssh,pscp,pnuke,速度很快的
6、自动检测
ssh-copy-id.exp
#!/usr/bin/expect -f
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh-copy-id $user@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"password" { send "$password\r" }
}
expect eof
init.sh
#!/bin/bash
HOSTS=$(cat IP.txt)
if [[ "$HOSTS" == "" ]]; then
echo "IP.txt can't is empty!"
exit
fi
PASSWORD=$PASSWORD
if [ "$PASSWORD" == "" ]; then
echo "PASSWORD can't is empty!"
exit
fi
yum install expect -y #交互命令
ssh-keygen -f ~/.ssh/id_rsa -N ""
for ip in $HOSTS; do
./ssh-copy-id.exp $ip root $PASSWORD
done
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/parallel-ssh/pssh-2.3.1.tar.gz
tar -xzvf pssh-2.3.1.tar.gz
cd pssh-2.3.1
python setup.py install
# uages:
# pssh -i -H `seq -f "172.16.92.%g" 11 25` echo hi
将所有服务器ip写入文件IP.txt,一行一个ip
运行 PASSWORD='aaa' sh init.sh
(aaa注意更换成自己的密码哟, 所有机器密码须一致!)
验证免密是否成功
执行:
pssh -i -h IP.txt "hostname"
若无报错则成功