栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

使用pssh批量在多台主机上执行命令
    • Centos7安装pssh
      • yum安装pssh
      • pip2安装pssh
    • 编写host.txt
    • 基本使用
      • 自动接受hostkey
      • 执行结果输出到文件
      • 直接打印输出结果

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 指定命令
暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号