自动化运维之pssh

Posted on Posted in NoOps
Tips: 本文创建于2015年11月13日,已超过 2 年,内容或图片可能已经失效!

pssh.jpg

pssh是一个用python编写,可以并发在多台服务器上批量执行命令的工具。支持文件并行复制,远程执行命令,杀掉远程主机进程

官网

要配置服务器的ssh单向信任

tar zxvf pssh-2.3.1.tar.gz
cd pssh-2.3.1
python setup.py install

安装结果

copying build/scripts-2.6/pssh -> /usr/bin
copying build/scripts-2.6/pssh-askpass -> /usr/bin
copying build/scripts-2.6/pslurp -> /usr/bin
copying build/scripts-2.6/pnuke -> /usr/bin
copying build/scripts-2.6/prsync -> /usr/bin
copying build/scripts-2.6/pscp -> /usr/bin
changing mode of /usr/bin/pssh to 755
changing mode of /usr/bin/pssh-askpass to 755
changing mode of /usr/bin/pslurp to 755
changing mode of /usr/bin/pnuke to 755
changing mode of /usr/bin/prsync to 755
changing mode of /usr/bin/pscp to 755

帮助文件

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    #后面跟一个远程主机名或IP地址
                        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参数的具体配置,可以参照ssh_config 文件的配置参数。eg:pssh -O StrictHostKeyChecking=no
                        SSH option (OPTIONAL)
  -v, --verbose         turn on warning and diagnostic messages (OPTIONAL)
  -A, --askpass         Ask for a password (OPTIONAL)   #提示输入密码,并把密码传递给ssh服务
  -x ARGS, --extra-args=ARGS    #用于传递ssh命令的一些参数,每个参数用引号括起来,当需要传递多个ssh命令参数时,不同参数用空格分开。eg:pssh -x "-l user001" "-p 22"
                        Extra command-line arguments, with processing for
                        spaces, quotes, and backslashes
  -X ARG, --extra-arg=ARG   #与-x用法一样,不过-X只能指定一个ssh命令参数
                        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

1、pssh批量查看远程主机信息

pssh -H root@192.168.1.153 -P date
192.168.1.153: Fri Nov 13 09:44:29 CST 2015
[1] 09:44:29 [SUCCESS] root@192.168.1.153
pssh -H root@192.168.1.153 -i date
[1] 09:45:03 [SUCCESS] root@192.168.1.153
Fri Nov 13 09:45:03 CST 2015

查看多台服务器的负载

cat /etc/pssh/hosts
192.168.1.153
192.168.1.222
pssh -i -O "StrictHostKeyChecking=no" -h /etc/pssh/hosts "uptime"
[1] 09:45:50 [SUCCESS] 192.168.1.153
 09:45:50 up 40 min,  2 users,  load average: 0.00, 0.00, 0.00
[2] 09:45:50 [SUCCESS] 192.168.1.222
 09:45:48 up 3 days,  1:25,  3 users,  load average: 0.01, 0.37, 0.29

pssh调用tar解压远程主机上的文件

pssh -i -h /etc/pssh/hosts "tar -zxvf nginx.tgz"
[1] 09:47:43 [SUCCESS] 192.168.1.153
nginx.txt
[2] 09:47:43 [SUCCESS] 192.168.1.222
install.log

如果要解压root的文件,加sudo

pssh -i -h /etc/pssh/hosts "sudo "tar -zxvf /mnt/nginx.tgz -C /mnt" "

批量删除远程主机的文件

pssh -i -h /etc/pssh/hosts "sudo "rm -rf /mnt/nginx.tgz" "

批量操作服务

pssh -i -h /etc/pssh/hosts "sudo "yum -y install httpd""

综合应用,在远程主机上执行多条命令,用逗号隔开

pssh -i -x "-l opsuser" "-p 9529" -h /etc/pssh/hosts -o /etc/pssh/info "uptime; uname -a"

2、pscp/pslurp

pscp 将本地文件复制到远程多台主机
pslurp 将远程文件复制到本地主机

将本地文件/etc/ssh/ssh_config复制到远程的/root/pssh 目录

/usr/bin/pscp -h /etc/pssh/hosts -e /root/pssh/error /etc/ssh/ssh_config /root/pssh

复制目录

/usr/bin/pscp -h /etc/pssh/hosts -e /root/pssh/error -r /etc/ssh /root/pssh

将远程文件复制到本地,并重命名为ssh1,-L指定本地路径

pslurp -h /etc/pssh/hosts -e /root/pssh/error -L /root/pssh /etc/ssh/ssh_config ssh1

将远程目录复制到本地,-r -L 两个参数顺序不能颠倒

pslurp -h /etc/pssh/hosts -e /root/pssh/error -r -L /root/pssh /etc/ssh ssh1

复制到本地的文件或者文件夹,会以远程主机的IP/域名建立文件夹,拷贝的文件在文件夹底下

3、prsync/pnuke

prsync 将文件或文件夹从本地同步到远程多个主机上

  • -a 可以维持文件的属性值不变
  • -z 压缩传输
prsync -h /etc/pssh/hosts -e /root/pssh/error -l root -a -r /root/pssh /root/pssh
prsync -h /etc/pssh/hosts -e /root/pssh/error -l root -az -r /root/pssh /root/pssh

pnuke 主要作用在远程主机上杀掉某个进程,相当于killall

pnuke -h /etc/pssh/hosts httpd
» 转载请注明来源:若我若鱼 » 自动化运维之pssh

Leave a Reply

Your email address will not be published. Required fields are marked *

17 + eighteen =