• PSSH 批量管理服务器


    pssh命令是一个python编写可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的,类似pdsh,个人认为相对pdsh更为简便,使用必须在各个服务器上配置好密钥认证访问。

    1. 安装

      安装可以使用yum或者apt-get安装,还可以使用源码安装, 由于我使用apt-get安装不好用,所以这里我只说下源码安装

    wget http://parallel-ssh.googlecode.com/files/pssh-2.3.1.tar.gz 
    tar xf pssh-2.3.1.tar.gz 
    cd pssh-2.3.1/ 
    python setup.py install

    2. pssh选项说明

    --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:打印出服务器返回信息

    3. 实例

     (1) 查看版本  

    #pssh --version
    #2.3.1

     (2) 查看帮助

    #pssh --help
    Usage: pssh [OPTIONS] command [...]
    
    Options:
      --version             show program's version number and exit
      --help                show this help message and exit
      -h HOST_FILE, --hosts=HOST_FILE
                            hosts file (each line "[user@]host[:port]")
      -H HOST_STRING, --host=HOST_STRING
                            additional host entries ("[user@]host[:port]")
      -l USER, --user=USER  username (OPTIONAL)
      -p PAR, --par=PAR     max number of parallel threads (OPTIONAL)
      -o OUTDIR, --outdir=OUTDIR
                            output directory for stdout files (OPTIONAL)
      -e ERRDIR, --errdir=ERRDIR
                            output directory for stderr files (OPTIONAL)
      -t TIMEOUT, --timeout=TIMEOUT
                            timeout (secs) (0 = no timeout) per host (OPTIONAL)
      -O OPTION, --option=OPTION
                            SSH option (OPTIONAL)
      -v, --verbose         turn on warning and diagnostic messages (OPTIONAL)
      -A, --askpass         Ask for a password (OPTIONAL)
      -x ARGS, --extra-args=ARGS
                            Extra command-line arguments, with processing for
                            spaces, quotes, and backslashes
      -X ARG, --extra-arg=ARG
                            Extra command-line argument
      -i, --inline          inline aggregated output and error for each server
      --inline-stdout       inline standard output for each server
      -I, --send-input      read from standard input and send as input to ssh
      -P, --print           print output as we get it
    
    Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime

      (3) 使用主机文件列表执行pwd命令  

    #pssh -h ip.txt -A -i pwd
    Warning: do not enter your password if anyone else has superuser
    privileges or access to your account.
    Password: 
    [1] 19:58:51 [SUCCESS] root@192.168.200.152
    /root
    [2] 19:58:51 [SUCCESS] root@192.168.200.154
    /root
    [3] 19:58:51 [SUCCESS] root@192.168.200.153
    /root
    [4] 19:58:52 [SUCCESS] root@192.168.200.155
    /root

    说明: -h 后面的ip是要操作的机器ip列表,格式如下: root@192.168.200.152   -A 表示手动输入密码模式  -i表示要执行的命令

      (4) 使用主机文件列表执行date命令  

    #pssh -h ip.txt -A -i date
    Warning: do not enter your password if anyone else has superuser
    privileges or access to your account.
    Password: 
    [1] 20:13:36 [SUCCESS] root@192.168.200.152
    2016年 07月 11日 星期一 20:10:24 CST
    [2] 20:13:36 [SUCCESS] root@192.168.200.154
    2016年 07月 11日 星期一 20:10:11 CST
    [3] 20:13:36 [SUCCESS] root@192.168.200.153
    2016年 07月 11日 星期一 20:10:56 CST
    [4] 20:13:36 [SUCCESS] root@192.168.200.155
    2016年 07月 11日 星期一 20:10:10 CST

    (5) 指定用户名

    #可以通过-l命令指定用户名
    $pssh -h ip.txt -A -i -l root date
    Warning: do not enter your password if anyone else has superuser
    privileges or access to your account.
    Password: 
    [1] 20:13:36 [SUCCESS] 192.168.200.152
    2016年 07月 11日 星期一 20:10:24 CST
    [2] 20:13:36 [SUCCESS] 192.168.200.154
    2016年 07月 11日 星期一 20:10:11 CST
    [3] 20:13:36 [SUCCESS] 192.168.200.153
    2016年 07月 11日 星期一 20:10:56 CST
    [4] 20:13:36 [SUCCESS] 192.168.200.155
    2016年 07月 11日 星期一 20:10:10 CST

    (6) 批量初始化服务器key

    #让远程服务自动在/root/.ssh生成秘钥,方便部署证书信任
    pssh -h host1.txt -l root -A "ssh-keygen -t rsa -f /root/.ssh/id_rsa -P """

    (7)批量修改机器密码

    pssh -h host.txt -l root -A 'echo root:xxxxxxxx | chpasswd'

    4. 介绍软件包内其他命令

         pscp   传输文件到多个hosts,他的特性和scp差不多
        
    # 通过pscp对多个机器传文件,把test.sh传送到多个机器上
    $ pscp.pssh -h host.txt -l root -A test.sh  

    使用pscp对多个机器传文件,然后再通过pssh执行脚本,方便快捷
        pslurp   从多台远程机器拷贝文件
     
        pnuke    kill远程机器的进程
     
     

    通过上面我们可以看到直接可以远程执行命令,对于机器的批量操作很方便。 大家使用的时候可以根据自己的实际需求编写相应的脚本

    这里就简单写这几个例子。

      

  • 相关阅读:
    BZOJ 2154 Crash的数字表格 【莫比乌斯反演】
    BZOJ 3529 [Sdoi2014]数表 【莫比乌斯反演】
    BZOJ 2820 YY的GCD 【莫比乌斯反演】
    BZOJ 2440 [中山市选2011]完全平方数 【莫比乌斯反演】
    [BalticOI 2004] Sequence
    AtCoder [ARC070E] NarrowRectangles
    AtCoder [AGC022E] Median Replace
    AtCoder [ARC101E] Ribbons on Tree
    CF107D Crime Management
    Loj 6497「雅礼集训 2018 Day1」图
  • 原文地址:https://www.cnblogs.com/davygeek/p/5661501.html
Copyright © 2020-2023  润新知