linux pssh parallel-ssh 批量执行远程shell命令

目录

pssh简介

pssh参数列表

使用实例


pssh简介

文档地址:

https://parallel-ssh.readthedocs.io/en/latest/

python异步并行SSH客户端库。

在许多服务器上异步运行SSH命令,而在客户端主机上的系统负载却最小,可以运行数十万。

高性能的基于本机代码的客户端-基于libssh2 C库。

假设现在需要对数百台服务器节点进行配置更新或者执行一些简短command,而目前并没有完备的部署工具软件, 那可以选择向pssh这样的并行登录远程终端并执行指定命令的shell工具。
以前机器节点少的时候,直接用shell写个for循环来执行命令,也没什么问题。当节点数量多了之后,一个shell命令可能要消耗几秒, 这时才能感受到pssh这种并行方式的好处,省时省力。

安装:yum -y install pssh 

pssh参数列表

除了pssh,当需要传递登录密码时,可以用到sshpass命令:

pintai@MG:~/bak$ 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

pintai@MG:~/bak$ sshpass --help
sshpass: invalid option -- '-'
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
   -f filename   Take password to use from file
   -d number     Use number as file descriptor for getting password
   -p password   Provide password as argument (security unwise)
   -e            Password is passed as env-var "SSHPASS"
   With no parameters - password will be taken from stdin

   -h            Show help (this screen)
   -V            Print version information
At most one of -f, -d, -p or -e should be used

在线直译

pintai @ MG:〜/ bak $ pssh-帮助
用法:pssh [OPTIONS]命令[...]

选项:
  --version显示程序的版本号并退出
  --help显示此帮助消息并退出
  -h HOST_FILE,--hosts = HOST_FILE
                        主机文件(每行“ [user @] host [:port]”)
  -H HOST_STRING,--host = HOST_STRING
                        其他主机条目(“ [user @] host [:port]”)
  -l USER,-user = USER用户名(可选)
  -p PAR,--par = PAR并行线程的最大数量(可选)
  -o OUTDIR,--outdir = OUTDIR
                        标准输出文件的输出目录(可选)
  -e ERRDIR,--errdir = ERRDIR
                        stderr文件的输出目录(可选)
  -t超时,--timeout =超时
                        每个主机的超时(秒)(0 =无超时)(可选)
  -O OPTION,--option = OPTION
                        SSH选项(可选)
  -v,--verbose打开警告和诊断消息(可选)
  -A,-askpass要求输入密码(可选)
  -x ARGS,--extra-args = ARGS
                        额外的命令行参数,处理
                        空格,引号和反斜杠
  -X ARG,--extra-arg = ARG
                        额外的命令行参数
  -i,--inline每个服务器的内联内联聚合输出和错误
  --inline-stdout每个服务器的内联标准输出
  -I,--send-input从标准输入读取并作为输入发送到ssh
  -P,--print打印输出

示例:pssh -h hosts.txt -l irb2 -o / tmp / foo正常运行时间

pintai @ MG:〜/ bak $ sshpass-帮助
sshpass:无效选项-'-'
用法:sshpass [-f | -d | -p | -e] [-hV]命令参数
   -f filename从文件中获取密码以使用
   -d number使用number作为获取密码的文件描述符
   -p password提供密码作为参数(不安全)
   -e密码作为env-var“ SSHPASS”传递
   没有参数-密码将从stdin获取

   -h显示帮助(此屏幕)
   -V打印版本信息
最多应使用-f,-d,-p或-e之一

使用实例

1.使用sshpass传递登录密码

先把需要远程登录的host集中写到一个文件, 比如叫hostlist,写入host列表:

192.168.1.11:22
192.168.1.12:22
192.168.1.13:22

然后将ssh登录密码写到另一个文件, 比如叫remotepass, 写入密码:

yourpassword

最后执行相关命令, 直接打印每个节点的输出内容:

sshpass -f remotepass pssh -h hostlist -l yourloginname -A -i "hostname"

输出结果如下

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
[1] 11:23:21 [SUCCESS] 192.168.1.11:22
test1.hostname
[2] 11:23:21 [SUCCESS] 192.168.1.12:22
test2.hostname
[3] 11:23:21 [SUCCESS] 192.168.1.13:22
test3.hostname

2.将结果输出到指定文件

如果需要将输出结果收集起来,那么可以通过-o选项来指定结果输出目录,比如:

sshpass -f remotepass pssh -h hostlist -l yourloginname -o outputdir -A "hostname"

执行时终端输出:

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
[1] 11:23:21 [SUCCESS] 192.168.1.11:22
[2] 11:23:21 [SUCCESS] 192.168.1.12:22
[3] 11:23:21 [SUCCESS] 192.168.1.13:22

而当前目录会生成outputdir目录,目录中每个host占一个文件,如:

pintai@MG:~/bak$ ls output/
192.168.1.11:22 192.168.1.12:22 192.168.1.13:22
pintai@MG:~/bak$ cat output/*
test1.hostname
test2.hostname
test3.hostname

3. 执行sudo命令

有些shell命令可能需要通过sudo权限来执行,一般来说本地可以这么执行

echo your_sudo_pass | sudo -S your_command

而在pssh中可以这么做:

sshpass -f remotepass pssh -h hostlist -l yourloginname -o outputdir -A "echo your_sudo_pass | sudo -S netstat -antup | grep xxx"

执行完毕后,具体输出结果可以在outputdir目录下查找。

4. 使用private key拷贝本地文件到多个远程终端

pscp -x "-i /locadir/id_rsa" -l yourname -h nodes.txt /tmp/local.txt /remote/dir/

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值