欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

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

最编程 2024-07-18 15:58:40
...


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

  • ​Centos7安装pssh​
  • ​yum安装pssh​
  • ​pip2安装pssh​
  • ​编写host.txt​
  • ​基本使用​
  • ​自动接受hostkey​
  • ​执行结果输出到文件​
  • ​直接打印输出结果​

Centos7安装pssh



yum安装pssh

yum install pssh -y

# 检查pssh
which



pip2安装pssh

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

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

# 检查pssh
which



编写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 指定命令