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:主機文件列表,內容格式”[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.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 "[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
(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] root@192.168.200.152 /root [2] 19:58:51 [SUCCESS] root@192.168.200.154 /root [3] 19:58:51 [SUCCESS] root@192.168.200.153 /root [4] 19:58:52 [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: [1] 20:13:36 [SUCCESS] root@192.168.200.152 2016年 07月 11日 星期一 20:10:24 CST [2] 20:13:36 [SUCCESS] root@192.168.200.154 2016年 07月 11日 星期一 20:10:11 CST [3] 20:13:36 [SUCCESS] root@192.168.200.153 2016年 07月 11日 星期一 20:10:56 CST [4] 20:13:36 [SUCCESS] root@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'
4. 介紹軟件包內其他命令
pscp 傳輸文件到多個hosts,他的特性和scp差不多
# 通過pscp對多個機器傳文件,把test.sh傳送到多個機器上 $ pscp.pssh -h host.txt -l root -A test.sh
使用pscp對多個機器傳文件,然后再通過pssh執行腳本,方便快捷
pslurp 從多台遠程機器拷貝文件
pnuke kill遠程機器的進程
通過上面我們可以看到直接可以遠程執行命令,對於機器的批量操作很方便。 大家使用的時候可以根據自己的實際需求編寫相應的腳本
這里就簡單寫這幾個例子。