linux批量管理推送工具mussh和pssh介绍

2016-03-31 15:46 81 1 收藏

今天图老师小编要向大家分享个linux批量管理推送工具mussh和pssh介绍教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - 服务器 】

linux批量管理推送工具mussh和pssh介绍

 mussh 是shell开发的一个小工具,pssh是python开发的一个用来批量管理linux主机的工具,现在我们来介绍mussh和pssh的使用方法。

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/fuwuqi/)  

先说下mussh,mussh 是shell开发的一个小工具,刚学习运维和shell的朋友可以拿来锻炼下,做个工具的二次开发。


[root@devops-ruifengyun ~ ]$ apt-get install mussh

正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的并且现在不需要了:
  kde-l10n-engb kde-l10n-zhcn libffi6:i386 libglib2.0-0:i386
  libsystemd-daemon0:i386 libudev1:i386 python-async python-git python-gitdb
  python-smmap
Use 'apt-get autoremove' to remove them.
下列软件包将被安装:
  mussh
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 205 个软件包未被升级。
需要下载 14.4 kB 的软件包。
解压缩后会消耗掉 71.7 kB 的额外空间。
获取:1 http://mirrors.oschina.net/ubuntu/ saucy/universe mussh all 1.0-1 [14.4 kB]
下载 14.4 kB,耗时 0秒 (30.3 kB/s)
Selecting previously unselected package mussh.
(正在读取数据库 ... 系统当前共安装有 234998 个文件和目录。)
正在解压缩 mussh (从 .../archives/mussh_1.0-1_all.deb) ...
正在处理用于 man-db 的触发器...
正在设置 mussh (1.0-1) ...
[root@devops-ruifengyun ~ ]$
linux批量管理推送工具mussh和pssh介绍   图老师


mussh的帮助,会看到他的帮助相当的简单。

 代码如下  [root@devops-ruifengyun ~ ]$ mussh --help
Usage: mussh [OPTIONS] -h host.. | -H hostfile [-c cmd] [-C scriptfile]
mussh --help        for full help text
 
Send a command or list of commands to multiple hosts.
 
OPTIONS:
        --help          This text.
        -d [n]          Verbose debug.  Prints each action, all hosts
                        and commands to be executed to STDERR.  'n' can
                        be from 0 to 2.
        -v [n]          Ssh debug levels.  Can be from 0 to 3.
        -m [n]          Run concurrently on 'n' hosts at a time (asynchronous).
                        Use '0' (zero) for infinite. (default if -m)
        -q              No output unless necessary. 
        -i identity [identity ..]
                        Load an identity file.  May be used
                        more than once.
        -o ssh-args   Args to pass to ssh with -o option.
        -a              Force loading ssh-agent.
        -A              Do NOT load ssh-agent.
        -b              Print each hosts' output in a block without mingling
                        with other hosts' output.
        -B              Allow hosts' output to mingle. (default)
        -u              Unique.  Eliminate duplicate hosts. (default)
        -U              Do NOT make host list unique.
        -P              Do NOT fall back to passwords on any host.  This will
                        skip hosts where keys fail.
    -l login      Use 'login' when no other is specified with hostname.
    -L login      Force use of 'login' name on all hosts.
        -s shell      Path to shell on remote host. (Default: bash)
        -t secs       Timeout setting for each session.
                        (requires openssh 3.8 or newer)
        -V              Print version info and exit.
PROXY ARGS:
        -p [user@]host
                        Host to use as proxy.  (Must have mussh installed)
        -po ssh-args        Args to pass to ssh on proxy with -o option.
HOST ARGS:
        -h [user@]host [[user@]host ..]
                        Add a host to list of hosts.  May be
                        used more than once.
        -H file [file ..]
                        Add contents of file(s) to list of hosts.
                        Files should have one host per line.  Use
                        "#" for comments.
COMMAND ARGS:
If neither is specified, commands will be read from standard input.
        -c command    Add a command or quoted list of commands and
                        args to list of commands to be executed on
                        each host.  May be used more than once.
        -C file [file ..]
                        Add file contents to list of commands to be
                        executed on each host.  May be used more
                        than once.
 
At least one host is required.  Arguments are in no particular order.
 
EXAMPLES:
mussh -H ./linuxhosts -C spfiles/testscript.sh
mussh -c "cat /etc/hosts" -h myhost.mydomain.com
 
Comments and Bug Reports: doughnut@doughnut.net




咱们直接跑一个例子,首先需要把对端的主机放到一个文件里面。然后mussh 调用-H 识别主机的list列表。

[root@devops-ruifengyun ~ ]$ 

