pssh安裝使用

以前遠程查看或傳輸文件都用ssh或rsync後來聽人介紹了pssh,從網上查到它是並行傳輸的,所以速度更快,使用python寫的。

安裝完後會有5個命令

用途

pssh 在多個主機上並行地運行命令。
pscp 把文件並行地複製到多個主機上。
prsync 通過 rsync 協議把文件高效地並行複製到多個主機上。
pslurp 把文件並行地從多個遠程主機複製到中心主機上。
pnuke 並行地在多個遠程主機上殺死進程。


#yum -y install python

pssh安裝
# wget spacer.gifhttps://parallel-ssh.googlecode.com/files/pssh-2.3.tar.gz
# tar zxvf pssh-2.3.tar.gz
# cd pssh-2.3
# python setup.py build
# python setup.py install

建立簡單ssh密鑰登陸
# ssh-keygen
# rsync -azv /root/.ssh/id_rsa.pub  [email protected]:/root/.ssh/authorized_keys2
這樣99就能直接登陸98了

測試
[root@test-server ~]# pssh -P -h host.txt hostname
192.168.0.98: test2
[1] 00:10:06 [SUCCESS] 192.168.0.98


pssh -P -h host.txt --outdir=/root/abc/ -t 3600 yum -y install vsftpd

#--outdir=路徑,將結果輸入到一個文件

#-t 時間,默認60秒



下面內容來自網絡

2.配置密鑰,免密碼訪問管理機器

在使用之前需要配置密鑰訪問,如下:

#ssh-keygen #一直回車
#複製公鑰到遠端服務器
#ssh-copy-id -i .ssh/id_rsa.pub [email protected]

ps.如果端口不是默認22 ,需要使用:ssh-copy-id -i .ssh/id_rsa.pub  ”-p 4567 [email protected]


3:運行第一條命令,查看啓動時間

]# pssh -h servers.txt -l root -P uptime
173.16.0.102:  13:14:57 up 1 day,  2:00,  1 user,  load average: 0.00, 0.00, 0.00
173.16.0.100:  13:23:34 up 21:40,  1 user,  load average: 0.00, 0.00, 0.00
[1] 13:13:52 [SUCCESS] 173.16.0.102
[2] 13:13:52 [SUCCESS] 173.16.0.100

4:刪除文件

我需要刪除以前yum.repo 文件

pssh -h servers.txt  -P "rm -r /etc/yum.repos.d/*.repo"

5:傳輸單個文件

pscp -h servers.txt /etc/yum.repos.d/ftp.repo /etc/yum.repos.d/
6:運行yum update

]# pssh -h servers.txt -l root -P "yum update"

173.16.0.100: Loaded plugins: fastestmirror
173.16.0.102: Loaded plugins: fastestmirror
173.16.0.100: Loading mirror speeds from cached hostfile
173.16.0.102: Loading mirror speeds from cached hostfile
173.16.0.100: Setting up Update Process
173.16.0.102: Setting up Update Process
173.16.0.100: No Packages marked for Update
173.16.0.102: No Packages marked for Update
[1] 13:38:13 [SUCCESS] 173.16.0.100
[2] 13:38:13 [SUCCESS] 173.16.0.102

7:讓機器加載Ext4

]# pssh -h servers.txt -l root -P "modprobe ext4"
[1] 13:49:42 [SUCCESS] 173.16.0.100
[2] 13:49:42 [SUCCESS] 173.16.0.102

8:安裝e4fsprogs, 支持ext4分區
[root@gluster02b ~]# pssh -h servers.txt -l root -P "yum -y install e4fsprogs"
173.16.0.102: Loaded plugins: fastestmirror
173.16.0.100: Loaded plugins: fastestmirror
173.16.0.100: Complete!
[1] 13:53:11 [SUCCESS] 173.16.0.100
173.16.0.102:

Installed:
 e4fsprogs.x86_64 0:1.41.12-2.el5

173.16.0.102: Complete!
[2] 13:53:11 [SUCCESS] 173.16.0.102

9:創建一個目錄

# pssh -h servers.txt -l root -P "mkdir /data01"
[1] 09:53:27 [SUCCESS] 173.16.0.100
[2] 09:53:27 [SUCCESS] 173.16.0.102
# pscp -h servers.txt /etc/fstab /etc/
[1] 09:53:51 [SUCCESS] 173.16.0.102
[2] 09:53:51 [SUCCESS] 173.16.0.100


