pssh安裝及使用

pssh全稱是parallel-ssh,基於Python編寫的併發在多臺服務器上批量執行命令的工具,它支持文件並行複製、遠程並行執行命令、殺掉遠程主機上的進程等;該工具能夠視做ansible的簡化版本,不須要安裝任何客戶端程序,只須要一臺控制主機,並保證控制主機與被管理主機之間創建單向互信便可;因爲pssh基於python實現的,因此在安裝以前務必保證安裝好python2.6以上。python

pssh工具包主要用法有5個:nginx

  • pssh 多主機並行運行命令
  • pscp 傳輸文件到多個hosts,他的特性和scp差很少
  • pslurp 從多臺遠程機器拷貝文件
  • pnuke kill遠程機器的進程
  • pslurp 從遠程主機考本文件到本地
  • prsync 使用rsync協議從本地計算機同步到遠程主機

參數以下:服務器

經常使用選項:-H  、-h 、-i併發

–version:查看版本
-h:主機文件列表,內容格式」[user@]host[:port]」
-H:主機字符串,內容格式」[user@]host[:port]」
-l:登陸使用的用戶名
-p:併發的線程數【可選】
-o:輸出的文件目錄【可選】
-e:錯誤輸入文件【可選】
-t:TIMEOUT 超時時間設置,0無限制【可選】
-O:SSH的選項
-v:詳細模式
-A:提示手動輸入密碼模式
-x:額外的命令行參數使用空白符號,引號,反斜線處理
-X:額外的命令行參數,單個參數模式,同-x
-i:每一個服務器內部處理信息輸出
-P:打印出服務器返回信息app

一、安裝ssh

先安裝ez_setup.py,ez_setup.py是python官方給出的一個安裝setuptools的工具,利用它,咱們能夠很輕鬆用命令行就自動完成安裝過程。工具

[root@nfs-server test]# wget http://peak.telecommunity.com/dist/ez_setup.py
--2019-08-03 00:02:41--  http://peak.telecommunity.com/dist/ez_setup.py
正在解析主機 peak.telecommunity.com... 209.190.5.234
正在鏈接 peak.telecommunity.com|209.190.5.234|:80... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:10240 (10K) [text/plain]
正在保存至: 「ez_setup.py」

100%[========================================================================>] 10,240      2.73K/s eta(英國中部時
100%[========================================================================>] 10,240      2.73K/s   in 3.7s    
2019-08-03 00:02:48 (2.73 KB/s) - 已保存 「ez_setup.py」 [10240/10240])

[root@nfs-server test]# wget http://files.opstool.com/files/pssh-2.3.tar.gz
--2019-08-03 00:04:03--  http://files.opstool.com/files/pssh-2.3.tar.gz
正在解析主機 files.opstool.com... 121.41.227.112
正在鏈接 files.opstool.com|121.41.227.112|:80... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:22135 (22K) [application/octet-stream]
正在保存至: 「pssh-2.3.tar.gz」

100%[========================================================================>] 22,135      --.-K/s   in 0.05s   

2019-08-03 00:04:05 (471 KB/s) - 已保存 「pssh-2.3.tar.gz」 [22135/22135])

二、解壓安裝包並安裝:測試

[root@nfs-server test]# ll
總用量 52
-rw-r--r-- 1 root root 10240 1月  21 2011 ez_setup.py -rw-r--r-- 1 root root 22135 6月  20 2014 pssh-2.3.tar.gz -rw-r--r-- 1 root root    44 8月   2 23:50 pssh.txt
[root@nfs-server test]# tar xf pssh-2.3.tar.gz 
[root@nfs-server test]# cd pssh-2.3
[root@nfs-server pssh-2.3]# python setup.py build
running build
running build_py
running build_scripts
[root@nfs-server pssh-2.3]# python setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/psshlib
copying psshlib/__init__.py -> build/lib/psshlib
copying psshlib/cli.py -> build/lib/psshlib
copying psshlib/manager.py -> build/lib/psshlib
copying psshlib/color.py -> build/lib/psshlib
copying psshlib/psshutil.py -> build/lib/psshlib
copying psshlib/askpass_server.py -> build/lib/psshlib
copying psshlib/task.py -> build/lib/psshlib
copying psshlib/askpass_client.py -> build/lib/psshlib
copying psshlib/version.py -> build/lib/psshlib
running build_scripts
creating build/scripts-2.6
copying and adjusting bin/pssh -> build/scripts-2.6
copying and adjusting bin/pnuke -> build/scripts-2.6
copying and adjusting bin/prsync -> build/scripts-2.6
copying and adjusting bin/pslurp -> build/scripts-2.6
copying and adjusting bin/pscp -> build/scripts-2.6
copying and adjusting bin/pssh-askpass -> build/scripts-2.6
changing mode of build/scripts-2.6/pssh from 644 to 755
changing mode of build/scripts-2.6/pnuke from 644 to 755
changing mode of build/scripts-2.6/prsync from 644 to 755
changing mode of build/scripts-2.6/pslurp from 644 to 755
changing mode of build/scripts-2.6/pscp from 644 to 755
changing mode of build/scripts-2.6/pssh-askpass from 644 to 755
running install_lib
creating /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/__init__.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/cli.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/manager.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/color.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/psshutil.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/askpass_server.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/task.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/askpass_client.py -> /usr/lib/python2.6/site-packages/psshlib
copying build/lib/psshlib/version.py -> /usr/lib/python2.6/site-packages/psshlib
byte-compiling /usr/lib/python2.6/site-packages/psshlib/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/cli.py to cli.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/manager.py to manager.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/color.py to color.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/psshutil.py to psshutil.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_server.py to askpass_server.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/task.py to task.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/askpass_client.py to askpass_client.pyc
byte-compiling /usr/lib/python2.6/site-packages/psshlib/version.py to version.pyc
running install_scripts
copying build/scripts-2.6/pslurp -> /usr/bin
copying build/scripts-2.6/pnuke -> /usr/bin
copying build/scripts-2.6/pscp -> /usr/bin
copying build/scripts-2.6/prsync -> /usr/bin
copying build/scripts-2.6/pssh-askpass -> /usr/bin
copying build/scripts-2.6/pssh -> /usr/bin
changing mode of /usr/bin/pslurp to 755
changing mode of /usr/bin/pnuke to 755
changing mode of /usr/bin/pscp to 755
changing mode of /usr/bin/prsync to 755
changing mode of /usr/bin/pssh-askpass to 755
changing mode of /usr/bin/pssh to 755
running install_data
creating /usr/man
creating /usr/man/man1
copying man/man1/pssh.1 -> /usr/man/man1
running install_egg_info
Writing /usr/lib/python2.6/site-packages/pssh-2.3-py2.6.egg-info
[root@nfs-server pssh-2.3]# echo $?
0
[root@nfs-server pssh-2.3]# pssh --version
2.3

 測試:ui

