【堡垒机】第9章——堡垒机后台处理批量任务
ajax发送命令
<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button><button type="button" id="submit_task_confirm" onclick="submit_task(this,'/api/multitask/cmd/')" class="btn btn-primary">确定执行</button></div>
function submit_task(ele,post_url){//console.log(ele);$(ele).attr('disabled','true');$.post(post_url, postDic,function(callback){console.log(callback);if (callback != 'TaskCreatingError'){$("#modal_content").html("任务创建成功! 任务编号:" + callback);$("#modal_content").attr("style","color:green;font:bold");$(".task_result").html("");$("#file-download-to-local").attr("task_id",callback); // only works when task_type = file_getGetTaskResult(callback,'refresh');}else{$("#modal_content").html("任务创建失败,请查看相关日志进行调试!");$("#modal_content").attr("style","color:red;font:bold")}});}
api接口
url(r'multitask/cmd/$', views.multitask_cmd,name='multitask_cmd'),
@login_required
def multitask_cmd(request):#print '==post:',request.POSTmulti_task = host_mgr.MultiTask('run_cmd',request)task_id = multi_task.run() # 执行run方法if task_id:return HttpResponse(task_id)else:return HttpResponse("TaskCreatingError")
class MultiTask(object):def __init__(self,task_type,request_ins):self.request = request_insself.task_type = task_typedef run(self):return self.parse_args()def parse_args(self):#print '==>parse_args:', self.request.POSTtask_func = getattr(self,self.task_type)return task_func()def terminate_task(self):task_id = self.request.POST.get('task_id')assert task_id.isdigit()task_obj = models.TaskLog.objects.get(id=int(task_id))res_msg = ''try:os.killpg(task_obj.task_pid,signal.SIGTERM)res_msg = 'Task %s has terminated!' % task_idexcept OSError as e:res_msg = "Error happened when tries to terminate task %s , err_msg[%s]" % (task_id,str(e))return res_msgdef run_cmd(self):cmd = self.request.POST.get("cmd")host_ids =[int(i.split('host_')[-1]) for i in json.loads(self.request.POST.get("selected_hosts"))]task_expire_time = self.request.POST.get("expire_time")exec_hosts = models.BindHosts.objects.filter(id__in=host_ids)random_str = self.request.POST.get('local_file_path')task_obj= self.create_task_log('cmd',exec_hosts,task_expire_time,cmd,random_str)#'-task_type', 'cmd', '-task_id', '15', '-expire', '30', '-task', 'ifconfig', '-uid', '1']print("-->run cmd:",cmd,host_ids)p = subprocess.Popen(['python3',settings.MultiTaskScript,'-task_type','cmd','-expire',task_expire_time,'-uid',str(self.request.user.id) ,'-task',cmd ,'-task_id', str(task_obj.id)],preexec_fn=os.setsid)task_obj.task_pid = p.pidtask_obj.save()return task_obj.id
调用python脚本,并格式化参数
if __name__ == '__main__':require_args= ['-task_type','-task_id','-expire','-uid']lack_args = [arg for arg in require_args if arg not in sys.argv[1:]]if len(lack_args) >0:sys.exit("lack args of: %s"% lack_args)task_type = sys.argv[sys.argv.index('-task_type')+1]if task_type =='cmd':require_args= ['-task',]# elif task_type == 'file_send':# require_args = ['-local','-remote']elif task_type == 'file_get':require_args = ['-remote',]lack_args = [arg for arg in require_args if arg not in sys.argv[1:]]if len(lack_args) >0:sys.exit("lack args of: %s"% lack_args)task_id = sys.argv[sys.argv.index('-task_id')+1]expire_time = sys.argv[sys.argv.index('-expire')+1]if task_type =='cmd':content = sys.argv[sys.argv.index('-task')+1]else:content = sys.argvuid = sys.argv[sys.argv.index('-uid')+1]task_obj = models.TaskLog.objects.get(id=int(task_id))connection.close()Pool = multiprocessing.Pool(processes=settings.MaxTaskProcesses)res_list = []if task_type == 'cmd':task_func = cmd_execelif task_type == 'file_send' or task_type == 'file_get':task_func = file_tranfer_execelse:sys.exit("wrong task_type")for h in task_obj.hosts.select_related():p = Pool.apply_async(task_func,args=(task_id,h.id,uid,content))res_list.append(p)#get resultPool.close()Pool.join()#for res in res_list:# res.get()
执行前端传递的cmd命令,并将结果存入数据库
__author__ = 'jieli'import global_settings
import json
import traceback
import paramiko
from CrazyEye import settings
import django
django.setup()
from web import models
from django.db import connection
import sys,time,os
import multiprocessingdef cmd_exec(task_id,bind_host_id,user_id,cmd ):bind_host = models.BindHosts.objects.get(id=bind_host_id)s = paramiko.SSHClient()s.load_system_host_keys()s.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:if bind_host.host_user.auth_method == 'ssh-password':s.connect(bind_host.host.ip_addr,int(bind_host.host.port),bind_host.host_user.username,bind_host.host_user.password,timeout=5)else:#rsa_keykey = paramiko.RSAKey.from_private_key_file(settings.RSA_PRIVATE_KEY_FILE)s.connect(bind_host.host.ip_addr,int(bind_host.host.port),bind_host.host_user.username,pkey=key,timeout=5)stdin,stdout,stderr = s.exec_command(cmd)result = stdout.read(),stderr.read()if any(result):#cmd_result = filter(lambda x:len(x.strip())>0,result)[0]cmd_result = result[0] if result[0] else result[1]else:cmd_result = b'execution has no output!'res_status = 'success'print('----------- HOST:%s IP:%s -------------' %(bind_host.host.hostname,bind_host.host.ip_addr) )# for line in cmd_result.decode():# print(line)print(cmd_result.decode())s.close()except Exception as e:#except ValueError as e:print('----------- HOST:%s IP:%s -------------' %(bind_host.host.hostname,bind_host.host.ip_addr))print('\033[31;1mError:%s\033[0m' % e)print(traceback.print_exc())cmd_result = eres_status = 'failed'log_obj = models.TaskLogDetail.objects.get(child_of_task_id= int(task_id), bind_host_id=bind_host.id)log_obj.event_log = cmd_resultlog_obj.result= res_statuslog_obj.save()
执行cmd命令后,返回任务id给前端,前端根据任务id获得cmd任务执行结果
获取结果API
def get_task_result(self,detail=True):'''get multi run task result'''task_id = self.request.GET.get('task_id')log_dic ={#'summary':{},'detail':{}}task_obj = models.TaskLog.objects.get(id=int(task_id))task_detail_obj_list = models.TaskLogDetail.objects.filter(child_of_task_id=task_obj.id)log_dic['summary']={'id':task_obj.id ,'start_time': task_obj.start_time,'end_time': task_obj.end_time,'task_type': task_obj.task_type,'host_num': task_obj.hosts.select_related().count(),'finished_num': task_detail_obj_list.filter(result='success').count(),'failed_num': task_detail_obj_list.filter(result='failed').count(),'unknown_num': task_detail_obj_list.filter(result='unknown').count(),'content': task_obj.cmd,'expire_time':task_obj.expire_time}if detail:for log in task_detail_obj_list:log_dic['detail'][log.id] = {'date': log.date ,'bind_host_id':log.bind_host_id,'host_id': log.bind_host.host.id,'hostname': log.bind_host.host.hostname,'ip_addr': log.bind_host.host.ip_addr,'username': log.bind_host.host_user.username,'system' : log.bind_host.host.system_type,'event_log': log.event_log,'result': log.result,'note': log.note}return json.dumps(log_dic,default=json_date_handler)
【堡垒机】第9章——堡垒机后台处理批量任务相关推荐
- 什么是堡垒机?为什么需要堡垒机?
作者:猿话 www.toutiao.com/i6881462700229329421 什么是堡垒机 堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而运用各种技 ...
- 「系统架构」什么是堡垒机?为什么需要堡垒机?
来源:https://www.toutiao.com/i6881462700229329421 什么是堡垒机 堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而 ...
- 华为堡垒机_运维堡垒机----Gateone
简介: 运维堡垒机的理念起源于跳板机.2000年左右,高端行业用户为了对运维人员的远程登录进行集中管理,会在机房里部署跳板机.跳板机就是一台服务器,维护人员在维护过程中,首先要统一登录到这台服务器上, ...
- 【产品集】什么是堡垒机?为什么需要堡垒机
什么是堡垒机 堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而运用各种技术手段监控和记录运维人员对网络内的服务器.网络设备.安全设备.数据库等设备的操作行为, ...
- 堡垒机、运维堡垒机、开源堡垒机、云堡垒机全面解析
一.概述 1.0.数据丢失危机1.1.面临的挑战 复制代码 二.堡垒机的概念和种类 2.0.网关型堡垒机2.1.运维审计型堡垒机2.1.1.主要功能 复制代码 三.主流堡垒机解决方案 3.0.使用开源 ...
- 【堡垒机小知识】堡垒机是硬件还是软件?
堡垒机属于网络安全产品的一种,是企业过等保利器.但对于堡垒机,很多企业还是陌生的,存在很多疑问,例如有企业再问,堡垒机是硬件还是软件? 堡垒机是硬件还是软件? 目前市面上堡垒机类型比较多,包含硬件堡垒 ...
- 堡垒机世界的征战之尚思卓越堡垒机VS帕拉迪堡垒机
堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的DDoS和破坏,而运用各种技术手段实时收集和监控网络环境中每一个组成部分的系统状态.安全事件.网络活动,以便集中报警.记录. ...
- 堡垒机是什么?堡垒机有什么用?
一.什么是堡垒机? 堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而运用各种技术手段监控和记录运维人员对网络内的服务器.网络设备.安全设备.数据库等设备的操作 ...
- 跳板机和堡垒机的区别_什么是堡垒机?为什么需要堡垒机?
什么是堡垒机 堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而运用各种技术手段监控和记录运维人员对网络内的服务器.网络设备.安全设备.数据库等设备的操作行为, ...
- 堡垒机应用服务器发布原理,堡垒机原理
堡垒机登录的原理 堡垒机实际上是旁路在网络交换机节点上的硬件设备,实现运维人员远程访问维护服务器的跳板,即物理上并联,逻辑上串联. 简单的说,服务器运维人员原先是直接通过远程访问技术进行服务器维护和操 ...
最新文章
- Gartner:2012年大数据HypeCycle
- HDU 1180 诡异的楼梯
- 关于在linux操作系统中中不能删除创建创建的用户的操作
- SecureCRT:保存输出日志的方法
- python怎么执行程序_小鹅通视频怎么下载?用python实现小鹅通视频下载(二)
- P2403 [SDOI2010]所驼门王的宝藏
- centos7.2下编译安装git
- java 同步 lock_关于java:同步是否像Lock.lock()一样驻留并发线程?
- spring认证的一些核心类
- ant部署web工程模板
- lc300.最长递增子序列
- linux6.4添加源,RHEL6.4更改为CentOS源
- 这些孩子对计算机游戏上瘾英语,沉迷网络游戏高中英语作文
- 记账系统推荐金蝶精斗云_金蝶精斗云是免费会计记账软件吗?
- 【Android】全网最详细的Android入门基础教程,零基础速领
- linux中安装openoffice
- H5端嵌入公众号后,修改头部标题
- 中文分词之维特比算法详解
- UOS下使用HHDESK文本编辑功能
- Whitelabel Error Page并且报500