10:複製文件夾到遠程

# pscp -h servers.txt -r /root/tools/ /root/
[1] 10:23:27 [SUCCESS] 173.16.0.100
[2] 10:23:28 [SUCCESS] 173.16.0.102


11:關閉Selinux

# pssh -h servers.txt -l root -P "sed -i '/SELINUX=enforcing/s/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux"
[1] 11:47:15 [SUCCESS] 173.16.0.100
[2] 11:47:15 [SUCCESS] 173.16.0.102


# pssh -h servers.txt -l root -P init 6
[1] 11:47:28 [SUCCESS] 173.16.0.102
[2] 11:47:28 [SUCCESS] 173.16.0.100
# pssh -h servers.txt -l root -P sestatus
[1] 11:49:37 [FAILURE] 173.16.0.100 Exited with error code 255
[2] 11:49:37 [FAILURE] 173.16.0.102 Exited with error code 255


# pssh -h servers.txt -l root -P sestatus
173.16.0.100: SELinux status:                 disabled
[1] 11:52:32 [SUCCESS] 173.16.0.100
173.16.0.102: SELinux status:                 disabled
[2] 11:52:32 [SUCCESS] 173.16.0.102


http://sheriy.com/?p=748

http://www.theether.org/pssh/docs/0.2.3/pssh-HOWTO.html


== 簡介 ==
pssh是一個python編寫可以在多臺服務器上執行命令的工具,同時支持拷貝文件,是同類工具中很出色的,類似pdsh,個人認爲相對pdsh更爲簡便,使用必須在各個服務器上配置好密鑰認證訪問。
項目地址:https://code.google.com/p/parallel-ssh/

PSSH provides parallel versions of OpenSSH and related tools. Included are pssh, pscp, prsync, pnuke, and pslurp. The project includes psshlib which can be used within custom applications. The source code is written in Python and can be cloned from:

git clone http://code.google.com/p/parallel-ssh/

PSSH is supported on Python 2.4 and greater (including Python 3.1 and greater). It was originally written and maintained by Brent N. Chun. Due to his busy schedule, Brent handed over maintenance to Andrew McNabb in October 2009.


== 下載安裝 ==

下載

  1. wget http://parallel-ssh.googlecode.com/files/pssh-2.3.1.tar.gz  


安裝
  1. tar xf pssh-2.3.1.tar.gz  

  2. cd pssh-2.3.1/  

  3. python setup.py install  


== 參數命令介紹 ==


'''介紹pssh參數'''
 pssh   在多個主機上並行地運行命令

      -h 執行命令的遠程主機列表  或者 -H user@ip:port  文件內容格式[user@]host[:port]

      -l 遠程機器的用戶名

      -P 執行時輸出執行信息

      -p 一次最大允許多少連接
      -o 輸出內容重定向到一個文件
      -e 執行錯誤重定向到一個文件
      -t 設置命令執行的超時時間
      -A 提示輸入密碼並且把密碼傳遞給ssh
      -O 設置ssh參數的具體配置,參照ssh_config配置文件
      -x 傳遞多個SSH 命令,多個命令用空格分開,用引號括起來
      -X 同-x 但是一次只能傳遞一個命令
      -i 顯示標準輸出和標準錯誤在每臺host執行完畢後

'''其他命令'''

       pscp     傳輸文件到多個hosts,類似scp

       pslurp   從多臺遠程機器拷貝文件到本地

       pnuke    並行在遠程主機殺進程

       prsync   使用rsync協議從本地計算機同步到遠程主機

== 實例 ==
pssh
  1. $ pssh -h ip.txt -l root chkconfig --level 2345 snmpd on  

  2. [1] 10:59:29 [SUCCESS] ... ...  

  3. [2] 10:59:29 [SUCCESS] ... ...  

  4. [3] 10:59:29 [SUCCESS] ... ...  

  5. ... ...  


pscp
  1. $ pscp -h ip.txt -l root /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf  

  2. [1] 11:00:42 [SUCCESS] ... ...  

  3. [2] 11:00:42 [SUCCESS] ... ...  

  4. [3] 11:00:42 [SUCCESS] ... ...  

  5. ... ...  


參考:http://my.oschina.net/guol/blog/59977


(一) 使用前提

中心主機連接遠程主機可以通過ssh密鑰無密碼連接

(二) 命令格式

pssh 總是通過清單 文件指定主機

