def smtp_add(ip_list): ''' 接受前端传送额ip列表 然后将其添加到配置文件中 ''' response = BaseResponse() host_ip = ["10.58.47.39", "10.58.47.40"] try: ip_list = json.loads(ip_list) print "ip_____list", ip_list if len(ip_list) == 0: raise Exception(u'你总得输个IP地址吧!') error_ip = [] for ip in ip_list: if not ipv4_re.match(str(ip)): error_ip.append(str(ip)) # raise Exception(u'ip address error %s' % ip) print "error_ip_list", error_ip if len(error_ip) != 0: response.message = u'输入IP地址格式错误' return response smtp_cmd = make_add_smtp_cmd(ip_list) # smtp执行的命令 host_pwd_info = pwd_query.main(host_ip, [], "P") host_file_status = create_ansible_host_file(host_pwd_info, 'smtp_host') if not host_file_status.status: raise Exception('create ansible smtp host failed') ansible_obj = Ansible_API(host_file_status.data) ansible_obj.host = "smtp_host" run_cmd_resule = ansible_obj.shell_run(smtp_cmd) response.status = True response.data = run_cmd_resule except Exception, e: response.message = str(e.message)
def single_ping(ip_data): response = BaseResponse() try: ip_list = json.loads(ip_data) # 先将输入的空ip去除 for i, ip in enumerate(ip_list): if len(ip) == 0: ip_list.pop(i) # 检测ip输入不匹配 error_ip = [] for ip in ip_list: if not ipv4_re.match(ip): error_ip.append(ip) if len(error_ip) > 0: # 说明IP地址有输入错误的 返回错误 response.status = False response.message = u"IP地址输入错误 %s" % ",".join(error_ip) return response # ip地址输入正确,开始处理ping re_list = ping_host_ip.run_ping_by_ip(ip_list) response.data = re_list response.status = True return response except Exception, e: response.message = str(e) return response
def upload_budget_file(file_obj, file_dir): ''' 上传表格文件 ''' response = BaseResponse() try: # 保存上传的脚本 filename = file_obj.name file_path = file_dir + filename print "upload_file_path", file_path f = open(file_path, 'wb') for line in file_obj.chunks(): # 保存文件 f.write(line) f.close() response.status = True response.message = u'上传成功' return response except AttributeError: response.message = u'选择上传文件' return response except Exception, e: print "up____error", str(e) response.message = str(e) return response
def get_host_rules(data): ''' 获取主机下的所有iptables规则 :param data: :return: ''' response = BaseResponse() ip_list = data.get('ip_list', None) # 输入的IP地址 envir = data.get('envir', None) # 环境 value_list = [ip_list, envir] for i in value_list: if i is None: response.message = u'请检查提交的数据' return response ipv4_re = re.compile( r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$' ) error_ip = [] host_ip = ip_list.strip().split(",") for ip in host_ip: if not ipv4_re.match(ip): error_ip.append(ip) if len(error_ip) > 0: response.host_ip = error_ip response.ip_status = False response.message = u"IP地址出现错误!!!" return response set_host = list(set(host_ip)) # 将列表去重 pwd_check = pwd_query.main(set_host, [], envir) # 调用密码API 获取密码的列表 ***** if len(pwd_check) == 0: response.message = u"密码检测失败!!请检查所选环境和密码是否存在~" response.pwd_status = False return response re_list = [] # 存放每台IP的执行结果 for host_info in pwd_check: ip = host_info['ip'] pwd = host_info['pwd'] iptables_obj = execute_iptables.RunIptables([], ip, pwd) # 连接服务器对象 exe_re = iptables_obj.get_all_rules() # 获取规则的方法 re_list.append(exe_re) response.data = re_list response.status = True return response
def create_new_data(file_obj, file_path, filename): # 读取excel文件 # 将表格中的数据一一对应,生成数据列表 # 写入数据库 response = BaseResponse() try: import_data_list = [] new_file_obj = upload_idc_file(file_obj, filename, file_path) if not new_file_obj.status: response.message = new_file_obj.message return response all_data_list = read_excel_data(new_file_obj.data) for line in all_data_list: if type(line[18]) == float: line[18] = int(line[18]) one_obj2 = IDCPhysical( time_period=line[0], delivery_date=line[1], pro_type=line[2], ip=line[3], device_type=line[4], server_type=line[5], app_type=line[6], device_status=line[7], is_virtual=line[8], sn=line[9], brand=line[10], device_version=line[11], device_conf=line[12], own_person=line[13], pro_line1=line[14], pro_line2=line[15], pro_line3=line[16], pro_person=line[17], module=line[18], idc=line[19], cabinet=line[20], cabinet_status=line[21], ) import_data_list.append(one_obj2) IDCPhysical.objects.bulk_create(import_data_list) response.status = True response.data = "success" except Exception, e: print "eeeeeeeeeeeeeeeeeeerror", e response.message = str(e)
def handle_iptables_post_data(data): ''' 验证用户 根据type去寻找分配适应的处理方法 ''' response = BaseResponse() try: post_data = json.loads(data) # 提交的数据 check_response = check_user_info(post_data) # 验证用户的结果 if not check_response.status: # 验证失败 raise Exception('Authentication failed.') user = post_data['user'] rule_type = post_data['type'] # envir = post_data['envir'] if not rule_type: # 验证失败 raise Exception("'rule type' cannot be empty.") # if not envir: # 验证失败 # print "'envir' cannot be empty." # raise Exception("'envir' cannot be empty.") rule_status = fix_rule_type(post_data['rules'], rule_type.strip(), user) if not rule_status.status: raise Exception(rule_status.message) response.other = rule_status.other response.data = rule_status.data response.status = True except Exception, e: print "handle_iptables_post_data Error", e response.message = e.message
def run_iptables_cmd(host_ip, rule_cmd_list): ''' 查询主机列表的用户名和密码 调用ansible API执行iptables的命令 ''' response = BaseResponse() try: pwd_info_list = pwd_query_no_envir.main(host_ip, []) # 查询host 密码 用户名 对结果要进行对比分析 no_found_ip_list = check_query_reuslt(host_ip, pwd_info_list) host_file_status = create_iptables_host_file(pwd_info_list) # 创建ansible host文件 if not host_file_status: raise Exception("ansible iptables host file create failed") if isinstance(rule_cmd_list, list): rule_cmd = make_iptables_cmd(rule_cmd_list) else: rule_cmd = rule_cmd_list ansible_obj = Ansible_API(host_file_status.data) ansible_obj.host = 'iptables' run_cmd_result = ansible_obj.shell_run(rule_cmd) for k, e in run_cmd_result.items(): print k, "--------------------", e if len(no_found_ip_list) != 0: response.other = "host %s is not found password" % " ".join(no_found_ip_list) response.status = True response.data = run_cmd_result except Exception, e: print "run_iptables_cmd Error: ", e response.message = str(e)
def virtual_can_not_virtual(): response = BaseResponse() try: ret = {} can_virtual_online = collect_handle.get_can_virtual_online() # 可虚拟化 在线 can_virtual_offline = collect_handle.get_can_virtual_offline( ) # 可虚拟化 下线 can_virtual_scrap = collect_handle.get_can_virtual_scrap() # 可虚拟化 报废 not_virtual_online = collect_handle.get_not_virtual_online( ) # 不可虚拟化 在线 not_virtual_offline = collect_handle.get_not_virtual_offline( ) # 不可虚拟化 在线 not_virtual_scrap = collect_handle.get_not_virtual_scrap() # 不可虚拟化 报废 all_can_virtual = can_virtual_online + can_virtual_offline + can_virtual_scrap all_not_virtual = not_virtual_online + not_virtual_offline + not_virtual_scrap ret['can_virtual_online'] = can_virtual_online ret['can_virtual_offline'] = can_virtual_offline ret['can_virtual_scrap'] = can_virtual_scrap ret['not_virtual_online'] = not_virtual_online ret['not_virtual_offline'] = not_virtual_offline ret['not_virtual_scrap'] = not_virtual_scrap ret['all_can_virtual'] = all_can_virtual ret['all_not_virtual'] = all_not_virtual response.data = ret response.status = True except Exception, e: response.message = e.message
def sql_data(): response = BaseResponse() try: ret = {} all_app_type_count = collect_handle.get_all_apply_type() # 所有应用类型的个数 all_sql_count = collect_handle.get_all_sql() # 所有数据库个数 all_app_virtual = collect_handle.get_all_app_virtual() # 应用宿主机 all_app_no_virtual = collect_handle.get_all_app_no_virtual() # 应用非宿主机 all_device_can_not_use = collect_handle.get_no_user_device_status( ) # 获取下线报废的个数 all_count = all_sql_count + all_app_virtual + all_app_no_virtual + all_device_can_not_use sql_percent = data_percent(all_sql_count, all_app_type_count - all_device_can_not_use) app_percent = data_percent(all_app_virtual, all_count - all_device_can_not_use) # ret['all_app_type_count'] = all_app_type_count # ret['all_app_count'] = all_app_count ret['all_sql_count'] = all_sql_count ret['all_app_virtual'] = all_app_virtual ret['all_app_no_virtual'] = all_app_no_virtual ret['all_device_can_not_use'] = all_device_can_not_use ret['all_count'] = all_count ret['sql_percent'] = sql_percent ret['app_percent'] = app_percent response.data = ret response.status = True except Exception, e: response.message = e.message
def fix_rule_type(rule_data_list, rule_type, user): ''' 处理iptables字典数据 生成规则 ''' response = BaseResponse() try: if rule_type == 'easy': # 如果规则是简单快速规则 easy_result = easy_rules(rule_data_list, user) if not easy_result.status: raise Exception(easy_result.message) response.other = easy_result.other response.data = easy_result.data elif rule_type.strip() == 'normal': normal_result = NormalRule.process(rule_data_list, user) if not normal_result.status: raise Exception(normal_result.message) response.data = normal_result.data else: raise Exception("rule type is Error.") response.status = True except Exception, e: response.message = e.message
def delete_upload_file(filename, file_dir): ''' 删除用户所选的文件 ''' response = BaseResponse() try: file_path = file_dir + filename if os.path.isfile(file_path): os.remove(file_path) response.status = True return response else: response.message = u'删除失败' return response except Exception, e: response.message = str(e) return response
def check_valid(**kwargs): response = BaseResponse() try: result = user_models.AdminInfo.objects.get(**kwargs) response.status = True response.data = result except Exception, e: response.message = str(e)
def get_all_rule_list(): response = BaseResponse() try: ret = iptables_handle.get_all_rules() response.status = 1 response.data = list(ret) except Exception, e: response.message = str(e)
def delete_date(data): response = BaseResponse() try: id_list = json.loads(data) for i in id_list: i = int(i) search_handle.delete_data_by_id(i) response.status = True response.data = u"删除成功" except Exception, e: response.message = str(e)
def get_pass_log_count(): ''' 获取iptables日志总条数 ''' response = BaseResponse() try: result = log_handle.get_pass_log_count() response.data = result response.status = True except Exception, e: response.message = str(e)
def upload_ping_file(file_obj, filename, up_dir): ''' 将文件上传至目录下 ''' response = BaseResponse() try: # 保存上传的文件 if not filename.endswith("xlsx"): response.message = u"文件格式错误" return response file_dir = up_dir + filename f = open(file_dir, 'wb') for line in file_obj.chunks(): # 保存文件 f.write(line) f.close() response.status = True return response except Exception, e: response.message = str(e) return response
def get_all_pro_type(): response = BaseResponse() try: values = [ 'id', 'name', ] result = search_handle.get_pro_type_list(values) # 返回的是一个类似列表 result = list(result) # 转换为list类型 response.data = result # 封装到对象中 response.status = True except Exception, e: response.message = str(e)
def pass_log_data_list(page_start, page_stop): ''' 获取指定条数的日志记录 ''' response = BaseResponse() try: values = ['id', 'import_type', 'user_name', 'ip_list', 're_state', 'add_time'] # 在这里定义好查询表时 需要筛选的字段 result = log_handle.get_pass_log_list(page_start, page_stop, values) # 搜索工单信息 result = list(result) response.data = result # 封装到对象中 response.status = True except Exception, e: response.message = str(e)
def get_log_count(): ''' 获取iptables日志总条数 :return: ''' response = BaseResponse() try: result = iptables_handle.get_log_lists_count( ) # j将搜索条件Q 传入其中 取出搜索到的数据的条数 response.data = result response.status = True except Exception, e: response.message = str(e)
def rang_ping(ip_data): response = BaseResponse() try: ip_dict = json.loads(ip_data) start_ip = ip_dict["start_ip"].strip() end_ip = ip_dict["end_ip"].strip() if len(start_ip) == 0 or len(end_ip) == 0: response.message = u"Ip地址输入错误" return response for ip in ip_dict.values(): if not ipv4_re.match(ip): response.message = u"Ip地址格式错误" return response start_list = start_ip.split(".") end_list = end_ip.split(".") if start_list[0] != end_list[0] or start_list[1] != end_list[1] or start_list[2] != end_list[2]: response.message = u"IP地址段输入错误" return response diff = int(str(end_list[3])) - int(str(start_list[3])) print "diff", diff if diff < 0: response.message = u"IP地址段输入错误(注意IP地址顺序)" return response ip_list = [] l = int(start_list[3]) for i in range(diff + 1): start_list[3] = str(l+i) ip_list.append(".".join(start_list)) re_list = ping_host_ip.run_ping_by_ip(ip_list) response.data = re_list response.status = True return response except Exception, e: response.message = str(e) return response
def send_file(file_dir, data, username): response = BaseResponse() try: # 开始读取文件中的内容 filename = data.get('file_name', None) envir = data.get('envir', None) if envir is None: response.message = u'请检查所选环境' return response file_dir = file_dir + "host_ip.xlsx" is_have = os.path.isfile(file_dir) if not is_have: response.message = u'指定文件不存在' return response ip_info_dict = read_ip_from_file(file_dir) ip_list = ip_info_dict['ip_list'] pwd_check = pwd_query.main(ip_list, ip_info_dict['ip_info_list'], envir) if len(pwd_check) == 0: response.pwd_state = False response.message = u"密码检测失败!!请检查所选环境和密码是否存在~" return response create_host_file(pwd_check) # 生成一个全新的hosts文件 send_file_obj = Ansible_API() send_result = send_file_obj.send_file(filename) log_manager.ansible_file_log(username, ip_list, filename) no_pass_host = check_no_password_host(ip_list, pwd_check) response.no_pass = no_pass_host response.status = True response.data = send_result return response except Exception, e: response.message = str(e) return response
def file_ping(filename, up_dir): ''' 通过上传文件来ping ''' response = BaseResponse() try: # 开始读取文件中的内容 file_dir = up_dir + filename print "file_===================dir", file_dir is_have = os.path.isfile(file_dir) if not is_have: response.message = u'指定文件不存在' return response ip_list = read_ip_from_file(file_dir) print "ip_=================list", ip_list re_list = ping_host_ip.run_ping_by_ip(ip_list) response.data = re_list response.status = True return response except Exception, e: response.message = str(e) return response
def delete_data_excel(data): ''' 删除excel中所选的行数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: delete_list = json.loads(data.get('delete_list')) current_file = data.get("current_file") s = read_excel_data.delete_excel_data(BUDGET_FILE_DIR + current_file, delete_list) response.data = "OKOKOKOKKK" response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def make_delete_rule(host_ips, rule_ips): ''' 制作简单规则 ''' cmd_list = [] easy_rule_cmd = "iptables -D INPUT -s %s -j ACCEPT" save_cmd = "service iptables save" response = BaseResponse() try: for rule_ip in rule_ips: cmd_list.append(easy_rule_cmd % rule_ip) cmd_list.append(save_cmd) response.status = True response.data = cmd_list except Exception, e: response.message = str(e)
def check_user_info(post_data): ''' 检查用户的信息 ''' response = BaseResponse() try: user = post_data['user'] ip = post_data['ip'] user_key = post_data['p_key'] user_dict = {'user': user, 'ip': ip, 'p_key': user_key} ret = user_models.IptablesAuth.objects.filter(**user_dict).first() if not ret: raise Exception('Not found this user info, Authentication failed.') response.status = True except Exception, e: response.message = e.message
def save_change_data(data): ''' 保存表格变更的数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: current_file = data.get('current_file') change_dict = json.loads(data.get('update_data')) print "current_file", current_file s = read_excel_data.save_change_excel(BUDGET_FILE_DIR + current_file, change_dict) response.data = "修改成功!!!" response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def add_new_excel_row(data): ''' 新增一行表格数据 ''' response = BaseResponse() from OM_platform.settings import BUDGET_FILE_DIR try: add_raw = data.get("action") current_file = data.get("current_file") print "dddddddddddddddddd2222222222222", add_raw, current_file s = read_excel_data.add_new_raw(BUDGET_FILE_DIR + current_file) response.data = "增加行成功!" response.status = True return response except Exception, e: print "eeeeeeeeeeeeeeeeeeeeeeeeeeee", e response.message = str(e) return response
def delete_ping_file(file_dir): ''' 删除所有的上传ping文件 ''' response = BaseResponse() try: all_file_list = os.listdir(file_dir) print ">>>>>>>>>>>>>>>>>>>>", all_file_list for file_name in all_file_list: print "<<<<<<<<<<<<<<<<<<<<<", file_dir+file_name os.remove(file_dir+file_name) # all_file_list2 = os.listdir(file_dir) response.status = True return response except Exception, e: response.message = str(e) return response
def idc_info_data(): response = BaseResponse() try: ret = [] all_idc_list = [u"德信", u"M6", u"鹏博士", u"M5", u"大簇"] for name in all_idc_list: idc_dict = {} online, offline, scrap = collect_handle.get_idc_device_status(name) idc_dict['name'] = name idc_dict['online'] = online idc_dict['offline'] = offline idc_dict['scrap'] = scrap idc_dict['all_count'] = online + offline + scrap ret.append(idc_dict) response.data = ret response.status = True except Exception, e: response.message = e.message
def brand_data(): response = BaseResponse() try: ret = [] all_deivce_list = [u"浪潮", u"华为", u"DELL", u"HP", u"曙光", u"IBM"] for name in all_deivce_list: brand_dict = {} online, offline, scrap = collect_handle.get_brand_status(name) brand_dict['name'] = name brand_dict['online'] = online brand_dict['offline'] = offline brand_dict['scrap'] = scrap brand_dict['all_count'] = online + offline + scrap ret.append(brand_dict) response.data = ret response.status = True except Exception, e: response.message = e.message