并行执行命令工具简介

作为运维工程师来讲,机器数量到一定级别的时候,批量运维和管理机器就是一件费神
的事情,还好有很多可以批量并行执行命令的工具,比如 pssh , python fabric
taobao 有在pssh基础之上改造的pgm. 这几个工具都可以帮助我们批量运行命令。当然
随着 puppet, ansible等工具的流行这些并行工具变的弱化了,不过依然还是很有用,今天我们来讲述一下 pssh 的使用方式

python并行执行命令工具

之前在阿里工作的时候,并行工具是pgm , 目前可以选择的工具如下

fabric pssh pgm 

pssh 官方介绍

PSSH provides parallel versions of OpenSSH and related tools.

Included are pssh, pscp, prsync, pnuke, and pslurp. The project includes psshlib which can be used within custom applications.
PSSH is supported on Python 2.4 and greater (including Python 3.1 and greater). It was originally written and maintained by Brent N. Chun. Due to his busy schedule, Brent handed over maintenance to Andrew McNabb in October 2009.

pssh 安装部署

下载 parallel-ssh 并安装

git clone https://github.com/ruiaylin/pssh.git

安装 install

cd pssh/
python setup.py install

配置待批量管理的服务器列表

host configuration like this : pssh_config

192.168.102.81:
192.168.8.183:

pssh 也可以配置成为不同的 group ,可以根据不同的组做对应的操作,比如说不同的集群,不同的角色都是可以的。后面有简单的测试。

hostgroups configuration like this :

root@ruiaylin-virtual-machine:~/batch# cat /etc/pssh/hostgroups
master: 192.168.19.132,192.168.19.135
slave: 192.168.19.134

为了管理方便,需要打通管理机器(有些公司叫做跳板机,也有叫做堡垒机)到各个主
机的信任通道,这样会避免每次ssh操作都需要输入密码,机器多的时候会真的疯掉的

打通信任通道 :

cd
mkdir .ssh
ssh-keygen -t dsa
cd .ssh ; ll
[root@xxxxxx .ssh]# ll
total
-rw------- root root Nov : authorized_keys
-rw------- root root Sep : id_dsa
-rw-r--r-- root root Sep : id_dsa.pub
-rw-r--r--. root root Nov : known_hosts
#然后将 id_dsa.pub 文件内容 copy 到 各个主机的
/home/youruser/.ssh/authorized_keys 文件中 , 打通完毕, 如果
操作完成,之后仍然无法直接ssh 登录,问题可能出在 authorized_keys 该文件的属性上面。 一般设置为700

examples :

pssh to execute command

pssh options

OPTIONS
-h host_file # -h + 包含IP的文件名
--hosts host_file
-H [user@]host[:port] # -H + <span style="font-family: Arial, Helvetica, sans-serif;">[用户@]主机IP[:端口] [ ]内的是可选参数 ,若有多个主机,用" "引起来,主机之间用空格分开</span>
--host [user@]host[:port]
-H "[user@]host[:port] [ [user@]host[:port ] ... ]"
<span style="white-space:pre"> </span> --host "[user@]host[:port] [ [user@]host[:port ] ... ]"
-l user # -l + 用户名(用于连接远程主机的用户名)
--user user
-p parallelism # -p + 并发数
--par parallelism
-t timeout # -t + 超时秒数
--timeout timeout
-o outdir # -o + 输出目录 说明:会在该目录下创建 <span style="font-family: Arial, Helvetica, sans-serif;">[用户@]主机IP[:端口]</span><span style="font-family: Arial, Helvetica, sans-serif;"> 格式的文件名,用于保存输出结果</span>
--outdir outdir
-e errdir # -e + 错误输出目录
--errdir errdir
-x args # -x + ssh连接时可提供的参数 ,例: -x "-o
StrictHostKeyChecking=no" 表示跳过ssh链接时询问yes/no
--extra-args args
-X arg
--extra-arg arg
-O options # -O + SSH配置文件中的选项 可以出现多个 -O 选项
--options options
-A
--askpass
-i # -i 参数用于将输出结果直接显示在当前终端
--inline
--inline-stdout
-v # -v 参数用于显示ssh连接时的错误信息
--verbose
-I
--send-input
Read input and send to each ssh process. Since ssh allows a command script to be sent on standard input, the -I option may be used in lieu of the command argument.
-P # -P 参数用于当主机连接上之后,输出执行结果,先输出执行结果,
再显示连接 的主机信息.
--print

执行命令 , 并check

#创建几个目录
pssh -h pssh_config -l root -i 'mkdir -p /root/works/{script,tmp,log} '
#check 刚才创建的结果
[root@dbtaskm works]# pssh -h pssh_config -l root -i 'ls /root/works/ '
[] :: [SUCCESS] 192.168.102.81:
log
script
tmp
[] :: [SUCCESS] 192.168.8.183:
log
script
tmp

