实现查看标签 添加一个查看标签
def Check(self): return format_html(\'<a href="http://www.likecs.com/admin/MyWeb/hostdb/{}/change/">查看</a>\',self.id)增加自定义actions: 自定义Action标签,Action标签就是Admin页面中左上角的横线部分,我们自己增加新的.
# name: admin.py from django.contrib import admin from MyWeb.models import * # 必须继承ModelAdmin基类,才可以调整参数,HostDB则是你的表的名称 @admin.register(HostInfo) class MyAdmin(admin.ModelAdmin): admin.site.site_title="自动化后台管理" admin.site.site_header = "Django 管理平台" <省略部分...> def func(self,request,queryset): # 此处可以写一些执行动作 print(self,request,queryset) func.short_description = "自定义active动作" actions = [func,] # Action选项都是在页面上方显示 actions_on_top = True # Action选项都是在页面下方显示 actions_on_bottom = False # 是否显示选择个数 actions_selection_counter = True 添加防火墙管理模块 {% extends "admin/base_site.html" %} {% load i18n static %} {% block content %} <link href="http://www.blib.cn/cdn/bootstrap3.css" /> <script src="http://www.blib.cn/cdn/jquery.js"></script> <style> #content { padding: 10px 10px; } .panel-body { padding: 10px; } </style> <div> <div> <h3>防火墙配置模块</h3> </div> <div> <div> <div> <div> <input type="text" placeholder="源地址"> <input type="text" placeholder="备注/说明"> <select> <option value="accept">放行IP</option> <option value="drop">屏蔽IP</option> </select> <button type="button">提交更改</button> </div> </div> </div> <div> <div> <table> <thead> <tr> <th>编号</th> <th>IP地址</th> <th>动作</th> <th>添加时间</th> <th>备注</th> <th>操作</th> </tr> </thead> <tbody> {% for item in data %} <tr> <td> {{ item.id }}</td> <td> {{ item.SourceAddr }}</td> <td> {{ item.Action }}</td> <td> {{ item.DataTime }}</td> <td> {{ item.Remarks }}</td> <td><a href="http://www.likecs.com/index.html">删除</a></td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </div> <script> $(document).ready(function(){ $("#toAccept").click(function(){ var message = {"type":null,"SourceAddr":null,"Remarks":null}; message[\'type\'] = $("#firewalldType option:selected").val(); message[\'SourceAddr\'] = $("#SourceAddr").val(); message[\'Remarks\'] = $("#Remarks").val(); data = JSON.stringify(message); $.ajax({ url:"/echo/", type:"POST", dataType:"json", data:data, success:function () { window.location.reload(); } }); }); }); </script> {% endblock %} from django.shortcuts import render,HttpResponse from MyWeb import models import paramiko,time,json ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) def ssh_shell(address,username,password,port,command): try: ssh.connect(address,port=port,username=username,password=password,timeout=1) stdin, stdout, stderr = ssh.exec_command(command) result = stdout.read() if not result: result=stderr.read() ssh.close() return result.decode() except Exception: return 0 def echo(request): if request.method == "GET": obj = models.FireWallDB.objects.all() print(obj) return render(request,"http://www.likecs.com/index.html",{"data":obj}) else: recv = json.loads(request.body.decode("utf-8")) addr = recv.get("SourceAddr") remak = recv.get("Remarks") if recv.get("type") == "accept": cmd = \'firewall-cmd --add-rich-rule \\'rule family=ipv4 source address="{}" accept\\'http://www.likecs.com/\'.format(addr) count = models.FireWallDB.objects.filter(SourceAddr=addr).count() if count <2: retn = ssh_shell("192.168.1.20","root","123","22",cmd) if retn !=0: obj = models.FireWallDB() obj.SourceAddr = addr obj.Action = "放行" obj.DataTime = str(time.strftime(\'%Y-%m-%d %H:%M:%S\',time.localtime(time.time()))) obj.Remarks = remak obj.SourceCMD = cmd obj.save() print("ok") elif recv.get("type") == "drop": cmd = \'firewall-cmd --add-rich-rule \\'rule family=ipv4 source address="{}" drop\\'http://www.likecs.com/\'.format(addr) print(cmd) else: print("最多两个") return HttpResponse("ok") return render(request,"http://www.likecs.com/index.html") from django.db import models class FireWallDB(models.Model): id = models.AutoField(primary_key=True) SourceAddr = models.CharField(max_length=64) Action = models.CharField(max_length=64) DataTime = models.CharField(max_length=64) Remarks = models.CharField(max_length=64) SourceCMD = models.CharField(max_length=512)