def login_raw(instance, h): '''invoke native ssh client''' ip, port, username, password = h.host.ip_addr, h.host.port, h.host_user.username, h.host_user.password ssh_path = instance.django_settings.SSH_CLIENT_PATH rand_tag_id = utils.random_str(16) session_obj = get_session_id(instance, h, rand_tag_id) session_track_process = subprocess.Popen( "/bin/sh %s/backend/session_tracker.sh %s %s" % (instance.django_settings.BASE_DIR, session_obj.id, rand_tag_id), shell=True, cwd=instance.django_settings.BASE_DIR, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd_str = "sshpass -p %s %s %s@%s -p%s -Z %s -o StrictHostKeyChecking=no" % ( password, ssh_path, username, ip, port, rand_tag_id) subprocess.run(cmd_str, shell=True) #print('---login done--',rand_tag_id, session_track_process.poll()) # if session_track_process.poll() is not None: # print("stdout:",session_track_process.stdout.read()) # print("stderr:",session_track_process.stderr.read()) #update session stay time session_obj.stay_time = time.time() - session_obj.date.timestamp() session_log_file = "%s/%s/session_%s.log" % ( instance.django_settings.SESSION_AUDIT_LOG_DIR, session_obj.date.strftime("%Y_%m_%d"), session_obj.id) log_parser = audit.AuditLogHandler(session_log_file) log_data = log_parser.parse() session_obj.cmd_count = len(log_data) session_obj.save()
def audit_log_detail(request, session_id): """ 详细审计日志查看页面 :param request: :param session_id: session对象ID :return: """ condition_dict = { "list_per_page": request.GET.get("list_per_page", 100), "parse_mark": request.GET.get("parse_mark") } audit_log_detail_form_obj = forms.AuditLogDetailForm(data=condition_dict) session_obj = models.Session.objects.filter( id=session_id).first() # 获取session对象 audit_log_detail_path = os.path.join( settings.AUDIT_LOG_DIR, datetime.strftime(session_obj.date, "%Y-%m-%d"), f"strace_{session_obj.id}.log") if os.path.exists(audit_log_detail_path): audit_log_parser = audit.AuditLogHandler( audit_log_detail_path, condition_dict.get("parse_mark")) cmd_list = audit_log_parser.parse() cmd_list = utils.get_paginator_query_sets( request, cmd_list, condition_dict.get("list_per_page")) return render( request, "audit_log_detail.html", { "audit_log_detail_form_obj": audit_log_detail_form_obj, "cmd_list": cmd_list }) else: return HttpResponse("Sorry the file is not exist!")
def session_reccord(request,session_id): try: session_obj = models.Session.objects.get(id=session_id) print("session obj:",session_obj) session_log_file = "%s/%s/session_%s.log" %(settings.SESSION_AUDIT_LOG_DIR, session_obj.date.strftime( "%Y_%m_%d"), session_obj.id) if os.path.isfile(session_log_file): log_wash = session_audit.AuditLogHandler(session_log_file) log_data = log_wash.parse() #update session stay time and cmd count session_obj.cmd_count = len(log_data) if len(log_data ) >1: last_cmd_time = log_data[-1][0] last_cmd_datetime_str = "%s %s"%(session_obj.date.strftime( "%Y_%m_%d"), last_cmd_time) #print("last_cmd_datetime_str",last_cmd_datetime_str) last_cmd_struct_time = time.strptime(last_cmd_datetime_str,"%Y_%m_%d %H:%M:%S") last_cmd_timestamp = time.mktime(last_cmd_struct_time) #print('last cmd timestamp:',last_cmd_timestamp) session_obj.stay_time = last_cmd_timestamp - session_obj.date.timestamp() session_obj.save() else: log_data = [['n/a','---no session log---']] # if os.path.isfile(session_log_file): # session_log = open(session_log_file).read() # else: # print('file not exist ',session_log_file) # session_log = '---no session log---' return render(request,"session_log.html",{'session_data':log_data,'session_obj':session_obj}) except ObjectDoesNotExist as e: return HttpResponse(e)
def user_audit_log_detail(request,log_date,session_id): log_file = os.path.join(settings.LOG_PATH,log_date,"session_%s.log" % session_id) from backend import audit audit_handler = audit.AuditLogHandler(log_file) cmd_list = audit_handler.parse() return render(request,'user_audit_detail.html',{'cmd_list':cmd_list,})
def audit_log_detail(request, log_date, session_id): log_date_path = "%s/%s" % (settings.AUDIT_LOG_DIR, log_date) log_file_path = "%s/session_%s.log" % (log_date_path, session_id) log_parser = audit.AuditLogHandler(log_file_path) cmd_list = log_parser.parse() return render(request, "user_audit_detail.html", locals())