Linux批量管理服务器小工具--PSSH

一、简介

pssh -- parallel ssh program

  pssh  is  a program for executing ssh in parallel on a number of hosts.  It provides features such as sending input to all of the processes, passing a password to ssh, saving output to files, and timing out.

 

简单理解就是,可以通过ssh,去并行操作远程管理的服务器。

     

建议,最好可以免密码登录到所有远程管理的服务器,免去输入密码的麻烦。

二、安装

网上有很多都是关于pssh的源码安装,我就不介绍了。下面介绍我的安装过程,很简单:

 

服务器操作系统:Centos 6.5 x64,本身yum源,可以直接安装 pssh

     

# yum -y install pssh

三、用法及选项

pssh [options ...] comand ...

   

options: 

-h host_file       Read hosts from the given host_file.   格式为[user@]host[:port],默认是userrootport22,可省略

-H host             host[user@]host[:port],默认是userrootport22,可省略。可以后面接多个host

-l user              指定默认访问的用户

-t timeout          指定超时时间,单位为秒

-o outdir           指定标准输出信息的保存路径,注意是路径,不是文件

-i                     显示标准输出和标准错误信息到终端

      

       Commandshell脚本或者是linux的操作命令

四、示例(man手册提供)

Connect to host1 and host2, and print "hello, world" from each:

pssh -i -H "host1 host2" echo "hello, world"

 

Print "hello, world" from each host specified in the file hosts.txt:

 pssh -i -h hosts.txt echo "hello, world"

 

              Run a command as root with a prompt for the root password:

pssh -i -h hosts.txt -A -l root echo hi

 

Run a long command without timing out:

pssh -i -h hosts.txt -t 0 sleep 10000


五、实际工作运用

1.批量升级服务器的项目

pssh -h host_file -o out_start_log_directory-l user "export BASH_ENV=/etc/profile;项目升级脚本.sh"


2.实时监控服务器的项目日志,到指定的文件夹

nohup pssh -h host_file -lroot -t 0 -o out_log_directory "tail -f 项目日志路径" > out_nohup_log_directory &