轻量级自动化运维工具pssh与pslurp

轻量级自动化运维工具pssh与pslurp

pssh是一个基于Python编写,可在多台服务器上执行命令,文件的并行复制的工具

要使用pssh工具包,必须配置本地主机和被管理的远程主机之间ssh单向的免秘钥认证

1 安装

# env Python 2.7.5
wget https://pypi.python.org/packages/source/p/pssh/pssh-2.3.1.tar.gz
tar zxvf pssh-2.3.1.tar.gz
cd pssh-2.3.1
python setup.py build
python setup.py install
pssh --version

2 简单使用

2.1 介绍

  • https://man.linuxde.net/pssh

  • pssh 多主机并行运行命令

  • pscp 传输文件到多个hosts,他的特性和scp差不多

  • pslurp 从多台远程机器拷贝文件

  • pnuke kill远程机器的进程

  • pslurp 从远程主机考本文件到本地

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

# 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

2.2 执行命令

pssh -ih hosts.list "ifconfig eth0 | grep inet | awk '{print $2}'"   # 执行命令
pssh -ih hosts.list  -o /tmp/pssh/ uptime  # 执行结果输出到文件

pscp -h hosts.list anaconda-ks.cfg /tmp/    #复制本地文件到远程主机
pscp -h hosts.list -r /data /tmp/           # 递归复制文件本地文件到远程主机

pslurp -h ecs1.nc -L /tmp/out/ /tmp/name hostname  # 复制远程主机/tmp/name文件到本地/tmp/out目录中,并重命名为hostname,本地目录会自动新建远程主机的名的目录

prsync  -h hosts.list -r /mnt/ /mnt/

pnuke -h hosts.list  nginx       # 批量杀死进程

pssh -h host.ip -P -I < cmd.sh
# 执行命令
#pssh -ih hosts.list uptime
[1] 16:56:57 [SUCCESS] 172.16.0.77
 16:56:57 up 297 days, 20:40,  0 users,  load average: 2.08, 1.99, 1.97
[2] 16:56:57 [SUCCESS] 172.16.0.202
 16:56:57 up 297 days, 20:40,  0 users,  load average: 1.88, 2.03, 2.01
[3] 16:56:57 [SUCCESS] 172.16.0.138
 16:56:57 up 297 days, 20:40,  0 users,  load average: 2.15, 2.15, 2.13
[4] 16:56:57 [SUCCESS] 172.16.0.78
 16:56:57 up 297 days, 20:40,  0 users,  load average: 2.89, 2.68, 2.68
[5] 16:56:57 [SUCCESS] 172.16.0.201
 16:56:57 up 297 days, 20:40,  0 users,  load average: 2.00, 2.02, 2.04
[6] 16:56:57 [SUCCESS] 172.16.0.139
 16:56:57 up 297 days, 20:40,  0 users,  load average: 1.89, 1.91, 1.94

#cat hosts.list
172.16.0.77
172.16.0.202
172.16.0.139
172.16.0.138
172.16.0.78
172.16.0.201

# 命令结果到处到文件
#pssh -h hosts.lsit  -o /tmp/pssh/ uptime
[1] 17:05:01 [SUCCESS] 172.16.0.77
[2] 17:05:01 [SUCCESS] 172.16.0.202
[3] 17:05:01 [SUCCESS] 172.16.0.139
[4] 17:05:01 [SUCCESS] 172.16.0.138
[5] 17:05:01 [SUCCESS] 172.16.0.78
[6] 17:05:01 [SUCCESS] 172.16.0.201
#ll /tmp/pssh/
total 24
-rw-r--r-- 1 root root 72 May 31 17:05 172.16.0.138
-rw-r--r-- 1 root root 72 May 31 17:05 172.16.0.139
-rw-r--r-- 1 root root 72 May 31 17:05 172.16.0.201
-rw-r--r-- 1 root root 72 May 31 17:05 172.16.0.202
-rw-r--r-- 1 root root 72 May 31 17:05 172.16.0.77
-rw-r--r-- 1 root root 72 May 31 17:05 172.16.0.78
#cat /tmp/pssh/172.16.0.138
17:05:01 up 297 days, 20:49,  0 users,  load average: 2.29, 2.12, 2.12

# 拉取远程主机文件到本地,本地会自动建立对应的远程主机的目录
#pslurp -h hosts.list -L /tmp/out/ /tmp/name hostname
[1] 18:13:54 [SUCCESS] 172.16.0.77
[2] 18:13:54 [SUCCESS] 172.16.0.202
[3] 18:13:54 [SUCCESS] 172.16.0.139
[4] 18:13:54 [SUCCESS] 172.16.0.78
[5] 18:13:54 [SUCCESS] 172.16.0.138
[6] 18:13:54 [SUCCESS] 172.16.0.201
#tree /tmp/out/
/tmp/out/
├── 172.16.0.138
│   └── hostname
├── 172.16.0.139
│   └── hostname
├── 172.16.0.201
│   └── hostname
├── 172.16.0.202
│   └── hostname
├── 172.16.0.77
│   └── hostname
└── 172.16.0.78
    └── hostname

PS

原文地址:https://www.cnblogs.com/firewalld/p/14832710.html