zoukankan      html  css  js  c++  java
  • pssh安装及使用

    pssh全称是parallel-ssh,基于Python编写的并发在多台服务器上批量执行命令的工具,它支持文件并行复制、远程并行执行命令、杀掉远程主机上的进程等;该工具可以视作ansible的简化版本,不需要安装任何客户端程序,只需要一台控制主机,并保证控制主机与被管理主机之间建立单向互信即可;由于pssh基于python实现的,所以在安装之前务必保证安装好python2.6以上。

    pssh工具包主要用法有5个:

    • pssh 多主机并行运行命令
    • pscp 传输文件到多个hosts,他的特性和scp差不多
    • pslurp 从多台远程机器拷贝文件
    • pnuke kill远程机器的进程
    • pslurp 从远程主机考本文件到本地
    • prsync 使用rsync协议从本地计算机同步到远程主机

    参数如下:

    常用选项:-H  、-h 、-i

    –version:查看版本
    -h:主机文件列表,内容格式”[user@]host[:port]”
    -H:主机字符串,内容格式”[user@]host[:port]”
    -l:登录使用的用户名
    -p:并发的线程数【可选】
    -o:输出的文件目录【可选】
    -e:错误输入文件【可选】
    -t:TIMEOUT 超时时间设置,0无限制【可选】
    -O:SSH的选项
    -v:详细模式
    -A:提示手动输入密码模式
    -x:额外的命令行参数使用空白符号,引号,反斜线处理
    -X:额外的命令行参数,单个参数模式,同-x
    -i:每个服务器内部处理信息输出
    -P:打印出服务器返回信息

    1、安装

    先安装ez_setup.py,ez_setup.py是python官方给出的一个安装setuptools的工具,利用它,我们可以很轻松用命令行就自动完成安装过程。

    [root@nfs-server test]# wget http://peak.telecommunity.com/dist/ez_setup.py
    --2019-08-03 00:02:41--  http://peak.telecommunity.com/dist/ez_setup.py
    正在解析主机 peak.telecommunity.com... 209.190.5.234
    正在连接 peak.telecommunity.com|209.190.5.234|:80... 已连接。
    已发出 HTTP 请求,正在等待回应... 200 OK
    长度:10240 (10K) [text/plain]
    正在保存至: “ez_setup.py”
    
    100%[========================================================================>] 10,240      2.73K/s eta(英国中部时
    100%[========================================================================>] 10,240      2.73K/s   in 3.7s    
    2019-08-03 00:02:48 (2.73 KB/s) - 已保存 “ez_setup.py” [10240/10240])
    
    [root@nfs-server test]# wget http://files.opstool.com/files/pssh-2.3.tar.gz
    --2019-08-03 00:04:03--  http://files.opstool.com/files/pssh-2.3.tar.gz
    正在解析主机 files.opstool.com... 121.41.227.112
    正在连接 files.opstool.com|121.41.227.112|:80... 已连接。
    已发出 HTTP 请求,正在等待回应... 200 OK
    长度:22135 (22K) [application/octet-stream]
    正在保存至: “pssh-2.3.tar.gz”
    
    100%[========================================================================>] 22,135      --.-K/s   in 0.05s   
    
    2019-08-03 00:04:05 (471 KB/s) - 已保存 “pssh-2.3.tar.gz” [22135/22135])

    2、解压安装包并安装:

    [root@nfs-server test]# ll
    总用量 52
    -rw-r--r-- 1 root root 10240 1月  21 2011 ez_setup.py
    -rw-r--r-- 1 root root 22135 6月  20 2014 pssh-2.3.tar.gz
    -rw-r--r-- 1 root root    44 8月   2 23:50 pssh.txt
    [root@nfs-server test]# tar xf pssh-2.3.tar.gz 
    [root@nfs-server test]# cd pssh-2.3
    [root@nfs-server pssh-2.3]# python setup.py build
    running build
    running build_py
    running build_scripts
    [root@nfs-server pssh-2.3]# python setup.py install
    running install
    running build
    running build_py
    creating build
    creating build/lib
    creating build/lib/psshlib
    copying psshlib/__init__.py -> build/lib/psshlib
    copying psshlib/cli.py -> build/lib/psshlib
    copying psshlib/manager.py -> build/lib/psshlib
    copying psshlib/color.py -> build/lib/psshlib
    copying psshlib/psshutil.py -> build/lib/psshlib
    copying psshlib/askpass_server.py -> build/lib/psshlib
    copying psshlib/task.py -> build/lib/psshlib
    copying psshlib/askpass_client.py -> build/lib/psshlib
    copying psshlib/version.py -> build/lib/psshlib
    running build_scripts
    creating build/scripts-2.6
    copying and adjusting bin/pssh -> build/scripts-2.6
    copying and adjusting bin/pnuke -> build/scripts-2.6
    copying and adjusting bin/prsync -> build/scripts-2.6
    copying and adjusting bin/pslurp -> build/scripts-2.6
    copying and adjusting bin/pscp -> build/scripts-2.6
    copying and adjusting bin/pssh-askpass -> build/scripts-2.6
    changing mode of build/scripts-2.6/pssh from 644 to 755
    changing mode of build/scripts-2.6/pnuke from 644 to 755
    changing mode of build/scripts-2.6/prsync from 644 to 755
    changing mode of build/scripts-2.6/pslurp from 644 to 755
    changing mode of build/scripts-2.6/pscp from 644 to 755
    changing mode of build/scripts-2.6/pssh-askpass from 644 to 755
    running install_lib
    creating /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/__init__.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/cli.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/manager.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/color.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/psshutil.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/askpass_server.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/task.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/askpass_client.py -> /usr/lib/python2.6/site-packages/psshlib
    copying build/lib/psshlib/version.py -> /usr/lib/python2.6/site-packages/psshlib
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/__init__.py to __init__.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/cli.py to cli.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/manager.py to manager.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/color.py to color.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/psshutil.py to psshutil.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_server.py to askpass_server.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/task.py to task.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_client.py to askpass_client.pyc
    byte-compiling /usr/lib/python2.6/site-packages/psshlib/version.py to version.pyc
    running install_scripts
    copying build/scripts-2.6/pslurp -> /usr/bin
    copying build/scripts-2.6/pnuke -> /usr/bin
    copying build/scripts-2.6/pscp -> /usr/bin
    copying build/scripts-2.6/prsync -> /usr/bin
    copying build/scripts-2.6/pssh-askpass -> /usr/bin
    copying build/scripts-2.6/pssh -> /usr/bin
    changing mode of /usr/bin/pslurp to 755
    changing mode of /usr/bin/pnuke to 755
    changing mode of /usr/bin/pscp to 755
    changing mode of /usr/bin/prsync to 755
    changing mode of /usr/bin/pssh-askpass to 755
    changing mode of /usr/bin/pssh to 755
    running install_data
    creating /usr/man
    creating /usr/man/man1
    copying man/man1/pssh.1 -> /usr/man/man1
    running install_egg_info
    Writing /usr/lib/python2.6/site-packages/pssh-2.3-py2.6.egg-info
    [root@nfs-server pssh-2.3]# echo $?
    0
    [root@nfs-server pssh-2.3]# pssh --version
    2.3

     测试:

    ①pssh 多主机并行执行命令

    获取每台机器上的时间,先在管理机上新建ip.txt

    [root@nfs-server test]# cat pssh.txt 
    root@192.168.0.117:22
    root@192.168.0.124:22
    [root@nfs-server test]# pssh -p 2 -i -h /root/test/ip.txt "date"
    [1] 00:40:21 [SUCCESS] root@192.168.0.124:22
    2019年 08月 03日 星期六 00:40:12 CST
    [2] 00:40:21 [SUCCESS] root@192.168.0.117:22
    2019年 08月 03日 星期六 00:40:20 CST

    ②pscp 把文件并行复制到多个机器上:

    将本地的/root/kaishi.sh文件复制到目标服务器的/tmp/目录下

    [root@nfs-server test]# pscp -h /root/test/pssh.txt /root/kaishi.sh /tmp/
    [1] 07:00:28 [SUCCESS] root@192.168.0.124:22
    [2] 07:00:28 [SUCCESS] root@192.168.0.117:22
    [root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "ls /tmp/kaishi.sh"
    [1] 07:02:23 [SUCCESS] root@192.168.0.124:22
    /tmp/kaishi.sh
    [2] 07:02:23 [SUCCESS] root@192.168.0.117:22
    /tmp/kaishi.sh

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

    将本地的/etc/sysconfig目录递归同步到目标服务器的 /tmp/目录下,并保持原来的时间戳,使用用户 root

    [root@nfs-server test]# prsync -h /root/test/pssh.txt -l -a -r /etc/sysconfig /tmp/test/
    [1] 07:21:57 [SUCCESS] root@192.168.0.124:22
    [2] 07:21:57 [SUCCESS] root@192.168.0.117:22
    [root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "ls /tmp/test/"
    [1] 07:22:33 [SUCCESS] root@192.168.0.117:22
    sysconfig
    [2] 07:22:33 [SUCCESS] root@192.168.0.124:22
    sysconfig

    ④pslurp 将文件从远程主机复制到本地

    将目标服务器的/tmp/*.log格式的文件复制到本地的/tmp目录下,并更名为test1.log格式,在/tmp/目录下即可看见以远端IP地址命名为目录,目录中存放复制过来的文件。

    [root@nfs-server test]# pslurp -p 2  -h /root/test/pssh.txt -L /tmp -l root /tmp/*.log test1.log
    [1] 07:36:06 [SUCCESS] 192.168.0.124:22
    [2] 07:36:06 [SUCCESS] 192.168.0.117:22
    [root@nfs-server test]# ll /tmp/
    总用量 16
    drwxr-xr-x  2 root root 4096 8月   3 07:36 192.168.0.117
    drwxr-xr-x  2 root root 4096 8月   3 07:36 192.168.0.124
    drwxr-xr-x. 7 root root 4096 7月  30 22:00 sysconfig
    drwxr-xr-x  3 root root 4096 8月   3 07:16 test
    [root@nfs-server test]# cat /tmp/192.168.0.117/test1.log 
    117
    [root@nfs-server test]# cat /tmp/192.168.0.124/test1.log 
    124

    ⑤pnuke 并行在远程主机杀进程:

    将远端服务器nginx进程杀死

    [root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "lsof -i:80"  #查看远端nginx状态
    [1] 07:44:16 [SUCCESS] root@192.168.0.117:22
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    nginx   2739  root    7u  IPv4  17042      0t0  TCP *:http (LISTEN)
    nginx   2740 nginx    7u  IPv4  17042      0t0  TCP *:http (LISTEN)
    [2] 07:44:16 [SUCCESS] root@192.168.0.124:22
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    nginx   2665  root    6u  IPv4  16143      0t0  TCP *:http (LISTEN)
    nginx   2666 nginx    6u  IPv4  16143      0t0  TCP *:http (LISTEN)
    [root@nfs-server test]# pnuke -p 2 -h /root/test/pssh.txt nginx  #杀死远端nginx进程
    [1] 07:44:36 [SUCCESS] root@192.168.0.117:22
    [2] 07:44:36 [SUCCESS] root@192.168.0.124:22
    [root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "lsof -i:80"
    [1] 07:44:42 [FAILURE] root@192.168.0.124:22 Exited with error code 1
    [2] 07:44:42 [FAILURE] root@192.168.0.117:22 Exited with error code 1

    其实集群管理的工具有很多,例如saltstack,puppet,func等,当机器数量比较多的时候,就需要考虑合适的工具来管理的,这样工作效率就会大大提高,事半功倍。

  • 相关阅读:
    0309. Best Time to Buy and Sell Stock with Cooldown (M)
    0621. Task Scheduler (M)
    0106. Construct Binary Tree from Inorder and Postorder Traversal (M)
    0258. Add Digits (E)
    0154. Find Minimum in Rotated Sorted Array II (H)
    0797. All Paths From Source to Target (M)
    0260. Single Number III (M)
    0072. Edit Distance (H)
    0103. Binary Tree Zigzag Level Order Traversal (M)
    0312. Burst Balloons (H)
  • 原文地址:https://www.cnblogs.com/su-root/p/11291840.html
Copyright © 2011-2022 走看看