其中的每行採用 [user] host[:port] 形式。

(三) 創建servers.txt文件

文件的內容爲遠程主機的ip,和用於連接ssh的用戶名和端口

# vim /home/server.txt

192.168.0.177

[email protected]:22031

(四) pssh用法-在多個主機上並行地運行命令

# pssh -P -h /home/server.txt hostname

在默認情況下,每個命令實例的輸出出現在 stdout 中。輸出劃分爲每個主機一段。但是,可以指定一個目錄來捕捉每個實例的輸出。例如,如果運行前面的命令並添加 --outdir=/opt/output/,那麼會把每個主機的命令輸出捕捉到/opt/output/ 中單獨的文件中

# pssh -P -h /home/server.txt --outdir=/opt/output/ hostname


# ll /opt/output/


# cat /opt/output/192.168.0.177

Standby

pssh可以生成最多 32 個進程,並行地連接各個節點。如果遠程命令在 60 秒內沒有完成,連接會終止。如果命令需要更多處理時間,可以使用 -t 設置更長的到期時間。(parallel-scp 和 parallel-rsync 沒有默認的到期時間,但是可以用 -t 指定到期時間。)

(五) pscp用法-把文件或者目錄並行地複製到多個主機上

# pscp -h /home/server.txt /home/server.txt /opt /qubaoquan


其中/home/server.txt 爲本地目錄 /opt/qubaoquan爲文件名,即server.txt複製到遠端後重名爲qubaoquan,如果不指定遠端文件名只有目錄則保持原文件名

也可以複製整個目錄到遠端

# ll /srv/test


# pscp --recursive -h /home/server.txt /srv/test /opt


(六) pslurp用法-把文件或者目錄並行地從多個遠程主機複製到中心主機上

它從每臺遠程計算機收集指定的文件,但是並不覆蓋文件的本地版本。pslurp 爲每臺遠程計算機創建一個子目錄並把指定的文件複製到此位置

# # pslurp --recursive -h /home/server.txt /etc/passwd /qubaoquan


/etc/passwd 爲遠程文件 /qubaoquan爲本地目錄,也可以爲拷貝到本地後的文件名.

# tree /qubaoquan/


# pslurp --recursive -h /home/server.txt -L /srv/test/ /srv llll

其中 --recursive表示遞歸子目錄,-L 選項指定創建子目錄的位置,/srv爲遠程目錄,lll爲拷貝到本地後的目錄名,看目錄結構就知道了

#tree /srv/test/


(七) pnuke用法-並行地在多個遠程主機上殺死進程

pnuke 相當於運行 ssh host killall。pnuke 的參數是一個模式。在遠程計算機上運行的名稱符合這個模式的所有進程都被殺死。可以用此命令方便地在一組服務器上停止同一守護進程。

# pnuke -h /home/server.txt --user=root cron


命令的結果則是在server.txt文件中指定的主機上執行killall cron命令 其中cron爲匹配到cron的進程,只要ps進程中出現相關詞語 都能殺死


192.168.12.4


vi host_list.txt




magic@shopex ~]$ pscp -h host_list.txt iptables /home/magic/.

[magic@shopex ~]$ pscp -h host_list.txt squid.conf /home/magic/.

[magic@shopex ~]$ pscp -h host_list.txt zabbix_agentd.conf /home/magic/.

[magic@shopex ~]$ pscp -h host_list.txt replace.sh /home/magic/.  

[magic@shopex ~]$ pssh -x "-t -t"  -h host_list.txt -P "/home/magic/squid.sh"




pscp -h host_list.txt crontab /home/magic/.



pscp -h host_list.txt crontab /root/.



pssh  -h host_list.txt -P "crontab /root/crontab"


pssh -x "-t -t"  -h host_list.txt -P "sudo crontab /home/magic/crontab"


pssh -x "-t -t"  -h host_list.txt_bk  -P "cp  /home/magic/squid.conf  /usr/local/squid-3.1.16/etc/."


pssh -x "-t -t"  -h host_list.txt_bk  -P "sudo /usr/local/squid-3.1.16/sbin/squid  -k  reconfigure"


pssh -x "-t -t"  -h host_list.txt  -P "cp  /root/squid.conf  /usr/local/squid-3.1.16/etc/."


pssh -x "-t -t"  -h host_list.txt  -P "/usr/local/squid-3.1.16/sbin/squid  -k  reconfigure"



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章