主頁(yè) > 知識(shí)庫(kù) > 使用PSSH批量管理Linux服務(wù)器

使用PSSH批量管理Linux服務(wù)器

熱門(mén)標(biāo)簽:美圖手機(jī) 智能手機(jī) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 檢查注冊(cè)表項(xiàng) 硅谷的囚徒呼叫中心 使用U盤(pán)裝系統(tǒng) 阿里云 網(wǎng)站建設(shè)

pssh是一款開(kāi)源的軟件,使用python實(shí)現(xiàn),用于批量ssh操作大批量機(jī)器;pssh是一個(gè)可以在多臺(tái)服務(wù)器上執(zhí)行命令的工具,同時(shí)支持拷貝文件,是同類(lèi)工具中很出色的;比起for循環(huán)的做法,我更推薦使用pssh!使用pssh的前提是:必須在本機(jī)與其他服務(wù)器上配置好密鑰認(rèn)證訪問(wèn)(即ssh信任關(guān)系)。

下面就說(shuō)下使用pssh進(jìn)行批量操作的記錄:

1)安裝pssh
可以yum直接安裝:

[root@bastion-IDC ~]# yum install -y pssh

2)pssh用法

[root@bastion-IDC ~]# pssh --help

-h 執(zhí)行命令的遠(yuǎn)程主機(jī)列表文件
-H user@ip:port 文件內(nèi)容格式[user@]host[:port]
-l 遠(yuǎn)程機(jī)器的用戶(hù)名
-p 一次最大允許多少連接
-o 輸出內(nèi)容重定向到一個(gè)文件
-e 執(zhí)行錯(cuò)誤重定向到一個(gè)文件
-t 設(shè)置命令執(zhí)行的超時(shí)時(shí)間
-A 提示輸入密碼并且把密碼傳遞給ssh(注意這個(gè)參數(shù)添加后只是提示作用,隨便輸入或者不輸入直接回車(chē)都可以)
-O 設(shè)置ssh參數(shù)的具體配置,參照ssh_config配置文件
-x 傳遞多個(gè)SSH 命令,多個(gè)命令用空格分開(kāi),用引號(hào)括起來(lái)
-X 同-x 但是一次只能傳遞一個(gè)命令
-i 顯示標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤在每臺(tái)host執(zhí)行完畢后
-I 讀取每個(gè)輸入命令,并傳遞給ssh進(jìn)程 允許命令腳本傳送到標(biāo)準(zhǔn)輸入

3)pssh實(shí)例說(shuō)明

[root@bastion-IDC ~]# cat hosts.txt    //列表文件內(nèi)的信息格式是“ip:端口”,如果本機(jī)和遠(yuǎn)程機(jī)器使用的ssh端口一致,則可以省去端口,直接用ip就行。不過(guò)建議還是將端口都帶上為好。
192.168.1.101:22
192.168.1.109:22
192.168.1.118:25791
192.168.1.105:25791
如上四臺(tái)機(jī)器放在一個(gè)列表文件hosts.txt內(nèi),本機(jī)已經(jīng)和這四臺(tái)機(jī)器做了ssh無(wú)密碼登陸的信任關(guān)系
注意:列表文件內(nèi)的機(jī)器必須提前和本機(jī)做好ssh信任關(guān)系,如果沒(méi)有做的話(huà),那么pssh批量執(zhí)行時(shí),輪到這臺(tái)沒(méi)有做信任關(guān)系的機(jī)器時(shí)就不會(huì)執(zhí)行。

a)批量執(zhí)行命令

[root@bastion-IDC ~]# pssh -h hosts.txt -l root -i 'uptime'
[1] 16:05:48 [SUCCESS] 192.168.1.105
03:03:25 up 79 days, 13:44, 0 users, load average: 0.04, 0.01, 0.00
[2] 16:05:48 [SUCCESS] 192.168.1.118
03:03:32 up 75 days, 15:27, 4 users, load average: 0.96, 0.74, 0.45
Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[3] 16:05:48 [SUCCESS] 192.168.1.109
03:03:25 up 61 days, 21:56, 2 users, load average: 0.02, 0.06, 0.18
Stderr: Address 192.168.1.102 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[4] 16:05:48 [SUCCESS] 192.168.1.101
16:03:17 up 35 days, 23:45, 1 user, load average: 0.03, 0.04, 0.01
Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

