轻量级运维工具pssh

网友投稿 910 2022-08-30

轻量级运维工具pssh

轻量级运维工具pssh

一、概述

pssh提供OpenSSH和相关工具的并行版本。包括pssh,pscp,prsync,pnuke和pslurp。该项目包括psshlib,可以在自定义应用程序中使用

pssh是python写的可以并发在多台机器上批量执行命令的工具,它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。

杀手锏是文件并行复制,当进行再远程主机批量上传-的时候,最好使用它。pssh用于批量ssh操作大批量机器;pssh是一个可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的;比起for循环的做法,更推荐使用pssh!

二、使用

1、安装

1、直接yum安装yum install -y pssh这样子安装完/usr/bin/pnuke/usr/bin/prsync/usr/bin/pscp.pssh ##主要注意这个命令全名是pscp.pssh/usr/bin/pslurp/usr/bin/psshmv /usr/bin/pscp.pssh /usr/bin/pscp2、源码包基本上连不上:这个pip地址可以-xf pssh-2.3.1.tar.gzcd pssh-2.3.1/python setup.py install

2、配置

在使用工具前,确保主机间做了密钥认证,否则无法实现自动化,当然我们可以使用sshpass配合pssh -A参数实现自动输入密码但这要保证多台主机的密码相同,同时还要注意如果known_hosts没有信任远程主机,那么命令执行会失败可以加上-O StrictHostKeyChecking=no参数解决,ssh能用的选项pssh也能用

3、pssh远程批量执行命令

远程批量执行命令Usage: pssh [OPTIONS] command [...]Options:-h --hosts 主机文件列表,格式"host[:port] [user]"-l --user 用户名-p --par 并发线程数-o --outdir 输出的文件目录-e --errdir 错误输出的文件目录-t --timeout 设置命令执行超时时间 -1表示无限制-O --options 设置ssh的一些选项-v --verbose 详细模式-P --print 打印出输出执行信息-i --inline 在每台host执行完毕后,显示出输出信息Example: pssh -h nodes.txt -l irb2 -o /tmp/pssh uptimepssh -h ip.txt -P "uptime" #如果没办法密钥认证.可以采用下面方法,但不是很安全pssh -A -h ip.txt -i "uptime" ##回车再输入密码sshpass -p 123456 pssh -A -h ip.txt -i "uptime"

4、pscp并行传输文件到远端

把文件或者目录并行的复制到多个主机上面Usage: pscp [OPTIONS] -h hosts.txt local remote-r 递归复制目录-h 主机文件列表,格式"host[:port] [user]"-l 用户名-p --par 并发线程数-o --outdir 输出的文件目录-e --errdir 错误输出的文件目录-t --timeout 设置命令执行超时时间 -1表示无限制-O --options 设置ssh的一些选项-v --verbose 详细模式 Example: pscp -h hosts.txt -l irb2 foo.txt /home/irb2/foo.txt#传文件,不支持远程新建目录pscp -h ip.txt test.py /tmp/dir1/#传目录pscp -r -h ip.txt test/ /tmp/dir1/

5、prsync并行传输文件到远端

prsync的主要作用是通过rsync协议将文件或目录从本地主机同步到远程多个主机上Usage: prsync [OPTIONS] -h hosts.txt local remote -r --recursive recusively copy directories (OPTIONAL) -a --archive use rsync -a (archive mode) (OPTIONAL) -z --compress use rsync compression (OPTIONAL) -h --hosts hosts file (each line "host[:port] [login]") -l --user username (OPTIONAL) -p --par max number of parallel threads (OPTIONAL) -o --outdir output directory for stdout files (OPTIONAL) -e --errdir output directory for stderr files (OPTIONAL) -t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL) -v --verbose turn on warning and diagnostic messages (OPTIONAL) -O --options SSH options (OPTIONAL)Example: prsync -r -h hosts.txt -l irb2 foo /home/irb2/foo#传文件,支持远程新建目录,即目录不存在则新建prsync -h ip.txt test.py /tmp/dir2/#传目录prsync -r -h ip.txt test/ /tmp/dir3/

6、pslurp从远程拉取文件到本地

从远程拉取文件到本地,在本地自动创建目录名为远程主机ip的目录,将拉取的文件放在对应主机IP目录下Usage: pslurp [OPTIONS] -h hosts.txt -o outdir remote local -r --recursive recusively copy directories (OPTIONAL) -L --localdir output directory for remote file copies -h --hosts hosts file (each line "host[:port] [login]") -l --user username (OPTIONAL) -p --par max number of parallel threads (OPTIONAL) -o --outdir output directory for stdout files (OPTIONAL) -e --errdir output directory for stderr files (OPTIONAL) -t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL) -v --verbose turn on warning and diagnostic messages (OPTIONAL) -O --options SSH options (OPTIONAL)Example: pslurp -h hosts.txt -L /tmp/outdir -l irb2 /home/irb2/foo.txt foo.txt-L /tmp/outdir:指定从远程主机拷贝文件放置的目录,拷贝远程主机/home/irb2/foo.txt文件,并将其重命名为foo.txt.如果拷贝目录需要使用-r参数:#格式:pslurp -h ip.txt -L <本地目录> <远程目录/文件> <本地重命名>#拉取文件pslurp -h ip.txt -L /root/ /root/1.jpg picturell /root/172.16.1.13/picture-rw-r--r-- 1 root root 148931 Jan 9 15:41 /root/172.16.1.13/picture#拉取目录pslurp -r -h ip.txt -L /root/ /root/test templl -d /root/172.16.1.13/temp/drwxr-xr-x 2 root root 23 Jan 9 15:49 /root/172.16.1.13/temp/

7、pnuke远程批量killall

远程批量killallUsage: pnuke [OPTIONS] -h hosts.txt pattern -h --hosts hosts file (each line "host[:port] [user]") -l --user username (OPTIONAL) -p --par max number of parallel threads (OPTIONAL) -o --outdir output directory for stdout files (OPTIONAL) -e --errdir output directory for stderr files (OPTIONAL) -t --timeout timeout (secs) (-1 = no timeout) per host (OPTIONAL) -v --verbose turn on warning and diagnostic messages (OPTIONAL) -O --options SSH options (OPTIONAL)Example: pnuke -h hosts.txt -l irb2 javapnuke -h ip.txt nginx

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:梅科尔工作室-钟伊帆-小熊派学习笔记(内核开发)
下一篇:25、文件查找命令
相关文章

 发表评论

暂时没有评论,来抢沙发吧~