pssh批量管理伺服器 pssh執行本地檔案(指令碼)
阿新 • • 發佈:2018-11-05
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.1.tar.gz cd pssh-2.3.1/ python setup.py install
2. pssh選項說明
--version:檢視版本 --help:檢視幫助,即此資訊 -h:主機檔案列表,內容格式”[[email protected]]host[:port]” -H:主機字串,內容格式”[[email protected]]host[:port]” -:登入使用的使用者名稱 -p:併發的執行緒數【可選】 -o:輸出的檔案目錄【可選】 -e:錯誤輸入檔案【可選】 -t:TIMEOUT 超時時間設定,0無限制【可選】 -O:SSH的選項 -v:詳細模式 -A:手動輸入密碼模式 -x:額外的命令列引數使用空白符號,引號,反斜線處理-X:額外的命令列引數,單個引數模式,同-x -i:每個伺服器內部處理資訊輸出 -P:打印出伺服器返回資訊
3. 例項
(1) 檢視版本
#pssh --version #2.3.1
(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 "[[email protected]]host[:port]") -H HOST_STRING, --host=HOST_STRING additional host entries ("[[email protected]]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
(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: [1] 19:58:51 [SUCCESS] [email protected]192.168.200.152 /root [2] 19:58:51 [SUCCESS] [email protected]192.168.200.154 /root [3] 19:58:51 [SUCCESS] [email protected]192.168.200.153 /root [4] 19:58:52 [SUCCESS] [email protected]192.168.200.155 /root
說明: -h 後面的ip是要操作的機器ip列表,格式如下: [email protected] -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: [1] 20:13:36 [SUCCESS] [email protected]192.168.200.152 2016年 07月 11日 星期一 20:10:24 CST [2] 20:13:36 [SUCCESS] [email protected]192.168.200.154 2016年 07月 11日 星期一 20:10:11 CST [3] 20:13:36 [SUCCESS] [email protected]192.168.200.153 2016年 07月 11日 星期一 20:10:56 CST [4] 20:13:36 [SUCCESS] [email protected]192.168.200.155 2016年 07月 11日 星期一 20:10:10 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: [1] 20:13:36 [SUCCESS] 192.168.200.152 2016年 07月 11日 星期一 20:10:24 CST [2] 20:13:36 [SUCCESS] 192.168.200.154 2016年 07月 11日 星期一 20:10:11 CST [3] 20:13:36 [SUCCESS] 192.168.200.153 2016年 07月 11日 星期一 20:10:56 CST [4] 20:13:36 [SUCCESS] 192.168.200.155 2016年 07月 11日 星期一 20:10:10 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'(8)
(8)pscp拷貝檔案到遠端主機
pscp -h ip.txt /etc/wenjian.txt /tmp/
(9)pnuke殺掉某一程序
這個命令類似yu killall命令
pnuke -h iplist.txt httpd
上邊的意思是在遠端主機上批量關閉httpd服務
能通過killall關閉的服務,都可以通過pnuke來批量完成
(10)pslurp 遠端主機拷貝檔案到本地主機
pslurp -h iplist.txt -L /home/ /hose/wenjian/yuanc.conf open.conf
上邊是,將所有遠端主機/hose/wenjian/yuanc.conf複製到本地主機/home/目錄下,並且重新命名為open.conf -L 來指定本地檔案路徑
(11)拷貝目錄
pslurp -h iplist.txt -r -L /home/ /hose/wenjian/ open
ps:建議分發檔案,執行命令,批量殺死程序,使用pssh,pscp,pnuke,速度很快的
4. 介紹軟體包內其他命令
pscp 傳輸檔案到多個hosts,他的特性和scp差不多# 通過pscp對多個機器傳檔案,把test.sh傳送到多個機器上 $ pscp.pssh -h host.txt -l root -A test.sh 使用pscp對多個機器傳檔案,然後再通過pssh執行指令碼,方便快捷
場景:目標命令中含有特殊符號,導致pssh批量執行可能出問題。
用法:
pssh -h ip.txt -i -I < /datas/1.sh
pssh -h RemoteHosts.ip -P -I < ~/LocalScript.sh