Linux 系统批量管理工具介绍,如何实现对一万台服务器的同时批量管理?

                              本帖最后由 alex3714 于 2011-01-11 10:58 编辑

 

 

 

 

 

做Linux系统管理以来,由于维护过比较大的网络,例如在飞信做支持的时候,面对上千台的服务器,有时候可能要对每台机子打一个补丁,或者是修改一个文件,如果只有10台服务器,那一一修改也就罢了,但是如果让你一台一台的登录1000台服务器只是为了去改一个文件,那一定痛苦死,并且效率低下,没有任何技术含量,如果一直做这种工作,那被称为IT民工也不能怪别人了,因为我一直想找一个可以批量管理的工具,后来发现了两种方式可以实现:

 

 

1.通过SSH密钥认证,这样登录到远程机器上后就不需要输入密码了,这样就可以通过脚本去批量登录到远程服务器并且修改你想要文件或操作等,但是这有一个缺点,就是这个在管理端的私钥你一定要保存好,万一管理服务器系统重装或其它原因导致私钥丢失,那你就没办法登录远程机器了。还有,如果需要管理的机器更改了IP,那你还得重新把公钥COPY到那台机子上,这样管理起来可能不是那么灵活。

 

 

2.通过expect 工具进行批量管理,expect工具很强大,可以实现交互式管理,比如如果你想改密码,输入passwd命令后,系统会提示你输入New Password: ,如果使用普通脚本的话,那你是没办法进行交互式的。但是expect就可以做到检测系统的返回值并且根据返回的提示来自动交互,如下例:

 

 

#!/usr/bin/expect -fset ipaddress [lindex $argv 0] #设置命令行参数

 

set passwd [lindex $argv 1]  #参数1 为password

 

set ipaddress [lindex $argv 0] #参数 0 为IP 地址

 

set timeout 1000

 

spawn ssh root@$ipaddress

 

expect {

 

        "yes/no" { send "yes\r";exp_continue }

 

        "Password:" { send "$passwd\r" }    #自动输入密码

 

}

 

expect "hknp"

 

send "/etc/init.d/heartbeat stop \r"  #停止一个程序

 

expect "hknp"

 

send "exit\r"   #退出系统

 

expect eof

 

exit

 

以上脚本通过命令: expect ha-switch.exp 192.168.193.133 ‘123DDFD’执行 ,其中123DDFD 就是133这台机子的root密码,如果你的一百台机子都是一样的密码,你就可以写个简单的批量脚本来登录所有的机子并停止一个程序,如下:

 

#!/bin/bash

 

for i in $(seq 100 200);

 

do

 

IP = "192.168.193.$i"

 

expect  ha-switch.exp $IP '123DDFD'

 

done

 

这样此脚本就会调用ha-switch.exp脚本并登录到192.168.193.100-200的机器上分别执行"/etc/init.d/heartbeat stop 命令了。

 

很强大吧,但使通过我使用的经验,我觉得expect 有个缺点就是有慢,因为它是一台一台的去登录 然后执行命令,因为有的时候由于DNS解析或什么原因 ,通过SSH登录到一台机子时可能需要等待30s才能登录进去,假如1000台机子的话那就需要50分钟才能完成在所有机器上的操作,对于要求在1分钟内实现数千台机器执行相同操作的需要来讲这显然达不到要求。

 

以上两种方法各有利弊,我个人建议在50-100台的小网络中可以考虑使用SSH认证或expect的方法。但是想像一下,如果我有一万台机器 ,分别处于全国各地不同的网络中,要求我在1分钟内更改所有机器的root密码,显然以上两种方法均是做不到的,当然有这样大型网络的公司中国也并不多见,但是从技术的角度上来讲这还是有一定挑战性的,由于在网上一直找不到这样的工具,我就自己索性写了一个,经过多天的努力,终于将这个批量管理工具写完了,此工具是用的Python写的,基于socket server的模式,即需要在所有的需要管理的服务器上启动一个客户端(可能好多朋友不太喜欢这种还需要装客户端的东东),客户端会开启一个端口,你的管理服务器就是通过此端口与被管理端通信,然后对被管理端进行操作,你可以远程修改密码,查看系统信息,内存情况等操作,操作结果会在你的管理端实现显示出来(这也是我比较喜欢的地方,就跟在本地操作命令一样)。并且还可以向远程服务器批量COPY文件,下面我就把这个工具在使用过程中的一些截图列出来:

 

 

bjnppb01:~/scripts/python_scripts/Remote_management_tool # python server.py  ##执行管理端

 

 

#############################################################################

 


#       RMT(Remote Management tool)                                               #
#                                                                             #
#       Version 1.0,2010-12-29                                              #
#       Author:Alex Li                                                              #
#       Email:lijie3721@126.com,QQ:317828332                                     #

 

##################################################################################

 

 