http://rfyiamcool.blog.51cto.com/

[root@devops-ruifengyun ~ ]$ cat mu.list 

10.1.25.46

10.1.25.47

10.1.25.48

10.1.25.49

[root@devops-ruifengyun ~ ]$ 

[root@devops-ruifengyun ~ ]$ 

[root@devops-ruifengyun ~ ]$ mussh -H ./mu.list -c 'dir'

10.154.252.46: a.py     install.log keepalived-1.2.12

10.154.252.46: epel-release-6-8.noarch.rpm  install.log.syslog  keepalived-1.2.12.tar.gz

10.154.252.47: install.log  install.log.syslog  keepalived-1.2.12  keepalived-1.2.12.tar.gz

10.154.252.48: install.log  install.log.syslog  rs.sh

10.154.252.49: install.log  install.log.syslog  rs.sh

[root@devops-ruifengyun ~ ]$ 

[root@devops-ruifengyun ~ ]$ 

wKiom1O8n8iR-Sr8AAJehoAgJNU949.jpg


测试他有没有并发的特性。


[root@devops-ruifengyun ~ ]$ time mussh -H ./mu.list -c 'sleep 3;dir'

10.154.252.46: a.py         install.log keepalived-1.2.12

10.154.252.46: epel-release-6-8.noarch.rpm  install.log.syslog  keepalived-1.2.12.tar.gz

10.154.252.47: install.log  install.log.syslog  keepalived-1.2.12  keepalived-1.2.12.tar.gz

10.154.252.48: install.log  install.log.syslog  rs.sh

10.154.252.49: install.log  install.log.syslog  rs.sh

mussh -H ./mu.list -c 'sleep 3;dir'  0.04s user 0.16s system 1% cpu 13.206 total

[root@devops-ruifengyun ~ ]$ 


果然没有并发执行。。。这也太不咋低了。


mussh支持发送脚本并执行的,在mussh的cli命令行  直接 -C 跟着脚本就可以了。


[root@devops-ruifengyun ~ ]$ time mussh -H ./mu.list -C c.sh

10.154.252.46: a.py         install.log keepalived-1.2.12

10.154.252.46: epel-release-6-8.noarch.rpm  install.log.syslog  keepalived-1.2.12.tar.gz

10.154.252.47: install.log  install.log.syslog  keepalived-1.2.12  keepalived-1.2.12.tar.gz

10.154.252.48: install.log  install.log.syslog  rs.sh

10.154.252.49: install.log  install.log.syslog  rs.sh

mussh -H ./mu.list -C c.sh  0.10s user 0.17s system 42% cpu 0.629 total

[root@devops-ruifengyun ~ ]$ 

[root@devops-ruifengyun ~ ]$ 

[root@devops-ruifengyun ~ ]$ 


咱们再来测试下pssh这个比mussh要高端的东西,pssh是python开发的一个用来批量管理linux主机的工具。


pssh相关参数


pssh在多个主机上并行地运行命令

-h 执行命令的远程主机列表,文件内容格式[user@]host[:port]

如 test@172.16.10.10:229

-H 执行命令主机,主机格式 user@ip:port

-l 远程机器的用户名

-p 一次最大允许多少连接

-P 执行时输出执行信息

-o 输出内容重定向到一个文件

-e 执行错误重定向到一个文件

-t 设置命令执行超时时间

-A 提示输入密码并且把密码传递给ssh(如果私钥也有密码也用这个参数)

-O 设置ssh一些选项

-x 设置ssh额外的一些参数,可以多个,不同参数间空格分开

-X 同-x,但是只能设置一个参数

-i 显示标准输出和标准错误在每台host执行完毕后


附加工具


pscp 传输文件到多个hosts,类似scp

pscp -h hosts.txt -l irb2 foo.txt /home/irb2/foo.txt

pslurp 从多台远程机器拷贝文件到本地

pnuke 并行在远程主机杀进程

pnuke -h hosts.txt -l irb2 java

prsync 使用rsync协议从本地计算机同步到远程主机

prsync -r -h hosts.txt -l irb2 foo /home/irb2/foo



[root@vm-10-154-252-82 ~]$                                                            

[root@vm-10-154-252-82 ~]$cat list                                                    

10.154.252.46                                                                         

10.154.252.47                                                                         

10.154.252.48                                                                         

10.154.252.49                                                                         

[root@vm-10-154-252-82 ~]$                                                            

[root@vm-10-154-252-82 ~]$                                                            

[root@vm-10-154-252-82 ~]$pssh -i -h list 'uptime'                                    

