linux pssh 安装和使用方法

2015-1-18 杜世伟 Linux

pssh命令是一个python编写可以在多台服务器上执行命令的轻量运维工具,同时支持拷贝文件,是同类工具中很出色的,
类似pdsh,个人认为相对pdsh更为简便,使用必须在各个服务器上配置好密钥认证访问。

#pssh 安装
1)yum方法
yum install pssh
2)编译安装

wget https://pypi.python.org/packages/60/9a/8035af3a7d3d1617ae2c7c174efa4f154e5bf9c2

4b36b623413b38be8e4a/pssh-2.3.1.tar.gz

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


#创建将要连接的服务器列表,如果连接的服务器的端口号是22后面的端口号可以不填写(默认端口号:22)
cat > hosts.txt <<EOF
root@192.168.111.132:22
root@192.168.111.134:22
root@192.168.111.135:22
EOF

使用下命令之前,要确保当前服务器和将要连接的服务器列表已添加SSH信任,
具体操作方法请参考:https://www.dushiwei.cn/post/598
1:pssh 多主机并行运行命令
#pssh command
#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

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

#Example
#pssh -P -h hosts.txt uptime                                                                                                                                                                                                                      qq(Sat,Dec30)qj
192.168.111.132:  07:58:07 up 1 day,  6:57,  1 user,  load average: 0.08, 0.03, 0.05
[1] 13:32:15 [SUCCESS] root@192.168.111.132:22
192.168.111.135:  13:27:30 up 18:52,  1 user,  load average: 0.38, 0.21, 0.16
[2] 13:32:17 [SUCCESS] root@192.168.111.135:22
192.168.111.134:  07:27:48 up 1 day,  6:27,  1 user,  load average: 0.34, 0.12, 0.07
[3] 13:32:17 [SUCCESS] root@192.168.111.134:22

2:pscp  把文件并行地复制到多个主机上
#pscpcommand
#pscp --help
# pscp --help                                                                                                                                                                                                                                     qq(Sat,Dec30)qj
# Usage: pscp [OPTIONS] local remote

# 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
#   -r, --recursive       recusively copy directories (OPTIONAL)

# Example: 
#pscp -h hosts.txt -l irb2 foo.txt /home/irb2/foo.txt

3:prsync 使用rsync协议从本地计算机同步到远程主机
# prsync --help                                                                                                                                                                                                                                   qq(Sat,Dec30)qj
# Usage: prsync [OPTIONS] local remote

# 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
#   -r, --recursive       recusively copy directories (OPTIONAL)
#   -a, --archive         use rsync -a (archive mode) (OPTIONAL)
#   -z, --compress        use rsync compression (OPTIONAL)
#   -S ARGS, --ssh-args=ARGS
#                         extra arguments for ssh

#Example: 
#prsync -r -h hosts.txt -l irb2 foo /home/irb2/foo

4:pslurp 将文件从远程主机复制到本地,和pscp方向相反:
#comand desc
# pslurp --help                                                                                                                                                                                                                                   qq(Sat,Dec30)qj
# Usage: pslurp [OPTIONS] remote local

# 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
#   -r, --recursive       recusively copy directories (OPTIONAL)
#   -L LOCALDIR, --localdir=LOCALDIR
#                         output directory for remote file copies

# Example: pslurp -h hosts.txt -L /tmp/outdir -l irb2
# /home/irb2/foo.txt foo.txt

5:pnuke 并行在远程主机杀进程:
#comand desc
# pnuke --help                                                                                                                                                                                                                                    qq(Sat,Dec30)qj
# Usage: pnuke [OPTIONS] pattern

# 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

# Example: 
#pnuke -h hosts.txt -l irb2 java

#eample 
#pnuke -h hosts.txt   php-fpm #杀死目标服务器的php-fpm进程,只要ps进程中出现相关词语 都能杀死
# [1] 15:05:14 [SUCCESS] 192.168.9.132 22
# [2] 15:05:14 [SUCCESS] 192.168.9.134 22
# [3] 15:05:14 [SUCCESS] 192.168.9.135 22

参考:
https://pypi.python.org/pypi/pssh/2.3.1
https://www.cnblogs.com/wangkangluo1/archive/2013/01/06/2847353.html
https://www.cnblogs.com/mchina/archive/2013/03/15/2956017.html

标签: linux pssh prsync pslurp pnuke

Powered by emlog 沪ICP备2023034538号-1