please slect the following menu: #功能列表
                0 list servers
                1 Scan agent status
                2 login to remote server
                3 Reboot all the remote servers(does't support)
                4 Upload server list
                5 excute command on all the aviliable servers
                6 change password for all the servers
                7 exit
Please enter the slected number:0  #列出你要管理的远程服务器
192.168.193.133
192.168.193.134
192.168.193.142
192.168.193.137
192.168.193.143
192.168.193.144
192.168.193.144
192.168.193.145
192.168.193.146
192.168.193.173
192.168.193.174
192.168.193.175
192.168.193.176
192.168.193.177
192.168.193.178
192.168.193.179
please slect the following menu:
                0 list servers
                1 Scan agent status
                2 login to remote server
                3 Reboot all the remote servers(does't support)
                4 Upload server list
                5 excute command on all the aviliable servers
                6 change password for all the servers
                7 exit
Please enter the slected number:1 #扫描服务器 状态
192.168.193.133  running
192.168.193.134  running
192.168.193.142  down  
192.168.193.137  running
192.168.193.143  running
192.168.193.144  running
192.168.193.144  running
192.168.193.145  running
192.168.193.146  running
192.168.193.173  running
192.168.193.174  running
192.168.193.175  running
192.168.193.176  down
192.168.193.177  running
192.168.193.178  running
192.168.193.179  running
Please enter the slected number:2 #登录 特定的一台服务器
Please enter the remote server IP: 192.168.193.178 #输入IP
You have successfully login to the remote server, now you can run most of the system command in this

 

mode ,but do not suggest
you to run the command such as top,tail -f,because right now I haven't find a way to support the continuous

 

data output
Please input the command:uname -a #登录成功后执行命令
Received log from /root/Remote_management_tool/192.168.193.178.log


##########################################################

 


Linux bjnpmdf2 2.6.16.60-0.54.5-smp #1 SMP Fri Sep 4 01:28:03 UTC 2009 x86_64 x86_64 x86_64 GNU/Linux  #返回的结果

 


####################################################################################


Please input the command:hostname #命令
Received log from /root/Remote_management_tool/192.168.193.178.log


##########################################################


bjnpmdf2   #返回的结果


####################################################################################

 


Please input the command:
Please enter the slected number:4  #上传服务器IP列表
Please enter the full path of your file: ../server.txt  # 服务列器表文件文件名


192.168.193.100
192.168.193.101
192.168.193.102
192.168.193.103
192.168.193.104
192.168.193.105
192.168.193.106
192.168.193.107
192.168.193.108
192.168.193.109
192.168.193.110
192.168.193.111
192.168.193.112
192.168.193.113
192.168.193.114
192.168.193.115
192.168.193.116
192.168.193.117
192.168.193.118
192.168.193.119
192.168.193.120
192.168.193.121
192.168.193.122
192.168.193.123
192.168.193.124
192.168.193.125
192.168.193.126
192.168.193.127
192.168.193.128
192.168.193.129
192.168.193.130
192.168.193.131
192.168.193.132
192.168.193.133
192.168.193.134
Adding uploaded list to Server list.########################## done.

 


please slect the following menu:
                0 list servers
                1 Scan agent status
                2 login to remote server
                3 Reboot all the remote servers(does't support)
                4 Upload server list
                5 excute command on all the aviliable servers
                6 change password for all the servers
                7 exit
Please enter the slected number:0  #列表上传后你再选择0就能看到你上传到的列表已经在管理列表中了

 


192.168.193.100
192.168.193.101
192.168.193.102
192.168.193.103
192.168.193.104
192.168.193.105
192.168.193.106
192.168.193.107
192.168.193.108
192.168.193.109
192.168.193.110
192.168.193.111
192.168.193.112
192.168.193.113
192.168.193.114
192.168.193.115
192.168.193.116
192.168.193.117
192.168.193.118
192.168.193.119
192.168.193.120
192.168.193.121
192.168.193.122
192.168.193.123
192.168.193.124
192.168.193.125
192.168.193.126
192.168.193.127
192.168.193.128
192.168.193.129
192.168.193.130
192.168.193.131
192.168.193.132
192.168.193.133
192.168.193.134
192.168.193.137
192.168.193.142
192.168.193.143
192.168.193.144
192.168.193.145
192.168.193.146
192.168.193.173
192.168.193.174
192.168.193.175
192.168.193.176
192.168.193.177
192.168.193.178
192.168.193.179
please slect the following menu:
                0 list servers
                1 Scan agent status
                2 login to remote server
                3 Reboot all the remote servers(does't support)
                4 Upload server list
                5 excute command on all the aviliable servers
                6 change password for all the servers
                7 exit
Please enter the slected number: 5  #  ## 会登录 所有在运行了客户端的机器
It might will takes a few minutes to scan all the avialiable servers......
The fllowing servers are avaliable:
192.168.193.133
192.168.193.134
192.168.193.137
192.168.193.143
192.168.193.144
192.168.193.145
192.168.193.146
192.168.193.173
192.168.193.174
192.168.193.175
192.168.193.177
192.168.193.178
192.168.193.179
please input your command:  hostname ###此时你再输入的命令就是在所有的机器 上运行的了,看下面的输出结果


Received log from /root/scripts/python_scripts/192.168.193.133.log


##########################################################


bjnppb02


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.134.log


##########################################################


bjnpif01


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.137.log


##########################################################


bjnpbo01


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.143.log


##########################################################


bjnpeq04


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.144.log


##########################################################


bjnpeq05


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.145.log


##########################################################


bjnpif03


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.146.log


##########################################################


bjnpif04


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.173.log


##########################################################


bjnptest1


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.174.log


##########################################################


bjnptes2


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.175.log


##########################################################


bjnptes3


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.177.log


##########################################################


bjnpmdf1


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.178.log


##########################################################


bjnpmdf2


####################################################################################


Received log from /root/Remote_management_tool/192.168.193.179.log


##########################################################


bjnptes4


####################################################################################

 


这样就可以轻松的管理所有的机器了,考虑到第一次运行时需要在所有的机器上安装客户端,建议使用expect工具来一次批量安装,这样就可以一劳永逸了,有需要的同鞋请下载附件。 Remote_management_tool_v1.2.tar (30 KB)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值