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

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

最编程 2024-07-18 16:50:39
...

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

    • Centos7安装pssh
      • yum安装pssh
      • pip2安装pssh
    • 编写host.txt
    • 常用参数
      • 基本使用
      • 自动接受hostkey
      • 执行结果输出到文件
      • 直接打印输出结果
    • pscp把文件并行地复制到多个主机上

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://wangchujiang.com/linux-command/c/pssh.html
–version:查看版本
–help:查看帮助,即此信息
-h:主机文件列表,内容格式”[user@]host[:port]”
-H:主机字符串,内容格式”[user@]host[:port]”
-:登录使用的用户名
-p:并发的线程数【可选】
-o:输出的文件目录【可选】
-e:错误输入文件【可选】
-t:TIMEOUT 超时时间设置,0无限制【可选】
-O:SSH的选项
-v:详细模式
-A:手动输入密码模式
-x:额外的命令行参数使用空白符号,引号,反斜线处理
-X:额外的命令行参数,单个参数模式,同-x
-i:每个服务器内部处理信息输出
-P:打印出服务器返回信息


基本使用

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

## -h:主机文件列表
pssh -h host.txt -P 'command'

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

自动接受hostkey

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

## -i:每个服务器内部处理信息输出
## -O:SSH的选项
pssh -i -O "StrictHostKeyChecking=no" -h /etc/pssh_hosts 'date'

执行结果输出到文件

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

直接打印输出结果

## -h hosts.txt 指定服务器列表的文件为hosts.txt
## -P 打印
pssh -h hosts.txt -P ls

pscp把文件并行地复制到多个主机上

参考 https://tonydeng.github.io/2014/12/08/pssh/

Centos7安装pscp https://www.cnblogs.com/wcwen1990/p/7630541.html

## -h hosts.txt 指定服务器列表的文件为hosts.txt
## -r 用于递归的复制目录
pscp -h ip文件 本地文件 远程目录