天天看點

批量管理伺服器,批量分發檔案

一、建立信任關系

192.168.0.248 為管理機

192.168.0.244 192.168.0.249 為遠端linux伺服器

1、在管理機生成證書

[root@centos-5 ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

d0:c6:81:a5:0a:74:5b:35:1c:b5:69:ba:5a:6f:4a:df [email protected]

2、将管理機上的公鑰傳送到各遠端伺服器(如果已經存在/root/.ssh/authorized_keys,則需要在後面追加公鑰内容)

如遠端伺服器更改了預設的ssh端口号,就使用scp -P 17173,17173為端口号

scp .ssh/id_rsa.pub 192.168.0.244:/root/.ssh/authorized_keys

scp .ssh/id_rsa.pub 192.168.0.249:/root/.ssh/authorized_keys

管理機與遠端主機信任關系建立完畢 

3、測試

[root@centos-5 ~]# ssh 192.168.0.244 ifconfig #檢視遠端伺服器ip

eth0      Link encap:Ethernet  HWaddr 00:0C:29:18:B7:2A

          inet addr:192.168.0.244  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe18:b72a/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:11326 errors:0 dropped:0 overruns:0 frame:0

          TX packets:357 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:1593328 (1.5 MiB)  TX bytes:56869 (55.5 KiB)

遠端檢視多台伺服器的資訊箱

[root@centos-5 ~]# cat iplist.txt

192.168.0.244

192.168.0.249

[root@localhost tmp]# vi manger.sh

#!/bin/bash

for ip in `cat iplist.txt`

do

echo "$ip---------------"

ssh $ip $1

done

[root@centos-5 ~]# sh manage.sh df -h

192.168.0.244---------------

檔案系統               1K-塊        已用     可用 已用% 挂載點

/dev/sda2              7936288   6812476    714156  91% /

/dev/sda5             20401592  13175824   6172712  69% /data

/dev/sda1               101086     11985     83882  13% /boot

tmpfs                   258304         0    258304   0% /dev/shm

192.168.0.249---------------

/dev/sda2              6940516   2657024   3925244  41% /

/dev/sda5             11235340   4991868   5663528  47% /data

/dev/sda1               101086     18189     77678  19% /boot

tmpfs                   512964         0    512964   0% /dev/shm

3.批量分發

scp -r -p $1 $ip:$2

sh fenfa.sh /data/backup/ /tmp/

參考文檔:http://navyaijm.blog.51cto.com/4647068/835309

其實我們也可以去192.168.0.244 192.168.0.249 遠端linux伺服器生成密鑰對,然後把私鑰192.168.0.244 和192.168.0.249的私鑰拷貝到192.168.0.248,分别儲存為/root/.ssh/244、/root/.ssh/249,然後通過

ssh -p 36900 -i /root/.ssh/244 192.168.0.244 ifconfig

ssh -p 36900 -i /root/.ssh/249 192.168.0.249 ifconfig

這個是今天看到别人這樣搞的。關鍵是公鑰必須要放到要登入的伺服器,不管是在服務端,還是用戶端生成的密鑰。

今天從香港機房173登入180

1.首先去180生成密鑰對檔案,authorized_keys不用說了,按照上面的方法搞,然後把密鑰檔案

vi /root/.ssh/id_rsa 的内容拷貝到173上vi /root/.ssh/jiucang180

chmod 600 /root/.ssh/jiucang180

2. ssh -p 36009 -i /root/.ssh/jiucang180 192.168.0.180 ifconfig

搞定收工,呵呵。

以下是從20同步到39伺服器:

在39伺服器執行:

1.ssh-keygen -t rsa

Enter file in which to save the key (/root/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase): 

Enter same passphrase again: 

cd .ssh/

mv id_rsa.pub authorized_keys

chmod  600 authorized_keys

2.到20伺服器執行以下操作

cd /root/.ssh/

vim sjhl_39,拷貝39上面的私鑰檔案/root/.ssh/id_rsa内容到sjhl_39裡面去

執行提示權限太高

 ssh -p 3600 -i 39 192.168.1.39 ifconfig

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @

Permissions 0644 for 'sjhl_39' are too open.

修改為600, chmod 600 39,再執行可以了。