使用pssh批量在多台主机上执行命令

Centos7安装pssh


yum安装pssh

yum install pssh -y

# 检查pssh
which pssh

pip2安装pssh

# 安装python-pip
yum install python-pip -y

# 使用pip2安装pssh
pip3 uninstall pssh
pip2 install pssh

# 检查pssh
which pssh

编写host.txt

相互设置主机免密

192.168.0.145
192.168.0.146
192.168.0.147

基本使用

参考: https://blog.51cto.com/dianel/1973190

pssh -h host.txt -P 'command'

## -h hosts.txt 指定服务器列表的文件为hosts.txt
## -P 打印
## "command"  执行的命令,用单引号括起来

自动接受hostkey

StrictHostKeyChecking=no可以自动接受本地的hostkey

pssh -i -O "StrictHostKeyChecking=no" -h /etc/pssh_hosts 'date'

执行结果输出到文件

# 批量执行uptime命令
pssh -h hosts.txt -l root -o /tmp/uptime uptime

## -h hosts.txt 指定服务器列表的文件为hosts.txt
##-l root 指定远程用户为root
## -o /tmp/uptime 指定远程命令执行返回结果输出目录为/tmp/uptime
## uptime 指定远程服务器上执行的命令为uptime

直接打印输出结果

pssh -h hosts.txt -P -i ls
## -P 打印
## -i 指定命令

猜你喜欢

转载自blog.csdn.net/omaidb/article/details/125782893