如果添加-A參數(shù),那么即使提前做了ssh信任關(guān)系,還是會(huì)提示輸入密碼!

[root@bastion-IDC ~]# pssh -h hosts.txt -l root -i -A 'uptime'
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:                          //注意這個(gè)參數(shù)添加后只是提示作用,可以在此隨便輸入或者不輸入直接回車(chē)都可以
[1] 16:08:25 [SUCCESS] 192.168.1.105
03:06:03 up 79 days, 13:46, 0 users, load average: 0.00, 0.00, 0.00
[2] 16:08:25 [SUCCESS] 192.168.1.109
03:06:03 up 61 days, 21:59, 2 users, load average: 0.00, 0.04, 0.15
Stderr: Address 192.168.1.102 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[3] 16:08:25 [SUCCESS] 192.168.1.101
16:05:54 up 35 days, 23:47, 1 user, load average: 0.00, 0.02, 0.00
Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[4] 16:08:25 [SUCCESS] 192.168.1.118
03:06:10 up 75 days, 15:29, 4 users, load average: 0.85, 0.78, 0.51
Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[root@bastion-IDC ~]# pssh -h hosts.txt -l root -i -t 10 -o /root/pssh.log 'uptime && date'
[1] 17:01:02 [SUCCESS] 192.168.1.109
03:58:33 up 79 days, 5:58, 1 user, load average: 0.00, 0.00, 0.00
Wed Feb 8 03:58:33 EST 2017
[2] 17:01:02 [SUCCESS] 192.168.1.105
03:58:40 up 79 days, 14:39, 1 user, load average: 0.00, 0.00, 0.00
Wed Feb 8 03:58:40 EST 2017
[3] 17:01:02 [SUCCESS] 192.168.1.101
16:58:31 up 36 days, 40 min, 1 user, load average: 0.10, 0.03, 0.01
Wed Feb 8 16:58:31 CST 2017
Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[4] 17:01:02 [SUCCESS] 192.168.1.118
03:58:47 up 75 days, 16:22, 3 users, load average: 0.20, 0.21, 0.31
Wed Feb 8 03:58:47 EST 2017
Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[root@bastion-IDC ~]# ll /root/pssh.log/
total 16
-rw-r--r--. 1 root root 100 Feb 8 17:01 192.168.1.101
-rw-r--r--. 1 root root 99 Feb 8 17:01 192.168.1.105
-rw-r--r--. 1 root root 99 Feb 8 17:01 192.168.1.109
-rw-r--r--. 1 root root 100 Feb 8 17:01 192.168.1.118

b)批量上傳文件或目錄(pscp.pssh命令)
批量上傳本地文件/mnt/test.file到遠(yuǎn)程服務(wù)器上的/tmp目錄:

[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt /mnt/test.file /tmp/
[1] 16:18:05 [SUCCESS] 192.168.1.105
[2] 16:18:05 [SUCCESS] 192.168.1.109
[3] 16:18:05 [SUCCESS] 192.168.1.101
[4] 16:18:05 [SUCCESS] 192.168.1.118

批量上傳本地文件/mnt/test.file、/mnt/aa.file、/mnt/bb.file到遠(yuǎn)程服務(wù)器上的/tmp目錄:

[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt /mnt/test.file /mnt/aa.file /mnt/bb.file /tmp/
[1] 16:22:50 [SUCCESS] 192.168.1.109
[2] 16:22:50 [SUCCESS] 192.168.1.105
[3] 16:22:50 [SUCCESS] 192.168.1.118
[4] 16:22:50 [SUCCESS] 192.168.1.101

或者:

[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt /mnt/{test.file,aa.file,bb.file} /tmp/
[1] 16:23:44 [SUCCESS] 192.168.1.109
[2] 16:23:44 [SUCCESS] 192.168.1.105
[3] 16:23:44 [SUCCESS] 192.168.1.101
[4] 16:23:44 [SUCCESS] 192.168.1.118

批量上傳本地目錄/mnt/zhong到遠(yuǎn)程服務(wù)器上的/tmp目錄(上傳目錄需要添加-r參數(shù)):

[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt -r /mnt/zhong /tmp/
[1] 16:19:36 [SUCCESS] 192.168.1.109
[2] 16:19:36 [SUCCESS] 192.168.1.105
[3] 16:19:36 [SUCCESS] 192.168.1.101
[4] 16:19:36 [SUCCESS] 192.168.1.118

批量上傳本地目錄/mnt/zhong、/mnt/aa、/mnt/vv到遠(yuǎn)程服務(wù)器上的/tmp目錄

[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt -r /mnt/zhong /mnt/aa /mnt/vv /tmp/
[1] 16:21:02 [SUCCESS] 192.168.1.105
[2] 16:21:02 [SUCCESS] 192.168.1.109
[3] 16:21:02 [SUCCESS] 192.168.1.101
[4] 16:21:02 [SUCCESS] 192.168.1.118

或者:

[root@bastion-IDC ~]# pscp.pssh -l root -h hosts.txt -r /mnt/{zhong,aa,vv} /tmp/
[1] 16:22:00 [SUCCESS] 192.168.1.109
[2] 16:22:00 [SUCCESS] 192.168.1.105
[3] 16:22:00 [SUCCESS] 192.168.1.101
[4] 16:22:00 [SUCCESS] 192.168.1.118

c)批量下載文件或目錄(pslurp命令)
批量下載服務(wù)器上的某文件到本地,不用擔(dān)心重名問(wèn)題,因?yàn)閜ssh已經(jīng)建立了以文件列表內(nèi)的ip為名稱(chēng)的目錄來(lái)存放下載的文件:

[root@bastion-IDC ~]# pslurp -l root -h hosts.txt /etc/hosts .
[1] 16:32:01 [SUCCESS] 192.168.1.109
[2] 16:32:01 [SUCCESS] 192.168.1.105
[3] 16:32:01 [SUCCESS] 192.168.1.101
[4] 16:32:01 [SUCCESS] 192.168.1.118
[root@bastion-IDC ~]# ll
total 123
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.101
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.105
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.109
drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.118
[root@bastion-IDC ~]# ll 192.168.1.101
total 4
-rw-r--r--. 1 root root 224 Feb 8 16:32 hosts
[root@bastion-IDC ~]# ll 192.168.1.109
total 4
-rw-r--r--. 1 root root 252 Feb 8 16:32 hosts
[root@bastion-IDC ~]# ll 192.168.1.105
total 4
-rw-r--r--. 1 root root 252 Feb 8 16:32 hosts
[root@bastion-IDC ~]# ll 192.168.1.118
total 4
-rw-r--r--. 1 root root 212 Feb 8 16:32 hosts

另外特別注意:
上面的批量下載操作,只能下載到本地的當(dāng)前目錄下,不能在命令中跟指定的路徑:

[root@bastion-IDC ~]# pslurp -l root -h hosts.txt /etc/hosts /mnt/
[1] 16:34:14 [FAILURE] 192.168.1.109 Exited with error code 1
[2] 16:34:14 [FAILURE] 192.168.1.105 Exited with error code 1
[3] 16:34:14 [FAILURE] 192.168.1.101 Exited with error code 1
[4] 16:34:14 [FAILURE] 192.168.1.118 Exited with error code 1

要想下載到本機(jī)的/mnt目錄下,正確的做法是先切換到/mnt目錄下,然后再執(zhí)行下載命令:(列表文件要跟全路徑)

[root@bastion-IDC ~]# cd /mnt/
[root@bastion-IDC mnt]# pslurp -l root -h /root/hosts.txt /etc/hosts ./
[1] 16:34:34 [SUCCESS] 192.168.1.109
[2] 16:34:34 [SUCCESS] 192.168.1.105
[3] 16:34:34 [SUCCESS] 192.168.1.118
[4] 16:34:34 [SUCCESS] 192.168.1.101
[root@bastion-IDC mnt]# ll
total 16
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.101
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.105
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.109
drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.118

上面是批量下載文件,要是批量下載目錄,只需要添加一個(gè)-r參數(shù)即可!

[root@bastion-IDC mnt]# pslurp -l root -h /root/hosts.txt -r /home/ ./
[1] 16:39:05 [SUCCESS] 192.168.1.109
[2] 16:39:05 [SUCCESS] 192.168.1.105
[3] 16:39:05 [SUCCESS] 192.168.1.101
[4] 16:39:05 [SUCCESS] 192.168.1.118
[root@bastion-IDC mnt]# ll 192.168.1.101
total 8
drwxr-xr-x. 6 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 224 Feb 8 16:38 hosts
[root@bastion-IDC mnt]# ll 192.168.1.*
192.168.1.101:
total 8
drwxr-xr-x. 6 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 224 Feb 8 16:38 hosts
192.168.1.105:
total 8
drwxr-xr-x. 4 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 252 Feb 8 16:38 hosts
192.168.1.109:
total 8
drwxr-xr-x. 4 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 252 Feb 8 16:38 hosts
192.168.1.118:
total 8
drwxr-xr-x. 3 root root 4096 Feb 8 16:39 home
-rw-r--r--. 1 root root 212 Feb 8 16:38 hosts

d)批量同步(prsync命令)
同步本機(jī)/mnt/test目錄下的文件或目錄到遠(yuǎn)程機(jī)器的/mnt/test路徑下

[root@bastion-IDC ~]# prsync -l root -h hosts.txt -r /mnt/test/ /mnt/test/
[1] 16:46:41 [SUCCESS] 192.168.1.109
[2] 16:46:41 [SUCCESS] 192.168.1.105
[3] 16:46:41 [SUCCESS] 192.168.1.118
[4] 16:46:41 [SUCCESS] 192.168.1.101

同步本機(jī)/mnt/test目錄下的文件或目錄到遠(yuǎn)程機(jī)器的/mnt路徑下

[root@bastion-IDC ~]# prsync -l root -h hosts.txt -r /mnt/test/ /mnt/
[1] 16:47:40 [SUCCESS] 192.168.1.109
[2] 16:47:40 [SUCCESS] 192.168.1.105
[3] 16:47:45 [SUCCESS] 192.168.1.101
[4] 16:47:46 [SUCCESS] 192.168.1.118

注意:
上面批量同步目錄操作是將本機(jī)對(duì)應(yīng)目錄數(shù)據(jù)同步到遠(yuǎn)程機(jī)器上,遠(yuǎn)程機(jī)器上對(duì)于目錄下多余的文件也會(huì)保留(不會(huì)刪除多余文件)

同理,批量同步文件操作,去掉-r參數(shù),
注意:同步文件的時(shí)候,其實(shí)就是完全覆蓋,遠(yuǎn)程機(jī)器對(duì)應(yīng)文件內(nèi)的文件會(huì)被全部替換!
如下:
同步本機(jī)的/mnt/test/file文件內(nèi)容到遠(yuǎn)程服務(wù)器/mnt/test/file文件內(nèi)

[root@bastion-IDC ~]# prsync -l root -h hosts.txt /mnt/test/file /mnt/test/file
[1] 16:53:54 [SUCCESS] 192.168.1.109
[2] 16:53:54 [SUCCESS] 192.168.1.105
[3] 16:53:54 [SUCCESS] 192.168.1.101
[4] 16:53:54 [SUCCESS] 192.168.1.118
[root@bastion-IDC ~]# prsync -l root -h hosts.txt /mnt/test/file /mnt/aaa
[1] 16:54:03 [SUCCESS] 192.168.1.109
[2] 16:54:03 [SUCCESS] 192.168.1.105
[3] 16:54:03 [SUCCESS] 192.168.1.101
[4] 16:54:04 [SUCCESS] 192.168.1.118

e)批量kill遠(yuǎn)程機(jī)器上的進(jìn)程(pnuke命令)
比如批量kill掉遠(yuǎn)程機(jī)器上的nginx進(jìn)程

[root@bastion-IDC ~]# pnuke -h hosts.txt -l root nginx
[1] 17:09:14 [SUCCESS] 192.168.1.109
[2] 17:09:14 [SUCCESS] 192.168.1.105
[3] 17:09:15 [SUCCESS] 192.168.1.118
[4] 17:09:15 [SUCCESS] 192.168.1.101

標(biāo)簽:黃山 懷化 煙臺(tái) 山南 湖北 通遼 湘潭 賀州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用PSSH批量管理Linux服務(wù)器》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266