pssh批量執行命令


pssh提供OpenSSH和相關工具的並行版本。包括pssh,pscp,prsync,pnuke和pslurp。該項目包括psshlib,可以在自定義應用程序中使用。pssh是python寫的可以並發在多台機器上批量執行命令的工具,它的用法可以媲美ansible的一些簡單用法,執行起來速度比ansible快它支持文件並行復制,遠程命令執行,殺掉遠程主機上的進程等等。

安裝pssh

在CentOS系統環境下,介紹yum的安裝和源碼安裝的方式:

  • yum方法
yum install pssh
  • 編譯安裝
wget http://parallelssh.googlecode.com/files/pssh-2.3.1.tar.gz
tar xf pssh-2..1.tar.gz
cd pssh-2.3.1/
python setup.py install

選項

--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:打印出服務器返回信息

實例

  • 獲取每台服務器的uptime:
# pssh -h ip.txt -i uptime
[1] 11:15:03 [SUCCESS] Mar.mars.he
11:15:11 up 4 days, 16:25,  1 user,  load average: 0.00, 0.00, 0.00
[2] 11:15:03 [SUCCESS] Jan.mars.he
11:15:12 up 3 days, 23:26,  0 users,  load average: 0.00, 0.00, 0.00
[3] 11:15:03 [SUCCESS] Feb.mars.he
11:15:12 up 4 days, 16:26,  2 users,  load average: 0.08, 0.02, 0.01
  • 查看每台服務器上mysql復制IO/SQL線程運行狀態信息:
# pssh -h IP.txt -i "/usr/local/mysql/bin/mysql -e 'show slave status \G'"|grep Running:
            Slave_IO_Running: yes
            Slave_SQL_Running: Yes
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
  • 保存每台服務器運行的結果:
# pssh -h IP.txt -i -o /tmp/pssh/ uptime
[1] 11:19:47 [SUCCESS] Feb.mars.he
11:19:55 up 4 days, 16:31,  2 users,  load average: 0.02, 0.03, 0.00
[2] 11:19:47 [SUCCESS] Jan.mars.he
11:19:56 up 3 days, 23:30,  0 users,  load average: 0.01, 0.00, 0.00
[3] 11:19:47 [SUCCESS] Mar.mars.he
11:19:56 up 4 days, 16:30,  1 user,  load average: 0.00, 0.00, 0.00
  • 我們來看一下/tmp/pssh/下的文件及其內容
# ll /tmp/pssh/
總用量 12
-rw-r--r--. 1 root root 70 12月  1 11:19 Feb.mars.he
-rw-r--r--. 1 root root 70 12月  1 11:19 Jan.mars.he
-rw-r--r--. 1 root root 69 12月  1 11:19 Mar.mars.he
    
# cat /tmp/pssh/*
11:19:55 up 4 days, 16:31,  2 users,  load average: 0.02, 0.03, 0.00
11:19:56 up 3 days, 23:30,  0 users,  load average: 0.01, 0.00, 0.00
11:19:56 up 4 days, 16:30,  1 user,  load average: 0.00, 0.00, 0.00

上面介紹的是pssh命令很少的一部分,大家可以將其用到適合自己的場景,發揮它的最大功效。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM