一、目录结构
二、实现代码
1、views
from django.shortcuts import render,redirect,HttpResponsefrom django.contrib.auth.decorators import login_requiredfrom django.contrib.auth import authenticate,logout,loginfrom django.conf import settingsimport os,re,jsonfrom backend import audit# Create your views here.def json_date_handler(obj): if hasattr(obj, 'isoformat'): return obj.strftime("%Y-%m-%d %T")@login_requireddef dashboard(request): return render(request,'index.html')def acc_login(request): error_msg = '' if request.method == "POST": username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username,password=password) if user: login(request,user) return redirect("/") else: error_msg = "Wrong username or password!" return render(request,"login.html",{'error_msg':error_msg})def acc_logout(request): logout(request) return redirect("/login/")@login_requireddef webssh(request): return render(request,'web_ssh.html')@login_requireddef user_audit(request): log_dirs = os.listdir(settings.AUDIT_LOG_DIR) return render(request,'user_audit.html',locals())@login_requireddef audit_log_date(request,log_date): log_date_path = "%s/%s" %(settings.AUDIT_LOG_DIR,log_date) log_file_dirs = os.listdir(log_date_path) session_ids = [re.search("\d+",i).group() for i in log_file_dirs ] session_objs = models.Session.objects.filter(id__in=session_ids) return render(request, 'user_audit_file_list.html', locals())
2、url
from django.conf.urls import urlfrom django.contrib import adminfrom web import viewsurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.dashboard), url(r'^user_audit/$', views.user_audit,name="user_audit"), url(r'^audit_log/(\w+-\w+-\w+)/$', views.audit_log_date,name="audit_log_date"), url(r'^audit_log/(\w+-\w+-\w+)/(\d+)/$', views.audit_log_detail,name="audit_log_detail"), url(r'^login/$', views.acc_login), url(r'^logout/$', views.acc_logout,name="logout"),]
3、settings
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'statics'),)
4、base.html
1 2 3 4 5 6CrazyEye | 智能堡垒机 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 51 52 53 54 55 56 57 58 59 {% block body %}body content{% endblock %} 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 98 99 100 101
5、index.html
1 {% extends 'base.html' %} 2 3 {% block body %} 4805 806 807 {% endblock %}
6、login.html
1 {% extends 'base.html' %} 2 3 4 5 6 {% block body %} 78 9 10 1168 69 70 71 {% endblock %}1219 20 21 22 23 2413 14 15 CrazyEye智能堡垒机16 17182563 64 65 66 672658 622757Sign In to your account
28 56
7、user_audit.html
{% extends 'index.html' %}{% block page-title %}用户审计{% endblock %}{% block page-content %}{% endblock %}审计目录
{% for dir_name in log_dirs %}
- { { dir_name }}
{% endfor %}
8、user_audit_detail.html
{% extends 'index.html' %}{% block page-title %}用户审计{% endblock %}{% block page-content %}{% endblock %}审计详情 主机:
{% for cmd in cmd_list %} id 日期 命令 {% endfor %} { { forloop.counter }} { { cmd.0 }} { { cmd.1 }}
9、user_audit_file_list.html
{% extends 'index.html' %}{% block page-title %}用户审计{% endblock %}{% block page-content %}{% endblock %}审计日志列表
{% for session_obj in session_objs %} Session ID 堡垒机用户 登录时间 登录主机 登录用户名 停留时间 执行命令数 {% endfor %} { { session_obj.id }} { { session_obj.user.name }} { { session_obj.date }} { { session_obj.bind_host.host.hostname }}({ { session_obj.bind_host.host.ip_addr }}) { { session_obj.bind_host.remote_user.username }} ... ...
三、测试截图
1、审计列表
2、审计目录
3、审计详情主机