linux命令学习5-pssh命令

时间:2022-02-10 22:49:47

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

1. 安装

  安装可以使用yum或者apt-get安装,还可以使用源码安装, 由于我使用apt-get安装不好用,所以这里我只说下源码安装

wget http://parallel-ssh.googlecode.com/files/pssh-2.3.1.tar.gz
tar xf pssh-2.3..tar.gz
cd pssh-2.3./
python setup.py install

2. pssh选项说明

--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:打印出服务器返回信息

3. 实例

 (1) 查看版本  

#pssh --version
#2.3.

 (2) 查看帮助

#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) ( = 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

(3) 使用主机文件列表执行pwd命令 

#pssh -h ip.txt -A -i pwd
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[] :: [SUCCESS] root@192.168.200.152
/root
[] :: [SUCCESS] root@192.168.200.154
/root
[] :: [SUCCESS] root@192.168.200.153
/root
[] :: [SUCCESS] root@192.168.200.155
/root

说明: -h 后面的ip是要操作的机器ip列表,格式如下: root@192.168.200.152   -A 表示手动输入密码模式  -i表示要执行的命令

(4) 使用主机文件列表执行date命令 

#pssh -h ip.txt -A -i date
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[] :: [SUCCESS] root@192.168.200.152
2016年 07月 11日 星期一 :: CST
[] :: [SUCCESS] root@192.168.200.154
2016年 07月 11日 星期一 :: CST
[] :: [SUCCESS] root@192.168.200.153
2016年 07月 11日 星期一 :: CST
[] :: [SUCCESS] root@192.168.200.155
2016年 07月 11日 星期一 :: CST

(5) 指定用户名

#可以通过-l命令指定用户名
$pssh -h ip.txt -A -i -l root date
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[] :: [SUCCESS] 192.168.200.152
2016年 07月 11日 星期一 :: CST
[] :: [SUCCESS] 192.168.200.154
2016年 07月 11日 星期一 :: CST
[] :: [SUCCESS] 192.168.200.153
2016年 07月 11日 星期一 :: CST
[] :: [SUCCESS] 192.168.200.155
2016年 07月 11日 星期一 :: CST

(6) 批量初始化服务器key

#让远程服务自动在/root/.ssh生成秘钥,方便部署证书信任
pssh -h host1.txt -l root -A "ssh-keygen -t rsa -f /root/.ssh/id_rsa -P \"\""

(7)批量修改机器密码

pssh -h host.txt -l root -A 'echo root:xxxxxxxx | chpasswd'

4. 介绍软件包内其他命令

     pscp   传输文件到多个hosts,他的特性和scp差不多
# 通过pscp对多个机器传文件,把test.sh传送到多个机器上
$ pscp.pssh -h host.txt -l root -A test.sh 使用pscp对多个机器传文件,然后再通过pssh执行脚本,方便快捷
    pslurp   从多台远程机器拷贝文件
 
    pnuke    kill远程机器的进程
 
 

通过上面我们可以看到直接可以远程执行命令,对于机器的批量操作很方便。 大家使用的时候可以根据自己的实际需求编写相应的脚本

这里就简单写这几个例子。