[1] 10:10:14 [SUCCESS] 10.154.252.46                                                  

 10:10:14 up 62 days, 19:26,  2 users,  load average: 0.08, 0.02, 0.03                

[2] 10:10:14 [SUCCESS] 10.154.252.49                                                  

 10:10:14 up 62 days, 19:30,  1 user,  load average: 0.00, 0.00, 0.00                 

[3] 10:10:14 [SUCCESS] 10.154.252.48                                                  

 10:10:14 up 62 days, 20:49,  1 user,  load average: 0.13, 0.03, 0.01                 

[4] 10:10:14 [SUCCESS] 10.154.252.47                                                  

 10:10:14 up 62 days, 19:59,  0 users,  load average: 0.28, 0.08, 0.02                

[root@vm-10-154-252-82 ~]$                                                            

[root@vm-10-154-252-82 ~]$                                                            


pssh 是python行的,容易实现程序调度的并发。

 代码如下复制代码 [root@vm-10-154-252-82 ~]$time pssh -i -h list 'sleep 3;uptime'           

[1] 10:13:10 [SUCCESS] 10.154.252.46                                      
 10:13:10 up 62 days, 19:29,  2 users,  load average: 0.00, 0.00, 0.01    
[2] 10:13:10 [SUCCESS] 10.154.252.47                                      
 10:13:10 up 62 days, 20:02,  0 users,  load average: 0.01, 0.04, 0.00    
[3] 10:13:10 [SUCCESS] 10.154.252.49                                      
 10:13:10 up 62 days, 19:33,  1 user,  load average: 0.00, 0.00, 0.00     
[4] 10:13:10 [SUCCESS] 10.154.252.48                                      
 10:13:10 up 62 days, 20:52,  1 user,  load average: 0.06, 0.04, 0.00     
real    0m3.175s                                                          
user    0m0.101s                                                          
sys     0m0.038s


wKioL1O8qOODFWxAAAOCxI_a0x8289.jpg                                            


pssh的源码是在 /usr/lib/python2.6/site-packages/psshlib


我们可以看看他的主调度的模块,managepy

import select    
import signal    
import sys       
import threading


里面含有这四个模块。 select用来做调度,signal用来做超时的判断,threading 用来做多任务的并发执行。  pssh 远程的ssh执行,没有调用paramiko或者是fabric这样现成的ssh库,而是直接用subprocess调用系统的ssh进程。

 代码如下       def clear_sigchld_handler(self):                                                    
         signal.signal(signal.SIGCHLD, signal.SIG_DFL)                                   
                                                                                          
     def set_sigchld_handler(self):                                                      
         # TODO: find out whether set_wakeup_fd still works if the default               
         # signal handler is used (I'm pretty sure it doesn't work if the                
         # signal is ignored).                                                           
         signal.signal(signal.SIGCHLD, self.handle_sigchld)                              
         # This should keep reads and writes from getting EINTR.                         
         if hasattr(signal, 'siginterrupt'):                                             
             signal.siginterrupt(signal.SIGCHLD, False)                                  
                                                                                          
     def handle_sigchld(self, number, frame):                                            
         """Apparently we need a sigchld handler to make set_wakeup_fd work."""          
         # Write to the signal pipe (only for Python 2.5, where the                     
         # set_wakeup_fd method doesn't exist).                                          
         if self.iomap.wakeup_writefd:                                                   
             os.write(self.iomap.wakeup_writefd, '')                                   
         for task in self.running:                                                       
             if task.proc:                                                               
                 task.proc.poll()                                                        
         # Apparently some UNIX systems automatically resent the SIGCHLD                 
         # handler to SIG_DFL.  Reset it just in case.                                   
         self.set_sigchld_handler()                                                      
     def check_timeout(self):                                                                          
         """Kills timed-out processes and returns the lowest time left."""                             
         if self.timeout = 0:                                                                         
             return None                                                                               
                                                                                                       
         min_timeleft = None                                                                           
         for task in self.running:                                                                     
             timeleft = self.timeout - task.elapsed()                                                  
             if timeleft = 0:                                                                         
                 task.timedout()                                                                       
                 continue                                                                              
             if min_timeleft is None or timeleft min_timeleft:                                       
                 min_timeleft = timeleft                                                               
                                                                                                       
         if min_timeleft is None:                                                                      
             return 0                                                                                  
         else:                                                                                         
             return max(0, min_timeleft)




