天天看點

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

一、批量管理

1.配置問密碼登陸

[root@localhost ~]# ssh-keygen -t dsa(生成密鑰)

generating public/private dsa key pair.

enter file in which to save the key (/root/.ssh/id_dsa): 

created directory '/root/.ssh'.

enter passphrase (empty for no passphrase): 

enter same passphrase again: 

your identification has been saved in /root/.ssh/id_dsa.

your public key has been saved in /root/.ssh/id_dsa.pub.

the key fingerprint is:

21:e3:80:d8:34:10:6c:26:8b:4c:72:fc:02:26:e5:8d [email protected]

[root@localhost ~]#

[root@localhost ~]# ssh-copy-id -i .ssh/id_dsa.pub 119.147.146.245(拷貝公鑰到要登陸的伺服器)

15

the authenticity of host '119.147.146.245 (119.147.146.245)' can't be established.

rsa key fingerprint is ba:7d:60:44:63:6c:f6:6a:9b:4b:ad:94:1b:98:2b:58.

are you sure you want to continue connecting (yes/no)? yes

warning: permanently added '119.147.146.245' (rsa) to the list of known hosts.

[email protected]'s password: 

now try logging into the machine, with "ssh '119.147.146.245'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@localhost ~]# ssh 119.147.146.245 ifconfig(遠端檢視單台伺服器的ip)

eth0      link encap:ethernet  hwaddr 00:50:56:88:0f:01  

          inet addr:119.147.146.245  bcast:119.147.146.255  mask:255.255.255.0

          inet6 addr: fe80::250:56ff:fe88:f01/64 scope:link

          up broadcast running multicast  mtu:1500  metric:1

          rx packets:18264423 errors:0 dropped:0 overruns:0 frame:0

          tx packets:1716785 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          rx bytes:7166025790 (6.6 gib)  tx bytes:162270128 (154.7 mib)

          base address:0x2000 memory:d8920000-d8940000

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

[root@localhost tmp]# echo 119.147.146.245 >>iplist(生成ip清單)

[root@localhost tmp]# echo 119.147.146.249 >>iplist

[root@localhost tmp]# echo 119.147.146.248 >>iplist

[root@localhost tmp]# vi manger.sh(撰寫腳本)

#!/bin/bash

for ip in `cat iplist`

do

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

        ssh $ip $1

done

[root@localhost tmp]# sh manger.sh df -h(遠端檢視多台伺服器的硬碟資訊)

119.147.146.245----------------------------------------

filesystem           1k-blocks      used available use% mounted on

/dev/mapper/volgrouproot-logvolroot

                      14093368   3488436   9877484  27% /

/dev/sda1               101086     19012     76855  20% /boot

tmpfs                   513468         0    513468   0% /dev/shm

/dev/sda5             53050708    185320  50170488   1% /mfsdata1

/dev/sda6             39065388    968620  36112344   3% /mfsdata2

/dev/sda7             39998020    968620  36997584   3% /mfsdata3

119.147.146.249----------------------------------------

                      10125560   3941852   5661060  42% /

/dev/mapper/volgroupdata-logvoldata

                      14220336    553532  12932804   5% /data

/dev/mapper/volgroupdata-logvollogs

                       4951688    141196   4554904   4% /data/logs

/dev/sda1               101086     12922     82945  14% /boot

tmpfs                   513068         0    513068   0% /dev/shm

119.147.146.248----------------------------------------

                      10125560   4159936   5442976  44% /

                      14220336    189528  13296808   2% /data

                       4951688    141208   4554892   4% /data/logs

/dev/sda1               101086     21349     74518  23% /boot

[root@localhost tmp]# sh manger.sh free -m(遠端檢視多台伺服器的記憶體使用情況)

             total       used       free     shared    buffers     cached

mem:       1026936     885652     141284          0      68036     711216

-/+ buffers/cache:     106400     920536

swap:      4194296        108    4194188

mem:       1026140    1016284       9856          0     154076     690928

-/+ buffers/cache:     171280     854860

swap:      1052248         88    1052160

mem:       1026140     977656      48484          0      48076     758976

-/+ buffers/cache:     170604     855536

swap:      1052248          8    1052240

[root@localhost tmp]# sh manger.sh "useradd admin"(批量建使用者)

[root@localhost tmp]# sh manger.sh "echo 'king+5688'|passwd --stdin admin"(批量更改密碼)

changing password for user admin.

passwd: all authentication tokens updated successfully.

二、批量分發

[root@localhost tmp]# cat fenfa.sh (撰寫腳本)

        scp -r -p $1 $ip $2

[root@localhost tmp]# sh fenfa.sh /etc /tmp/(把/etc目錄拷貝到多台遠端伺服器上的/tmp下

[root@localhost tmp]# ls(驗證)

etc

三、通過普通使用者實作遠端分發檔案,遠端管理

1.建立普通使用者admin 密碼:123456

2.生成密鑰拷貝到對端

[admin@localhost ~]$ ssh-keygen -t dsa

enter file in which to save the key (/home/admin/.ssh/id_dsa): 

created directory '/home/admin/.ssh'.

your identification has been saved in /home/admin/.ssh/id_dsa.

your public key has been saved in /home/admin/.ssh/id_dsa.pub.

00:1f:1c:fa:a2:e0:79:cc:a4:24:92:47:de:3e:bf:fb [email protected]

[admin@localhost .ssh]$ ssh-copy-id -i id_dsa.pub 119.147.146.245

10

[email protected]'s password: 

3.撰寫腳本(和上面的一樣)

[admin@localhost ~]$ ls

fenfa.sh  iplist  manger.sh

4.測試

拷貝本地的/etc目錄到多台遠端伺服器的/tmp目錄下

[admin@localhost ~]$ su - root /home/admin/fenfa.sh /etc /tmp

password: (輸入root密碼)

mime.types                                                       100%   14kb  13.8kb/s   00:00    

virc                                                             100% 1533     1.5kb/s   00:00    

.bashrc                                                          100%  124     0.1kb/s   00:00    

.bash_profile                                                    100%  176     0.2kb/s   00:00    

.bash_logout                                                     100%   33     0.0kb/s   00:00    

.zshrc                                                           100% 658     0.6kb/s   00:00  

檢視遠端伺服器記憶體使用情況

[admin@localhost ~]$ su - root /home/admin/manger.sh "free -m"

mem:          1002        966         36          0         86        756

-/+ buffers/cache:        123        879

swap:         4095          0       4095

mem:          1002        951         50          0        146        676

-/+ buffers/cache:        129        872

swap:         1027          0       1027

mem:          1002        962         40          0         60        739

-/+ buffers/cache:        162        839

其實用普通使用者做批量管理,批量分發就是多了一步輸入root的密碼的步驟,這樣做的目的是為了安全