多条命令要用分好分割

pssh -h pssh_config -l root -i 'cd  /root/works/  ; ls '
#执行结果
[root@dbtaskm works]# pssh -h pssh_config -l root -i 'cd /root/works/ ; ls '
[] :: [SUCCESS] 192.168.8.183:
log
script
tmp
[] :: [SUCCESS] 192.168.102.81:
log
script
tmp
# 关闭selinux
pssh -h servers.txt -l root -P "sed -i '/SELINUX=enforcing/s/SELINUX=enforcing/SELINUX=disabled/'/etc/sysconfig/selinux"

pscp 集中分到文件到 主机列表的机器

将文件 collect-mysql.py 分发到机器列表对应目录
pscp -h pssh_config  collect-mysql.py   /root/works/tmp/
#check 执行结果
pssh -h pssh_config -l root -i 'cd /root/works/tmp ; ls '
[] :: [SUCCESS] 192.168.102.81:
collect-mysql.py
[] :: [SUCCESS] 192.168.8.183:
collect-mysql.py
如果是包含文件夹 ,请使用 如下命令
 pscp -h pssh_config  -l root  -r  /root/bin/*   /root/bin/  

slurp copy文件到管理机器

pslurp -L /root/works/testlurp/ -h ../pssh_config  /root/works/tmp/collect-mysql.py  mysql.py
#
[] :: [SUCCESS] 192.168.102.81:
[] :: [SUCCESS] 192.168.8.183:
# check the result
[root@dbtaskm testlurp]# cd /root/works/testlurp/ ; ls *
192.168.102.81:
mysql.py
192.168.8.183:
mysql.py

pnuke

The pnuke command is useful when you want to kill a bunch of processes on a set of machines. For example, suppose you’ve got a bunch of java processes running on three nodes that you’d like to nuke (let’s use the three machines from the pssh example). Here you would do the following:

 # pnuke -h ips.txt -l irb2 java
Success on 128.112.152.122:
Success on 18.31.0.190:
Success on 128.232.103.201:

hostgroup 单独测试

配置 /etc/pssh/hostgroups

root@ruiaylin-virtual-machine:~/batch# cat /etc/pssh/hostgroups
master: 192.168.19.132,192.168.19.135
slave: 192.168.19.134

pssh :

root@ruiaylin-virtual-machine:/etc/pssh# pssh -g master  -i hostname
[] :: [SUCCESS] 192.168.19.132
mytestdb02
[] :: [SUCCESS] 192.168.19.135
mytestdb01
root@ruiaylin-virtual-machine:/etc/pssh# pssh -g master -i 'ifconfig |grep inet | grep -v 127 '
[] :: [SUCCESS] 192.168.19.135
inet addr:192.168.19.135 Bcast:192.168.19.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2a:d6db/ Scope:Link
inet6 addr: ::/ Scope:Host
[] :: [SUCCESS] 192.168.19.132
inet addr:192.168.19.132 Bcast:192.168.19.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe78:dfd8/ Scope:Link
inet6 addr: ::/ Scope:Host
root@ruiaylin-virtual-machine:/etc/pssh#
root@ruiaylin-virtual-machine:/etc/pssh#
root@ruiaylin-virtual-machine:/etc/pssh# pssh -g slave -i hostname
[] :: [SUCCESS] 192.168.19.134
mytaskdb
root@ruiaylin-virtual-machine:/etc/pssh#

pscp :

本地create 两个file 用于测试:

root@ruiaylin-virtual-machine:~/batch# ls file1 file2
file1 file2
root@ruiaylin-virtual-machine:~/batch# cat file1 file2
test master
test slave
root@ruiaylin-virtual-machine:~/batch#

执行

root@ruiaylin-virtual-machine:~/batch# pscp -g slave  file2  /root/bin/filetest
[] :: [SUCCESS] 192.168.19.134
root@ruiaylin-virtual-machine:~/batch# pscp -g master file1 /root/bin/filetest
[] :: [SUCCESS] 192.168.19.132
[] :: [SUCCESS] 192.168.19.135
root@ruiaylin-virtual-machine:~/batch#

结果

root@ruiaylin-virtual-machine:~/batch# pssh -g master -i 'cat /root/bin/filetest '
[] :: [SUCCESS] 192.168.19.132
test master
[] :: [SUCCESS] 192.168.19.135
test master
root@ruiaylin-virtual-machine:~/batch# pssh -g slave -i 'cat /root/bin/filetest '
[] :: [SUCCESS] 192.168.19.134
test slave
root@ruiaylin-virtual-machine:~/batch#

总结

pssh 是基于python的一个batch 管理主机的工具, 现在也有 python 的 fabric 模块,也可以完成这类似的工具, 后面有时间可以总结一下。

批量部署 自动化之 - [pssh](转)的更多相关文章

  1. Jenkins +svn +maven +tomcat+ ansible 自动化批量部署

    Jenkins +svn +maven +tomcat+ ansible 自动化批量部署 一.部署svn yum install subversion 先创建目录 mkdir /home/svn 创建 ...

  2. Cobbler自动化批量部署CentOS6.5系统

    Cobbler作为一个预备工具,使批量部署Red Hat/Centos/Fedora系统更容易,同时也支持Suse和Debian系统的部署. 它提供以下服务集成: * PXE服务支持 * DHCP服务 ...

  3. 比Ansible更吊的自动化运维工具,自动化统一安装部署自动化部署udeploy 1.0 版本发布

    新增功能: 逻辑与业务分离,完美实现逻辑与业务分离,业务实现统一shell脚本开发,由框架统一调用. 并发多线程部署,不管多少台服务器,多少个服务,同时发起线程进行更新.部署.启动. 提高list规则 ...

  4. PXE批量部署linux操作系统

    前言 在实际生产环境中,有时候我们会碰到为几十上百甚至上千台服务器安装Linux操作系统的需求,如果我们还是常规的去使用移动介质逐台安装,显然是一件低效又令人抓狂的事情,那要安装到何年何月啊?这对于我 ...

  5. 使用saltstack批量部署服务器运行环境事例——批量部署nagios客户端

    之前关于搭建web服务器集群实验的这篇文章http://www.cnblogs.com/cjyfff/p/3553579.html中,关于如何用saltstack批量部署服务器这一点当时没有记录到文章 ...

  6. centos7使用cobbler(2.8)批量部署操作系统之一

    一.    批量部署操作系统的前提 要想批量部署操作系统,得具备以下条件: 客户机支持pxe网络引导 服务器端和客户端建立网络通信(DHCP) 服务器端要有可供客户机开机引导的引导文件 服务器端的可引 ...

  7. Cobbler批量部署CentOS

    简介 Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows.该工具使用python开发,小巧轻便(才15k行python代码),使用简单的命令即可完成PXE ...

  8. Ansible实现zabbix服务器agent端批量部署

    项目需求:由于搭建zabbix,需要每台服务器都需要安装监控端(agent)正常的的操作是一台一台去安装,这样确实有点浪费时间,这里为大家准备了一款开源 的自动化运维工具Ansible,相信大家也很熟 ...

  9. 使用 PXE+Kickstart 实现无人值守批量部署系统

    一.废话两句 在云数据中心,一次几十台甚至几百台服务器上线,系统安装将变得非常繁琐,系统安装好了后还会涉及很多配置,如果一台台来安装的话工作量非常大.(虽然有加班费,开个玩笑)为了解决这个问题,我们需 ...

随机推荐

  1. 连接LilyPad之Windows平台的驱动

    连接LilyPad之Windows平台的驱动 LilyPad和其他的Arduino控制板的不同之处是它是为电子织物和可穿戴设计的.那么,它的大小就必须要紧凑.所以,它并没有板载其他大多数板子都具有的U ...

  2. 【树形dp】Distance in Tree

    [CF161.D] Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes A tree  ...

  3. 【模拟】Gym - 101190A - Abbreviation

    让你把所有的“连续的仅有首字母大写的”词组用缩写表示,并且在后面用括号注明原词组. #include<cstdio> #include<cstring> using names ...

  4. openfire安装完毕后无法登录控制台(忘记密码)的解决方法

    openfire登录管理控制提示: Login failed:make sure your username and password are correct and that you’re an a ...

  5. Learning Spatial Regularization with Image-level Supervisions for Multi-label Image Classification

  6. 网站服务管理系统wdcp系列教程之九

    在我们安装了网站服务管理系统wdcp之后,在使用过程中可能会出现这样或那样的疑问,下面给大家整理几点出来,方便大家学习.还有不懂得话,可以去wdlinux论坛找找相关教程. 1.wdcp如何拒绝或阻止 ...

  7. Android Logcat Security(转)

    讲解了在Android开发中logcat使用不当导致的安全问题 原帖地址:http://drops.wooyun.org/tips/3812 0x00 科普 development version : ...

  8. 解决URL中包含“%2F”导致Apache地址重写mod_rewrite失效的问题

    在使用Apache地址重写mod_rewrite期间,发现,当URL和PATH_INFO中出现%2f(/)或者%5c(\), 会被认为这是个不合法的请求, Apache将会直接返回"404 ...

  9. Linux中的黑洞(black hole)-/dev/null

    http://blog.csdn.net/loongshawn/article/details/50514018

  10. 基于tiny4412的Linux内核移植(支持device tree)(二)

    作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...