解析主机host文件的逻辑

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/fuwuqi/)  代码如下 
def read_host_files(paths, default_user=None, default_port=None):                                     
    """Reads the given host files.                                                                    
                                                                                                      
    Returns a list of (host, port, user) triples.                                                     
    """                                                                                               
    hosts = []                                                                                        
    if paths:                                                                                         
        for path in paths:                                                                            
            hosts.extend(read_host_file(path, default_user=default_user))                             
    return hosts                                                                                      
                                                                                                      
                                                                                                      
def read_host_file(path, default_user=None, default_port=None):                                       
    """Reads the given host file.                                                                     
                                                                                                      
    Lines are of the form: host[:port] [login].                                                       
    Returns a list of (host, port, user) triples.                                                     
    """                                                                                               
    lines = []                                                                                        
    f = open(path)                                                                                    
    for line in f:                                                                                    
        lines.append(line.strip())                                                                    
    f.close()                                                                                         
                                                                                                      
    hosts = []                                                                                        
    for line in lines:                                                                                
        # Skip blank lines or lines starting with #                                                   
        line = line.strip()                                                                           
        if not line or line.startswith('#'):                                                          
            continue                                                                                  
        host, port, user = parse_host_entry(line, default_user, default_port)                         
        if host:                                                                                      
            hosts.append((host, port, user))                                                          
    return hosts




再来看看执行命令的逻辑。

 代码如下 
     def start(self, nodenum, iomap, writer, askpass_socket=None):                      
         """Starts the process and registers files with the IOMap."""                   
         self.writer = writer                                                           
                                                                                         
         if writer:                                                                     
             self.outfile, self.errfile = writer.open_files(self.pretty_host)           
                                                                                         
         # Set up the environment.                                                      
         environ = dict(os.environ)                                                     
         environ['PSSH_NODENUM'] = str(nodenum)                                         
         environ['PSSH_HOST'] = self.host                                               
         # Disable the GNOME pop-up password dialog and allow ssh to use                
         # askpass.py to get a provided password.  If the module file is                
         # askpass.pyc, we replace the extension.                                       
         environ['SSH_ASKPASS'] = askpass_client.executable_path()                      
         if askpass_socket:                                                             
             environ['PSSH_ASKPASS_SOCKET'] = askpass_socket                            
         if self.verbose:                                                               
             environ['PSSH_ASKPASS_VERBOSE'] = '1'                                      
         # Work around a mis-feature in ssh where it won't call SSH_ASKPASS             
         # if DISPLAY is unset.                     

来源:http://www.tulaoshi.com/n/20160331/2051148.html

延伸阅读
标签: 电脑入门
给计算机配置交换空间可以充分利用系统,在物理内存被充满时使用,那么Linux下要如何使用Linux交换空间呢?Linux交换空间又要如何管理呢?下面图老师小编就给大家介绍下Linux下使用交换空间的方法,一起来学习下吧。 一、UNIX如何使用交换空间 所有 UNIX 操作系统都支持某种虚拟内存 (VM) 系统。使用 VM 系统,您有两个主要区域来存储...
1、登录帐户管理 在Linux下登录用户帐户的管理是通过utmp和wtmp这两个工具来实现的。wtmp还记录系统重启和系统状态变化的有关信息。所有与utmp和wtmp相关的数据都分别被保存在/var/run/utmp和/var/log/wtmp这两个文件中。这两个文件均归属于root用户所有并且访问权限被设置为644,这些文件中的数据是加密过的。可以用dump-utmp这个...
防火墙(Firewall)是在一个可信的网络和不可信的网络之间建立安全屏障的软件或硬件产品。Linux操作系统内核具有包过滤能力,系统管理员通过管理工具设置一组规则即可建立一个基于Linux的防火墙,用这组规则过滤被主机接收、发送的包或主机从一个网卡转发到另一个网卡的包,用一台闲置的 PC就可以替代昂贵的专门防火墙硬件,对于某些中小企业...
  随着linux的逐步普及,现在有不少人对于Linux的安装及设置已经比较熟悉了。与Linux 的蓬勃发展相适应,想深入了解Linux的也越来越多。而要想深入了解Linux,就需要阅读和分析linux内核的源代码。 Linux的内核源代码可以从很多途径得到。一般来讲,在安装的linux系统下,/usr/src/linux目录下的东西就是内核源代码。另外还可以...
本文主要介绍linux上检测rootkit的两种工具: Rootkit Hunter和Chkrootkit. Rootkit Hunter     中文名叫”Rootkit猎手”, 可以发现大约58个已知的rootkits和一些嗅探器和后门程序. 它通过执行一系列的测试脚本来确认你的机器是否已经感染rootkits. 比如检查rootkits使用的基本文件, 可执行二进制文件的错误文件权限, 检测内...

经验教程

145

收藏

44
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部