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],默認是user爲root,port爲22,可省略
-H host host爲[user@]host[:port],默認是user爲root,port爲22,可省略。可以後面接多個host
-l user 指定默認訪問的用戶
-t timeout 指定超時時間,單位爲秒
-o outdir 指定標準輸出信息的保存路徑,注意是路徑,不是文件
-i 顯示標準輸出和標準錯誤信息到終端
Command:shell腳本或者是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 &