1.报表模板
[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 overall_info={'xunjian_description': '第一个巡检报告', 'start_time': '2020-01-01', 'end_time': '2021-06-01', 'people': "小伟哥"} all_dict_list = [] complex_results = {"ID":1,"指标描述":"密码复杂度"} shell_result = subprocess.check_output("egrep '^password.*minlen.*ocredit' /etc/pam.d/system-auth |wc -l", shell=True).decode().strip().split('\n') print("密码复杂度 command result: {}".format(shell_result[0])) if shell_result[0] =="1": complex_results["巡检结果"]=1 complex_results["是否通过"]="是" else: complex_results["巡检结果"]=0 complex_results["是否通过"]="否" all_dict_list.append(complex_results) permitroot_results = {"ID":2,"指标描述":"禁用root用户登录"} permitroot_result = subprocess.check_output("egrep '^PermitRootLogin.*no' /etc/ssh/sshd_config |wc -l", shell=True).decode().strip().split('\n') print("禁用root用户登录 command result: {}".format(permitroot_result[0])) if permitroot_result[0] =="1": permitroot_results["巡检结果"]=1 permitroot_results["是否通过"]="是" else: permitroot_results["巡检结果"]=0 permitroot_results["是否通过"]="否" all_dict_list.append(permitroot_results) print('permitroot_results: {} \n'.format(all_dict_list)) env = Environment(loader=FileSystemLoader('./')) template = env.get_template('xunjian_table.html') with open("xunjian_out.html", '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,results,HTML,xunjian,result,margin,permitroot From: https://www.cnblogs.com/hixiaowei/p/16704045.html