首页 > 编程语言 >安全巡检 python HTML报告

安全巡检 python HTML报告

时间:2023-06-19 18:33:43浏览次数:37  
标签:巡检 indicator python HTML dict result command margin

1.报表模板 #cat xunjian_table.html

[root@yinliao-yanshi report_jinja2]# cat xunjian_table.html 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>insight report</title>
</head>
<style type="text/css">
    h1 {margin-left: 20px}
    h2 {margin-left: 20px;
        font-size: 19px;
        font-weight: bold;
        display: inline-block;
        padding-left: 10px;
        border-left: 5px solid #916dd5;}
    h3 {margin-left: 20px}
    h4 {margin-left: 20px;
        margin-bottom: -5px}
    table {margin-left: 20px;
           margin-top: 5px;
           margin-bottom: 5px}
    p {margin-left: 20px}
    a {margin-top: 200px;}
    </style>
<h1>巡检报告</h1>
<body>
<h2>一、巡检详情</h2>
<p>巡检描述:{{ xunjian_description }}</p>
<p>巡检时间段:{{ start_time }} --> {{ end_time }} </p>
<p>巡检人:{{ people }}</p>
</body>
</html>



<h2>二、巡检结果</h2>

    <table border="1" width = "40%" cellspacing='0' cellpadding='0'>
    <tr>
        <th>ID</th>
        <th>指标描述</th>
        <th>巡检结果</th>
        <th>是否通过</th>
    </tr>

    {% for item in items %}
    <tr align='center'>
        <td>{{ item.ID }}</td>
        <td>{{ item.指标描述 }}</td>
        <td>{{ item.巡检结果 }}</td>
        <td>{{ item.是否通过 }}</td>
    </tr>
    {% endfor%}
    </table>
[root@yinliao-yanshi report_jinja2]#

 

2. python脚本

[root@yinliao-yanshi report_jinja2]# cat xunjian_command_result.py 
#!/usr/bin/python3
# -*-  coding=utf-8 -*-
# author: xiaoweige
import subprocess
from jinja2 import Environment, FileSystemLoader
import datetime

overall_info={'xunjian_description': '第一个巡检报告',
'start_time': '2020-01-01',
'end_time': '2021-06-01',
'people': "小伟哥"}

all_dict_list = []
indicator_list = [{"ID":1,"指标描述":"密码复杂度","command":"egrep '^password.*minlen.*ocredit'  /etc/pam.d/system-auth |wc -l"},{"ID":2,"指标描述":"禁用root用户登录","command":"egrep '^PermitRootLogin.*no'   /etc/ssh/sshd_config |wc -l"},{"ID":3,"指标描述":"设置密码有效期","command":"egrep 'PASS_MAX_DAYS.*90' /etc/login.defs|wc -l"}]
def exec_command_result(indicator_dict):
    command_no_wc = indicator_dict["command"].split('|wc')[0]
    try:
      indicator_dict["巡检结果"]=subprocess.check_output(command_no_wc,shell=True).decode().strip('\n')
    except:
      indicator_dict["巡检结果"]='不存在符合条件的配置'

    print(command_no_wc)
    shell_result = subprocess.check_output(indicator_dict["command"], shell=True).decode().strip().split('\n')
    if shell_result[0] == "1":
       indicator_dict["是否通过"]="是"
    else:
       indicator_dict["是否通过"]="否"
    return indicator_dict

for each_indicator_dict in indicator_list:
    indicator_result = exec_command_result(each_indicator_dict)
    all_dict_list.append(indicator_result)



env = Environment(loader=FileSystemLoader('./'))
template = env.get_template('xunjian_table.html')
ip_result=subprocess.check_output("ip a|grep 'inet.*eth0'|awk '{print $2}'| awk -F'/' '{print $1}'",shell=True).decode().strip('\n')
with open("{}_xunjian_out_{}.html".format(ip_result,datetime.datetime.now().strftime('%Y_%m_%d_%H_%M')), 'w+', encoding='utf-8') as f:
    out = template.render(xunjian_description=overall_info['xunjian_description'],
                          start_time=overall_info['start_time'],
                          end_time=overall_info['end_time'],
                          people=overall_info['people'],
                          items = all_dict_list)
    f.write(out)
    f.close()


[root@yinliao-yanshi report_jinja2]#

 

安全巡检 python HTML报告_css

 

安全巡检 python HTML报告_复杂度_02

 

用一个例子来演示会更加清晰



标签:巡检,indicator,python,HTML,dict,result,command,margin
From: https://blog.51cto.com/u_13747193/6516825

相关文章

  • Python爬虫(二):写一个爬取壁纸网站图片的爬虫(图片下载,词频统计,思路)
    好家伙,写爬虫 代码: importrequestsimportreimportosfromcollectionsimportCounterimportxlwt#创建Excel文件workbook=xlwt.Workbook(encoding='utf-8')worksheet=workbook.add_sheet('url_list')#将数据写入Excel文件worksheet.write(0,0,&......
  • python+allure报告内容+发送钉钉消息
    一、钉钉配置获取1.点击钉钉左上角"设置" 2.找到机器人管理,点击“前往设置”,选择自定义机器人 3.设置名称和群,安全设置可不加,保存url后续接口请求需要用到 4.三种安全设置 二、pyhon脚本注:修改机器人URL和报告路径后即可使用,转载!#coding=gbkimportcsvimportj......
  • Python中的DYNAMIXEL快速入门指南
    原文链接:https://www.youtube.com/watch?v=LAizFTTdL8o hisvideowillbecoveringtherequiredcomponentsandhardware&softwaresetup,andfinallyruntheDYNAMIXELinPythonwithDYNAMIXELSDKwithinjustafewMINUTES. 本视频将涵盖所需的组件和硬件、软......
  • python 日志使用
    python日志使用日志基础教程日志是对软件执行时所发生事件的一种追踪方式。软件开发人员对他们的代码添加日志调用,借此来指示某事件的发生。一个事件通过一些包含变量数据的描述信息来描述(比如:每个事件发生时的数据都是不同的)。开发者还会区分事件的重要性,重要性也被称为等级......
  • python 类鸟群Boids
    importsys,argparseimportmathimportnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlib.animationasanimationfromscipy.spatial.distanceimportsquareform,pdist,cdist#计算点之间的距离fromnumpy.linalgimportnormfrommatplotlib.colors......
  • Python3使用装饰器实现参数类型检查
    fromfunctoolsimportwrapsdefmerge_args(varnames:tuple,args:tuple,kwargs:dict)->dict:"""融合参数-将args参数都转为kwargs参数:paramvarnames:变量名列表:paramargs:args参数:paramkwargs:kwargs参数:return:"&......
  • 江苏工匠杯bad_python
    江苏工匠杯bad_pythonPython逆向拿到附件是一个pyc文件,按照我们常规的python逆向思路,肯定是先试一试能不能uncompyle6反编译.发现无法反编译,我们用010editor查看一下pyc文件的文件头.可以看到文件头是以0D33结尾的330D00000000000000000000000000000000000......
  • Python实现一个简单的栈结构
    基于list来实现一个栈:classSimpleStack:def__init__(self):self.__stack:list=[]​def__iter__(self):returnSimpleStackIterator(self)​def__len__(self):returnlen(self.__stack)​def__getitem__(self,index)......
  • Python3中的函数对象
    在Python中一切皆对象,函数也是一种对象,有相关的属性和方法。对于任意对象,我们可以用dir()函数来获取其内置的属性及方法名,例如:defadd(a:int,b:int=1)->int:"""加法函数"""returna+bprint(dir(add))运行结果如下:['__annotations__','__call__','_......
  • python之mysql登录验证
    fromdotenvimportload_dotenvimportpymysqlimportosload_dotenv()connection=pymysql.connect(host=os.getenv("HOST"),database=os.getenv("DATABASE"),user=os.getenv("USERNAME"),password=os.getenv("PASSWORD......