批量主机管理工具_python
一、主要功能
本服务器管理工具主要实现以下两种功能:
(1)批量执行服务器操作命令
(2)批量上传下载文件
二、程序目录
三、代码
start.py:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #/usr/bin/env python 2 #_*_ coding:utf-8 _*_ 3 #Author:chenxiaozan 4 import re,os,sys 5 import paramiko 6 BASE_DIR=os.path.dirname(os.path.dirname(__file__)) 7 split=os.path.sep 8 sys.path.append(BASE_DIR) 9 10 from core import servermanage 11 12 13 if __name__ == '__main__': 14 obj=servermanage.Servermanage() 15 obj.interaction()
hosts:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #_*_coding:utf-8 _*_ 2 #[gruop_name] 3 #host_ip password 4 5 [test] 6 192.168.1.1 123456 7 192.168.102.1 123456 8 9 [test3] 10 192.168.102.1 123456
settings:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #/usr/bin/env python 2 #_*_ coding:utf-8 _*_ 3 #Author:chenxiaozan 4 import re,os,sys 5 6 BASE_DIR=os.path.dirname(os.path.dirname(__file__)) 7 split=os.path.sep 8 sys.path.append(BASE_DIR+split) 9 10 11 12 #主机配置文件 13 host_file=BASE_DIR+split+'conf'+split+'hosts'
msg.py:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #/usr/bin/env python 2 #_*_ coding:utf-8 _*_ 3 #Author:chenxiaozan 4 import re,os,sys 5 BASE_DIR=os.path.dirname(os.path.dirname(__file__)) 6 split=os.path.sep 7 sys.path.append(BASE_DIR+split) 8 from conf import settings 9 10 def read_msg(): 11 ''' 12 读取主机配置文件 13 :return: 14 ''' 15 count = 1 16 f=open(settings.host_file,'r') 17 host_data_dict={} 18 for line in f: 19 line=line.strip() 20 if line!='': #去除空行 21 begin_wiht=re.match('#',line) #去除注释行 22 if not begin_wiht: 23 group=re.findall('\[(.+)\]', line) #判断是否是组名 24 if group: 25 group_name=group[0] 26 host_data_dict[group_name] = {} 27 else: 28 line_list=re.split(' +',line) 29 if len(line_list)==2: 30 if line_list[0] not in host_data_dict[group_name]: 31 #print('本行可添加到主机信息') 32 host_data_dict[group_name][line_list[0]]=line_list[1] 33 elif len(line_list)==0: 34 pass 35 else : 36 print('hosts文件第%s行服务器信息为空行或填写格式错误,本行相关服务器不执行本次操作'%count) 37 count+=1 38 return host_data_dict 39 40 41 42 43 if __name__ == '__main__': 44 45 host_data_dict=read_msg() 46 print(host_data_dict)
servermanage.py:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #/usr/bin/env python 2 #_*_ coding:utf-8 _*_ 3 #Author:chenxiaozan 4 import re,os,sys 5 import paramiko 6 BASE_DIR=os.path.dirname(os.path.dirname(__file__)) 7 split=os.path.sep 8 sys.path.append(BASE_DIR+split) 9 import threading 10 import paramiko 11 12 from conf import settings 13 from core import msg 14 15 16 class Servermanage(object): 17 def __init__(self): 18 self.host_data_dict=msg.read_msg() 19 self.lock=threading.Lock() 20 def interaction(self): 21 print('根据您填写的hosts文件,有以下分组:') 22 for i in self.host_data_dict: 23 print(i) 24 while True: 25 self.group_choice=input('请选择你需要操作的组名>>').strip() 26 if self.group_choice in self.host_data_dict: 27 print('您所选择的主机组拥有以下主机:') 28 for host_ip in self.host_data_dict[self.group_choice]: 29 print(host_ip) 30 print('''用法: 31 1、直接输入linux命令:例如 df (注:不能执行动态命令,如top) 32 2、上传下载文件: put 上传文件 目标文件名 (注:本地上传先存放数据在db文件夹) 33 get 下载文件 目标文件名 (注:远程下载存放数据在db文件夹,并命名追加ip) 34 ''') 35 while True: 36 self.cmd_input=input('请输入命令(r返回,q退出)>>').strip() 37 if self.cmd_input=='': 38 continue 39 elif self.cmd_input=='r': 40 break 41 elif self.cmd_input=='q': 42 exit() 43 else: 44 self.cmd_input_list=self.cmd_input.split(' ') 45 if self.cmd_input_list[0]=='put' or self.cmd_input_list[0]=='get' and len(self.cmd_input_list)==3: 46 self.mul_thread(self.scp) 47 elif self.cmd_input_list[0]=='put' or self.cmd_input_list[0]=='get' and len(self.cmd_input_list)!=3: 48 print('您输入的scp命令不符合格式') 49 50 else: 51 self.mul_thread(self.cmd) 52 53 elif self.group_choice=='': 54 continue 55 else: 56 print('您输入的组名不存在') 57 58 59 def mul_thread(self,run): 60 thread_list=[] 61 for ip in self.host_data_dict[self.group_choice]: 62 t=threading.Thread(target=run,args=(ip,self.host_data_dict[self.group_choice][ip])) 63 t.start() 64 thread_list.append(t) 65 for i in thread_list: 66 i.join() 67 68 def cmd(self,ip,passwd): 69 ssh=paramiko.SSHClient() 70 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) 71 ssh.connect(hostname=ip,port=22,username='root',password=passwd) 72 stdin, stdout, stderr = ssh.exec_command(command=self.cmd_input) 73 res = stdout if stdout else stderr 74 self.lock.acquire() 75 print('===========%s==========='%ip) 76 print(res.read().decode()) #在这里进行decode()的时候不同服务器可能出现不同code 77 self.lock.release() 78 ssh.close() 79 80 def scp(self,ip,passwd): 81 db_path=BASE_DIR+split+'db' 82 transport=paramiko.Transport((ip,22)) 83 transport.connect(username='root',password=passwd) 84 sftp=paramiko.SFTPClient.from_transport(transport) 85 try: 86 if self.cmd_input_list[0]=='get': 87 sftp.get(self.cmd_input_list[1],'%s%s%s_%s'%(db_path,split,self.cmd_input_list[2],ip)) 88 else: 89 sftp.put('%s%s%s'%(db_path,split,self.cmd_input_list[1]), self.cmd_input_list[2]) 90 except Exception as e: 91 print('请检查要上传或下载的文件存不存在') 92 93 if __name__ == '__main__': 94 obj=Servermanage() 95 obj.interaction()
readme:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #/usr/bin/env python 2 #_*_ coding:utf-8 _*_ 3 #Author:chenxiaozan 4 5 功能说明: 6 本服务器管理工具主要实现以下两种功能: 7 (1)批量执行服务器操作命令 8 (2)批量上传下载文件 9 10 使用说明: 11 (1)批量执行命令时,只需要直接敲如命令即可。不能执行动态命令,如top 12 (2)使用批量上传下载文件时,上传之前需要将你要上传的文件放在db文件夹内。下载文件会统一下载在db文件夹内,并将文件名后面追加主机ip进行区分