①pssh 多主機並行執行命令spa

獲取每臺機器上的時間,先在管理機上新建ip.txt

[root@nfs-server test]# cat pssh.txt 
root@192.168.0.117:22
root@192.168.0.124:22
[root@nfs-server test]# pssh -p 2 -i -h /root/test/ip.txt "date"
[1] 00:40:21 [SUCCESS] root@192.168.0.124:22
2019年 08月 03日 星期六 00:40:12 CST
[2] 00:40:21 [SUCCESS] root@192.168.0.117:22
2019年 08月 03日 星期六 00:40:20 CST

②pscp 把文件並行複製到多個機器上:

將本地的/root/kaishi.sh文件複製到目標服務器的/tmp/目錄下

[root@nfs-server test]# pscp -h /root/test/pssh.txt /root/kaishi.sh /tmp/
[1] 07:00:28 [SUCCESS] root@192.168.0.124:22
[2] 07:00:28 [SUCCESS] root@192.168.0.117:22
[root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "ls /tmp/kaishi.sh"
[1] 07:02:23 [SUCCESS] root@192.168.0.124:22
/tmp/kaishi.sh
[2] 07:02:23 [SUCCESS] root@192.168.0.117:22
/tmp/kaishi.sh

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

將本地的/etc/sysconfig目錄遞歸同步到目標服務器的 /tmp/目錄下,並保持原來的時間戳,使用用戶 root

[root@nfs-server test]# prsync -h /root/test/pssh.txt -l -a -r /etc/sysconfig /tmp/test/
[1] 07:21:57 [SUCCESS] root@192.168.0.124:22
[2] 07:21:57 [SUCCESS] root@192.168.0.117:22
[root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "ls /tmp/test/"
[1] 07:22:33 [SUCCESS] root@192.168.0.117:22 sysconfig
[2] 07:22:33 [SUCCESS] root@192.168.0.124:22 sysconfig

④pslurp 將文件從遠程主機複製到本地

將目標服務器的/tmp/*.log格式的文件複製到本地的/tmp目錄下,並改名爲test1.log格式,在/tmp/目錄下便可看見以遠端IP地址命名爲目錄,目錄中存放複製過來的文件。

[root@nfs-server test]# pslurp -p 2 -h /root/test/pssh.txt -L /tmp -l root /tmp/*.log test1.log
[1] 07:36:06 [SUCCESS] 192.168.0.124:22 [2] 07:36:06 [SUCCESS] 192.168.0.117:22 [root@nfs-server test]# ll /tmp/ 總用量 16 drwxr-xr-x 2 root root 4096 8月 3 07:36 192.168.0.117 drwxr-xr-x 2 root root 4096 8月 3 07:36 192.168.0.124 drwxr-xr-x. 7 root root 4096 7月 30 22:00 sysconfig drwxr-xr-x 3 root root 4096 8月 3 07:16 test [root@nfs-server test]# cat /tmp/192.168.0.117/test1.log 117 [root@nfs-server test]# cat /tmp/192.168.0.124/test1.log 124

⑤pnuke 並行在遠程主機殺進程:

將遠端服務器nginx進程殺死

[root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "lsof -i:80" #查看遠端nginx狀態
[1] 07:44:16 [SUCCESS] root@192.168.0.117:22
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   2739  root    7u  IPv4  17042      0t0  TCP *:http (LISTEN)
nginx   2740 nginx    7u  IPv4  17042      0t0  TCP *:http (LISTEN)
[2] 07:44:16 [SUCCESS] root@192.168.0.124:22
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   2665  root    6u  IPv4  16143      0t0  TCP *:http (LISTEN)
nginx   2666 nginx    6u  IPv4  16143      0t0  TCP *:http (LISTEN)
[root@nfs-server test]# pnuke -p 2 -h /root/test/pssh.txt nginx #殺死遠端nginx進程
[1] 07:44:36 [SUCCESS] root@192.168.0.117:22
[2] 07:44:36 [SUCCESS] root@192.168.0.124:22
[root@nfs-server test]# pssh -p 2 -i -h /root/test/pssh.txt "lsof -i:80"
[1] 07:44:42 [FAILURE] root@192.168.0.124:22 Exited with error code 1
[2] 07:44:42 [FAILURE] root@192.168.0.117:22 Exited with error code 1

其實集羣管理的工具備不少,例如saltstack,puppet,func等,當機器數量比較多的時候,就須要考慮合適的工具來管理的,這樣工做效率就會大大提升,事半功倍。

相關文章
相關標籤/搜索