• Polysh批量管理服务器


    by:铁乐与猫

    注:文章下面涉及到ip地址和端口的敏感信息用了x处理代替。读者使用时可自行替换自己环境中的ip地址和端口。

    用途

    用在跳板机上(ssh免密登录),然后一条命令可以发送到多台机器执行后并在这一台机器上显示结果。分布式架构下,一个服务部署在多台机器下的运维必备利器。

    安装

    Polyshis written in Python and requires Python ≥ 2.4.

    pip install polysh
    

    或访问 https://pypi.org/project/polysh/#description 下载tar源码包进行安装。例:

    wget http://guichaz.free.fr/polysh/files/polysh-x.x.tar.gz
    tar -zvxf polysh-x.x.tar.gz
    cd polysh-x.x
    python setup.py install --home=~
    
    # --home 生产机器环境一般有严格权限管理,这时可以指定到登录者的home目录下进行安装,避免权限导致的安装失败。不指定该参数的话,安装后命令文件会出现在/bin/polysh和/usr/local/bin/polysh
    

    注意,生产环境一般安装在跳板机(堡垒机)上。

    配置(选做)

    系统变量

    成功安装后,会在home参数配置的目录下生成bin目录(按照上述步骤即~/bin)和其目录下的polysh指令文件。将该bin目录配置到~/.bash_profile文件中PATH路径里。

    例:

    vim ~/.bash_profile
    
    PATH=$PATH:$HOME/bin:/usr/local/polysh/bin
    

    免密登录

    可以避免连接多台机器时对各机器密码的管理,达到更高效的便捷使用。需要把polysh所在机器的~/.id_rsa.pub(公钥)内容配置到所需连接的机器上来完成免密登录。

    如果本机不存在~/.ssh/id_rsa.pub文件,需通过ssh-keygen -t rsa来创建本机的公私钥文件。(默认生成无需输入任何参数,三次回车即可)

    执行后,id_rsa(私钥文件)和id_rsa.pub(公钥文件)出现在~/.ssh/文件夹下。

    将id_rsa.pub内容复制在其他连接机器的./ssh/authorized_keys文件中。若不存在依据上述步骤创建。

    另:ssh公钥生效需满足至少下面两个条件:

    • 用户.ssh目录权限为700
    • .ssh/authorized_keys文件权限为600

    ~/.ssh/目录下的文件说明:

    authorized_keys —— 存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥。

    id_rsa —— 生成的私钥文件。

    id_rsa.pub —— 生成的公钥文件。

    know_hosts —— 已知的主机公钥清单。

    使用

    服务器名方式

    例1:{}包括,适合少数服务器

    (venv) [root@cgw2]# polysh czeus_web{1,2}
    ready (2)> nc -v -w 10 -z 10.17.2.x 11x
    czeus_web2 : Connection to 10.17.2.x 11x port [tcp/memcache] succeeded!
    czeus_web1 : Connection to 10.17.2.x 11x port [tcp/memcache] succeeded!
    

    例2:单引号配合<>,适合多台服务器

    [root@cgw2 polysh-home]# polysh --ssh='exec ssh -p 2x' 'cevony<1-30>'
    ready (30)>   
    

    文件方式(--hosts-file)

    例:创建~/host.list

    # 文件每一行为ip地址或域名,推荐用域名,这样提示更明显
    10.17.2.x
    10.17.2.x
    10.17.2.x
    

    保存,之后想用则执行命令:

    polysh --host-file=host.list
    

    注:比较新的版本选项己改成了:

    polysh --hosts-file FILE
    
    Control commands are prefixed by ":".
           [-h] [--hosts-file FILE] [--command CMD] [--ssh SSH] [--user USER]
           [--no-color] [--password-file FILE] [--log-file LOG_FILE]
           [--abort-errors] [--debug] [--profile]
           [host_names [host_names ...]]
    
    # 例:
    # polysh --hosts-file memcache_public_host.list
    

    例,批量服务器用nc命令扫描:

    (venv) [root@cgw2 polysh-home]# polysh --hosts-file czeus_memcache1.list 
    ready (15)> nc -w 4 -v czeus_memcached1 11x
    czeus_memcached2 : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_memcached3 : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_php6       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_memcached4 : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_proxy1     : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_web2       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_web5       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_web6       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_php1       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_php3       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_php7       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_php4       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_php5       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_web1       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    czeus_php2       : Connection to czeus_memcached1 11x port [tcp/memcache] succeeded!
    

    退出

    执行exit或同时按 Crtl+z

    如ssh的端口有变,不是默认的22端口,需指定ssh的端口命令如下:

     polysh --ssh='exec ssh -p 2x' czeus_web{1,2}
    

    同理,在--ssh中 通过exec ssh -i 指定密钥文件。

    如果不执行上面的命令,会出现error:

    (venv) [root@cgw2 polysh-home]# polysh czeus_web{1,2}
    Error talking to czeus_web2  
    Error talking to czeus_web1
    
  • 相关阅读:
    CF Hello 2020 E.New Year and Castle Construction
    HTML 简介
    グランドエスケープ
    CF 1244 C
    N皇后解法以及位运算优化
    CF
    动态规划TG.lv(1) (洛谷提高历练地)
    搜索Ex (洛谷提高历练地)
    数字图像处理——图像增强
    数字图像处理——图像的几何变换
  • 原文地址:https://www.cnblogs.com/tielemao/p/13366396.html
Copyright © 2020-2023  润新知