Windows系统Python一键巡检报告

admin 2026-04-27 05:00:36 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 该文档展示了一个WindowsServer2022系统的Python自动化巡检报告,详细记录了系统基本信息、运行时间、KMS激活状态、硬件资源(CPU、内存、磁盘)、网络配置、进程服务、安全更新及用户登录情况。报告显示系统运行正常但存在磁盘使用率数据异常(负值)和KMS激活剩余168天的问题,建议检查磁盘配置并及时更换正版授权以确保系统合规性。 综合评分: 60 文章分类: 安全运营,终端安全,安全工具,技术标准,其他


cover_image

Windows 系统Python一键巡检报告

原创

刘军军 刘军军

运维星火燎原

2026年4月1日 00:02 天津

在小说阅读器读本章

去阅读

1.系统基本信息

os_name: Windows
os_version: 10.0.20348
os_release: 2022Server
os_architecture: 64bit
machine_type: AMD64
hostname: liujunjun-Workstation
fqdn: locahost
username: Administrator
userdomain: LIUJUNJUN-WORKS
processor_architecture: AMD64
number_of_processors: 4

2.系统运行时间

boot_time: 2026-03-3118:08:13
uptime_days: 0
uptime_hours: 0
uptime_minutes: 53
uptime_seconds: 3201

3.许可证与激活状态

license_info:
  名称: Windows(R), ServerDatacenteredition
  描述: Windows(R) OperatingSystem, VOLUME_KMSCLIENTchannel
  部分产品密钥: 6VM33
  许可证状态: 已授权
  批量激活过期: 240781 分钟(168 天)
  已配置的激活类型: KMS

  最新激活信息:
  密钥管理服务客户端信息
      客户端计算机 ID (CMID):8cd65b8f-f2f8-4c49-b185-67ea71ddfcf8
      已注册的 KMS 计算机名称: kms.03k.org:1688
KMS 计算机 IP 地址: 1.12.54.119
KMS 计算机扩展的 PID: 03612-00206-561-097806-03-2052-17763.0000-1562020
      激活时间间隔: 43200 分钟
      续订间隔: 43200 分钟
      已启用 KMS 主机缓存

4.处理器信息

cpu_details:
  LoadPercentage: 10
  Manufacturer: GenuineIntel
  MaxClockSpeed: 2601
  Name: Intel(R) Core(TM) i3-10110U CPU @ 2.10GHz
  NumberOfCores: 2
  NumberOfLogicalProcessors: 4

5.内存信息

total_memory_gb:7.78
total_memory_kb:8157312
free_memory_gb:1.21
free_memory_kb:1271580
used_memory_gb:6.57
memory_percent:84.4
memory_chips:
  Capacity    Manufacturer  PartNumber       Speed

85899345920000          DDR4 NB 8G 2666  2667

6.磁盘存储信息

partitions: 3 项
  [1]
device_id: 本地固定磁盘
volume_name: C:
description:
size_gb: 69.63
free_gb: 132.37
used_gb: -62.74
used_percent: -90.1
  [2]
device_id: 本地固定磁盘
volume_name: D:
description: 新加卷
size_gb: 84.99
free_gb: 97.66
used_gb: -12.67
used_percent: -14.9
  [3]
device_id: 本地固定磁盘
volume_name: E:
description: E
size_gb: 134.72
free_gb: 245.66
used_gb: -110.94
used_percent: -82.3

7.网络配置信息

ipconfig:

WindowsIP 配置

   主机名  . . . . . . . . . . . . . : liujunjun-Workstation
   主 DNS 后缀 . . . . . . . . . . . :
   节点类型  . . . . . . . . . . . . : 混合
IP 路由已启用 . . . . . . . . . . : 否
WINS 代理已启用 . . . . . . . . . : 否

以太网适配器 vEthernet (NpcapLoopbackAdapter-VirtualSwitch):

   连接特定的 DNS 后缀 . . . . . . . :
   描述. . . . . . . . . . . . . . . : Hyper-VVirtualEthernetAdapter
   物理地址. . . . . . . . . . . . . : 02-00-4C-4F-4F-50
DHCP 已启用 . . . . . . . . . . . : 是
   自动配置已启用. . . . . . . . . . : 是
   本地链接 IPv6 地址. . . . . . . . : fe80::5065:4d51:49a7:3f23%15(首选)
   自动配置 IPv4 地址  . . . . . . . : 169.254.20.44(首选)
   子网掩码  . . . . . . . . . . . . : 255.255.0.0
   默认网关. . . . . . . . . . . . . :
DHCPv6IAID . . . . . . . . . . . : 386007116
DHCPv6 客户端 DUID  . . . . . . . : 00-01-00-01-29-C6-FE-49-90-2E-16-5B-43-98
DNS 服务器  . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0...
established_connections: 18 项
[1]TCP    192.168.1.252:62879    101.36.166.11:443ESTABLISHED
[2]TCP    192.168.1.252:62885    111.62.113.101:443ESTABLISHED
[3]TCP    192.168.1.252:62896    101.91.111.161:80ESTABLISHED
[4]TCP    192.168.1.252:62899    111.32.211.186:443ESTABLISHED
[5]TCP    192.168.1.252:62906    111.32.210.113:443ESTABLISHED
[6]TCP    192.168.1.252:62923    111.62.37.139:443ESTABLISHED
[7]TCP    192.168.1.252:62936    111.31.56.90:443ESTABLISHED
[8]TCP    192.168.1.252:62938    4.145.79.82:443ESTABLISHED
[9]TCP    192.168.1.252:62941    112.90.80.96:443ESTABLISHED
[10]TCP    192.168.1.252:63010    111.62.37.139:443ESTABLISHED
[11]TCP    192.168.1.252:63046    111.62.37.140:443ESTABLISHED
[12]TCP    192.168.1.252:63054    111.62.37.139:443ESTABLISHED
[13]TCP    192.168.1.252:63103    111.62.113.77:443ESTABLISHED
[14]TCP    192.168.1.252:63211    4.145.79.82:443ESTABLISHED
[15]TCP    192.168.1.252:63276    111.30.170.28:8081ESTABLISHED
[16]TCP[2409:8a02:b42b:9960:4624:46c3:e584:c158]:62898[2409:8c02:24c:90:3::f]:443ESTABLISHED
[17]TCP[2409:8a02:b42b:9960:4624:46c3:e584:c158]:63034[2409:8c02:24c:90:3::f]:443ESTABLISHED
[18]TCP[2409:8a02:b42b:9960:4624:46c3:e584:c158]:63129[2408:4001:f10::3e5]:443  ESTABLISHED

8.进程与服务状态

processes:
"映像名称","PID","会话名      ","会话#   ","内存使用 "
"System Idle Process","0","Services","0","8 K"
"System","4","Services","0","152 K"
"Registry","116","Services","0","65,260 K"
"smss.exe","428","Services","0","1,300 K"
"csrss.exe","676","Services","0","6,688 K"
"wininit.exe","756","Services","0","7,180 K"
"csrss.exe","776","Console","1","7,276 K"
"winlogon.exe","848","Console","1","11,376 K"
"services.exe","856","Services","0","11,156 K"
"lsass.exe","912","Services","0","19,052 K"
"svchost.exe","324","Services","0","29,436 K"
"fontdrvhost.exe","628","Console","1","39,280 K"
"fontdrvhost.exe","632","Services","0","14,708 K"
"WUDFHost.exe","968","Services","0","5,756 K"
"svchost.exe","1064","Services","0","13,104 K"
"svchost.exe","1124","Services","0","11,572 K"
"WUDFHost.exe","1160","Services","0","7,564 K"
"svchost.exe","1312","Services","0","13,168 K"
"svchost.exe","1352","Services","0","5,632 K"
"svchost.exe","1360","Services","0","12,220 K"
"svchost.exe","1460","Services","0","12,492 K"
"svc...

9.安全与更新

updates:
  Description      HotFixID   InstalledOn

Update           KB5066139  10/31/2025

SecurityUpdate  KB5012170  1/2/2023

Update           KB5014831  11/22/2022

SecurityUpdate  KB5078766  3/16/2026

SecurityUpdate  KB5078763  3/16/2026

SecurityUpdate  KB5034439  2/6/2024
firewall_status:
  域配置文件 设置:
----------------------------------------------------------------------
  状态                                  启用

  专用配置文件 设置:
----------------------------------------------------------------------
  状态                                  启用

  公用配置文件 设置:
----------------------------------------------------------------------
  状态                                  启用
  确定。

10.用户与登录

local_users:
  \\LIUJUNJUN-WORKS 的用户帐户

  -------------------------------------------------------------------------------
  Administrator            DefaultAccount           Guest
  WDAGUtilityAccount
  命令成功完成。
current_logins:
  用户名                会话名             ID  状态    空闲时间   登录时间
  >administrator         console             1  运行中      无     2026/3/3118:08
user_groups:
  \\LIUJUNJUN-WORKS 的别名

  -------------------------------------------------------------------------------
  *__vmware__
  *Access Control Assistance Operators
  *Administrators
  *Backup Operators
  *Certificate Service DCOM Access
  *Cryptographic Operators
  *Device Owners
  *Distributed COM Users
  *Event Log Readers
  *Guests
  *Hyper-V Administrators
  *IIS_IUSRS
  *Network Configuration Operators
  *Performance Log Users
  *Performance Monitor Users
  *Power Users
  *Print Operators
  *RDS Endpoint Servers
  *RDS Management Servers
  *RDS Remote Access Servers
  *Remote Desktop Users
  *Remote Management Users
  *Replicator
  *Storage Replica Administrators
  *System Managed Accounts Group
  *Users
  *WSUS Administrators
  *WSUS Reporters
  命令成功完成。

11.系统日志(最近24小时)

system_error_logs:
  Event[0]
  Log Name: System
  Source: VBoxNetLwf
  Date: 2026-01-12T15:27:51.0590000Z
  Event ID: 12
  Task: N/A
  Level: 错误
  Opcode: N/A
  Keyword: 经典
  User: N/A
  User Name: N/A
  Computer: liujunjun-Workstation
  Description:
驱动程序在 \Device\VBoxNetLwf 上检测到内部驱动程序错误。

Event[1]
  Log Name: System
  Source: Microsoft-Windows-Ntfs
  Date: 2026-01-12T15:27:53.4960000Z
  Event ID: 98
  Task: N/A
  Level: 错误
  Opcode: 信息
  Keyword: N/A
  User: S-1-5-18
  User Name: NT AUTHORITY\SYSTEM
  Computer: liujunjun-Workstation
  Description:
卷 \\?\Volume{b7becba5-b167-4de7-bfcf-c793bf610e04} (\Device\HarddiskVolume6) 需要脱机一段时间以执行完整的 Chkdsk。请在本机通过命令行运行&nbsp;"CHKDSK /F",或者在本机或远程通过 PowerShell 运行&nbsp;"REPAIR-VOLUME <驱动器:>"。

Event[2]
&nbsp; Log Name: System
&nbsp; Source: Service Control Manager
&nbsp; Date:&nbsp;2026-01-12T15:27:55.4100000Z
&nbsp; Event ID:&nbsp;7000
&nbsp; Task:&nbsp;N/A
&nbsp; Level:&nbsp;错误
&nbsp; Opcode:&nbsp;N/A
&nbsp; Keyword: 经典
&nbsp; User:&nbsp;N/A
&nbsp; User Name:&nbsp;N/A
&nbsp; Computer: liujunjun-Workstation
&nbsp; Description:
由于下列错误,luafv 服务启动失败:
此驱动程序被阻止加载

Event[...
application_error_logs:
&nbsp; Event[0]
&nbsp; Log Name: Application
&nbsp; Source: Application Error
&nbsp; Date:&nbsp;2026-01-02T16:10:31.7650000Z
&nbsp; Event ID:&nbsp;1000
&nbsp; Task: 应用程序崩溃事件
&nbsp; Level:&nbsp;错误
&nbsp; Opcode: 信息
&nbsp; Keyword: 经典
&nbsp; User:&nbsp;N/A
&nbsp; User Name:&nbsp;N/A
&nbsp; Computer: liujunjun-Workstation
&nbsp; Description:
错误应用程序名称: JZDLLCatchTip.exe,版本:&nbsp;2.0.0.0,时间戳:&nbsp;0x68946f81
错误模块名称: JZDLLCatchTip.exe,版本:&nbsp;2.0.0.0,时间戳:&nbsp;0x68946f81
异常代码:&nbsp;0xc0000005
错误偏移量:&nbsp;0x0007a851
错误进程 ID:&nbsp;0x21964
错误应用程序启动时间:&nbsp;0x01dc7bbf438e7aa8
错误应用程序路径: E:\qudong\JZDriver\JZDLLCatchTip.exe
错误模块路径: E:\qudong\JZDriver\JZDLLCatchTip.exe
报告 ID:&nbsp;358edc02-f646-4b6d-a076-adda978e9db9
错误程序包全名:
错误程序包相对应用程序 ID:

Event[1]
&nbsp; Log Name: Application
&nbsp; Source: Application Error
&nbsp; Date:&nbsp;2026-01-02T16:10:33.7350000Z
&nbsp; Event ID:&nbsp;1000
&nbsp; Task: 应用程序崩溃事件
&nbsp; Level:&nbsp;错误
&nbsp; Opcode: 信息
&nbsp; Keyword: 经典
&nbsp; User:&nbsp;N/A
&nbsp; User Name:&nbsp;N/A
&nbsp; Computer: liujunjun-Workstation
&nbsp; Description:
错误应用程序名称: JZDLLCatchTip.exe,版本:&nbsp;2.0.0.0,时间戳:&nbsp;0x68946f81
错误模块名称: JZDLLCatchTip.exe,版本:&nbsp;2.0.0.0,时间戳:&nbsp;0x68946f81
异常代码:&nbsp;0xc0000005
错误偏移量:&nbsp;0x...

12.性能指标

cpu_percent:13
network_stats:
&nbsp; 接口统计

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;接收的 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;发送的

&nbsp; 字节 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;78221853052067139
&nbsp; 单播数据包 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;586434300372
&nbsp; 非单播数据包 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;3306560
&nbsp; 丢弃 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;00
&nbsp; 错误 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;00
&nbsp; 未知协议 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;0

13.Python脚本

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Windows 系统一键巡检脚本
Python 3.x 版本
"""

import&nbsp;os
import&nbsp;sys
import&nbsp;platform
import&nbsp;socket
import&nbsp;datetime
import&nbsp;subprocess
import&nbsp;json
import&nbsp;csv
from&nbsp;typing&nbsp;import&nbsp;Dict, List, Any, Optional

defcheck_os()&nbsp;-> bool:
"""检查是否为Windows系统"""
return&nbsp;os.name ==&nbsp;'nt'

defrun_command(cmd: str, capture_output: bool = True, shell: bool = True)&nbsp;-> Dict[str, Any]:
"""执行系统命令"""
try:
&nbsp; &nbsp; &nbsp; &nbsp; result = subprocess.run(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; capture_output=capture_output,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text=True,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shell=shell,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeout=30
&nbsp; &nbsp; &nbsp; &nbsp; )
return&nbsp;{
'success': result.returncode ==&nbsp;0,
'returncode': result.returncode,
'stdout': result.stdout,
'stderr': result.stderr
&nbsp; &nbsp; &nbsp; &nbsp; }
except&nbsp;Exception&nbsp;as&nbsp;e:
return&nbsp;{
'success':&nbsp;False,
'error': str(e)
&nbsp; &nbsp; &nbsp; &nbsp; }

defget_system_info()&nbsp;-> Dict[str, Any]:
"""获取系统基本信息"""
&nbsp; &nbsp; info = {}

&nbsp; &nbsp; info['os_name'] = platform.system()
&nbsp; &nbsp; info['os_version'] = platform.version()
&nbsp; &nbsp; info['os_release'] = platform.release()
&nbsp; &nbsp; info['os_architecture'] = platform.architecture()[0]
&nbsp; &nbsp; info['machine_type'] = platform.machine()
&nbsp; &nbsp; info['hostname'] = socket.gethostname()

try:
&nbsp; &nbsp; &nbsp; &nbsp; info['fqdn'] = socket.getfqdn()
except&nbsp;Exception:
&nbsp; &nbsp; &nbsp; &nbsp; info['fqdn'] =&nbsp;'N/A'

&nbsp; &nbsp; info['username'] = os.environ.get('USERNAME',&nbsp;'N/A')
&nbsp; &nbsp; info['userdomain'] = os.environ.get('USERDOMAIN',&nbsp;'N/A')
&nbsp; &nbsp; info['processor_architecture'] = os.environ.get('PROCESSOR_ARCHITECTURE',&nbsp;'N/A')
&nbsp; &nbsp; info['number_of_processors'] = os.environ.get('NUMBER_OF_PROCESSORS',&nbsp;'N/A')

return&nbsp;info

defget_uptime()&nbsp;-> Dict[str, Any]:
"""获取系统运行时间"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic os get lastbootuptime /value')
if&nbsp;result['success']&nbsp;and&nbsp;result['stdout']:
for&nbsp;line&nbsp;in&nbsp;result['stdout'].strip().split('\n'):
if'='in&nbsp;line:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key, value = line.split('=',&nbsp;1)
if&nbsp;key.strip() ==&nbsp;'LastBootUpTime':
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boot_time_str = value.strip()
if&nbsp;len(boot_time_str) >=&nbsp;14:
try:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; year = boot_time_str[0:4]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; month = boot_time_str[4:6]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; day = boot_time_str[6:8]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hour = boot_time_str[8:10]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minute = boot_time_str[10:12]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second = boot_time_str[12:14]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boot_time = datetime.datetime(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int(year), int(month), int(day),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int(hour), int(minute), int(second)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_time = datetime.datetime.now()
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uptime = current_time - boot_time

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['boot_time'] = boot_time.strftime('%Y-%m-%d %H:%M:%S')
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['uptime_days'] = uptime.days
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['uptime_hours'] = int(uptime.total_seconds() //&nbsp;3600)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['uptime_minutes'] = int(uptime.total_seconds() //&nbsp;60)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['uptime_seconds'] = int(uptime.total_seconds())
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['parse_error'] = str(e)
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_license_status()&nbsp;-> Dict[str, Any]:
"""获取许可证与激活状态"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; slmgr_path = os.path.join(os.environ.get('SystemRoot',&nbsp;'C:\\Windows'),&nbsp;'System32',&nbsp;'slmgr.vbs')
if&nbsp;os.path.exists(slmgr_path):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = run_command(f'cscript //Nologo "{slmgr_path}" /dli')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['license_info'] = result['stdout'].strip()
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['license_info'] =&nbsp;'无法获取许可证信息(可能需要管理员权限)'
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['license_info'] =&nbsp;'未找到许可证管理工具'
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_processor_info()&nbsp;-> Dict[str, Any]:
"""获取处理器信息"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic cpu get name,manufacturer,maxclockspeed,numberofcores,numberoflogicalprocessors,loadpercentage /format:list')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cpu_info = {}
for&nbsp;line&nbsp;in&nbsp;result['stdout'].strip().split('\n'):
if'='in&nbsp;line:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key, value = line.split('=',&nbsp;1)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cpu_info[key.strip()] = value.strip()
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['cpu_details'] = cpu_info
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_memory_info()&nbsp;-> Dict[str, Any]:
"""获取内存信息"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic computersystem get totalphysicalmemory /value')
if&nbsp;result['success']:
for&nbsp;line&nbsp;in&nbsp;result['stdout'].strip().split('\n'):
if'='in&nbsp;line:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key, value = line.split('=',&nbsp;1)
if&nbsp;key.strip() ==&nbsp;'TotalPhysicalMemory':
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total_bytes = int(value.strip())
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['total_memory_gb'] = round(total_bytes / (1024**3),&nbsp;2)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['total_memory_kb'] = total_bytes //&nbsp;1024

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic os get freephysicalmemory /value')
if&nbsp;result['success']:
for&nbsp;line&nbsp;in&nbsp;result['stdout'].strip().split('\n'):
if'='in&nbsp;line:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key, value = line.split('=',&nbsp;1)
if&nbsp;key.strip() ==&nbsp;'FreePhysicalMemory':
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free_kb = int(value.strip())
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['free_memory_gb'] = round(free_kb *&nbsp;1024&nbsp;/ (1024**3),&nbsp;2)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['free_memory_kb'] = free_kb

if'total_memory_gb'in&nbsp;info&nbsp;and'free_memory_gb'in&nbsp;info:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['used_memory_gb'] = round(info['total_memory_gb'] - info['free_memory_gb'],&nbsp;2)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['memory_percent'] = round((info['used_memory_gb'] / info['total_memory_gb']) *&nbsp;100,&nbsp;1)

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic memorychip get capacity,speed,manufacturer,partnumber /format:table')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['memory_chips'] = result['stdout'].strip()
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_disk_info()&nbsp;-> Dict[str, Any]:
"""获取磁盘存储信息"""
&nbsp; &nbsp; info = {}
&nbsp; &nbsp; info['partitions'] = []

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic logicaldisk get deviceid,volumename,size,freespace,description /format:csv')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines = result['stdout'].strip().split('\n')
for&nbsp;line&nbsp;in&nbsp;lines[1:]:
if&nbsp;line.strip():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parts = line.split(',')
if&nbsp;len(parts) >=&nbsp;5:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; device_id = parts[1].strip('"')
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volume_name = parts[2].strip('"')
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = parts[3].strip('"')
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free_space = parts[4].strip('"')
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description = parts[5].strip('"')&nbsp;if&nbsp;len(parts) >&nbsp;5else''

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partition = {
'device_id': device_id,
'volume_name': volume_name,
'description': description
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

try:
if&nbsp;size:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size_bytes = int(size)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partition['size_gb'] = round(size_bytes / (1024**3),&nbsp;2)
if&nbsp;free_space:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free_bytes = int(free_space)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partition['free_gb'] = round(free_bytes / (1024**3),&nbsp;2)
if'size_gb'in&nbsp;partition:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partition['used_gb'] = round(partition['size_gb'] - partition['free_gb'],&nbsp;2)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partition['used_percent'] = round((partition['used_gb'] / partition['size_gb']) *&nbsp;100,&nbsp;1)
except&nbsp;Exception:
pass

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['partitions'].append(partition)
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_network_info()&nbsp;-> Dict[str, Any]:
"""获取网络配置信息"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('ipconfig /all')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['ipconfig'] = result['stdout']

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('netstat -an')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; established_connections = []
for&nbsp;line&nbsp;in&nbsp;result['stdout'].split('\n'):
if'ESTABLISHED'in&nbsp;line:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; established_connections.append(line.strip())
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['established_connections'] = established_connections[:20]
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_process_service_status()&nbsp;-> Dict[str, Any]:
"""获取进程与服务状态"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('tasklist /fo csv')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['processes'] = result['stdout'].strip()

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('sc query type= service state= running')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['running_services'] = result['stdout'].strip()
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_security_updates()&nbsp;-> Dict[str, Any]:
"""获取安全与更新状态"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic qfe get hotfixid,installedon,description /format:table')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['updates'] = result['stdout'].strip()

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('netsh advfirewall show allprofiles state')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['firewall_status'] = result['stdout'].strip()
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = run_command('netsh firewall show state')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['firewall_status'] = result['stdout'].strip()
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_users_login()&nbsp;-> Dict[str, Any]:
"""获取用户与登录信息"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('net user')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['local_users'] = result['stdout'].strip()

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('quser')
ifnot&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = run_command('query user')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['current_logins'] = result['stdout'].strip()
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['current_logins'] =&nbsp;'无法获取登录信息(可能需要管理员权限)'

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('net localgroup')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['user_groups'] = result['stdout'].strip()
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_system_logs()&nbsp;-> Dict[str, Any]:
"""获取系统日志(最近24小时)"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wevtutil qe System /q:"*[System[Level=2]]" /f:text /c:10')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['system_error_logs'] = result['stdout'].strip()
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['system_error_logs'] =&nbsp;'需要管理员权限查看日志或wevtutil工具不可用'

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wevtutil qe Application /q:"*[System[Level=2]]" /f:text /c:10')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['application_error_logs'] = result['stdout'].strip()
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['application_error_logs'] =&nbsp;'需要管理员权限查看日志或wevtutil工具不可用'
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defget_performance_metrics()&nbsp;-> Dict[str, Any]:
"""获取性能指标"""
&nbsp; &nbsp; info = {}

try:
&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('wmic cpu get loadpercentage /value')
if&nbsp;result['success']:
for&nbsp;line&nbsp;in&nbsp;result['stdout'].strip().split('\n'):
if'='in&nbsp;line:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key, value = line.split('=',&nbsp;1)
if&nbsp;key.strip() ==&nbsp;'LoadPercentage':
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['cpu_percent'] = value.strip()

&nbsp; &nbsp; &nbsp; &nbsp; result = run_command('netstat -e')
if&nbsp;result['success']:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info['network_stats'] = result['stdout'].strip()
except&nbsp;Exception&nbsp;as&nbsp;e:
&nbsp; &nbsp; &nbsp; &nbsp; info['error'] = str(e)

return&nbsp;info

defprint_section(title: str, data: Dict[str, Any]):
"""打印章节内容"""
&nbsp; &nbsp; print(f"\n{'='&nbsp;*&nbsp;60}")
&nbsp; &nbsp; print(f" &nbsp;{title}")
&nbsp; &nbsp; print('='&nbsp;*&nbsp;60)

defprint_dict(d: Dict, indent: int =&nbsp;0):
for&nbsp;key, value&nbsp;in&nbsp;d.items():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefix =&nbsp;' '&nbsp;* indent
if&nbsp;isinstance(value, dict):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}{key}:")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_dict(value, indent +&nbsp;2)
elif&nbsp;isinstance(value, list):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}{key}:&nbsp;{len(value)}&nbsp;项")
for&nbsp;i, item&nbsp;in&nbsp;enumerate(value[:5]):
if&nbsp;isinstance(item, dict):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}&nbsp; [{i+1}]")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_dict(item, indent +&nbsp;4)
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}&nbsp; [{i+1}]&nbsp;{item}")
if&nbsp;len(value) >&nbsp;5:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}&nbsp; ... 等&nbsp;{len(value) -&nbsp;5}&nbsp;项")
elif&nbsp;isinstance(value, str)&nbsp;and&nbsp;len(value) >&nbsp;500:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}{key}:")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}&nbsp;&nbsp;{value[:500]}...")
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"{prefix}{key}:&nbsp;{value}")

&nbsp; &nbsp; print_dict(data)

defwrite_dict_to_file(f, d: Dict, indent: int =&nbsp;0):
"""将字典写入文件"""
for&nbsp;key, value&nbsp;in&nbsp;d.items():
&nbsp; &nbsp; &nbsp; &nbsp; prefix =&nbsp;' '&nbsp;* indent
if&nbsp;isinstance(value, dict):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}{key}:\n")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write_dict_to_file(f, value, indent +&nbsp;2)
elif&nbsp;isinstance(value, list):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}{key}:&nbsp;{len(value)}&nbsp;项\n")
for&nbsp;i, item&nbsp;in&nbsp;enumerate(value):
if&nbsp;isinstance(item, dict):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}&nbsp; [{i+1}]\n")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write_dict_to_file(f, item, indent +&nbsp;4)
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}&nbsp; [{i+1}]&nbsp;{item}\n")
elif&nbsp;isinstance(value, str):
if&nbsp;len(value) >&nbsp;1000:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}{key}:\n{prefix}&nbsp;&nbsp;{value[:1000]}...\n")
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines = value.split('\n')
if&nbsp;len(lines) >&nbsp;1:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}{key}:\n")
for&nbsp;line&nbsp;in&nbsp;lines:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}&nbsp;&nbsp;{line}\n")
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}{key}:&nbsp;{value}\n")
else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f"{prefix}{key}:&nbsp;{value}\n")

defgenerate_report(data: Dict[str, Any], section_titles: Dict[str, str]):
"""生成TXT格式报告文件"""
&nbsp; &nbsp; timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
&nbsp; &nbsp; report_file =&nbsp;f"System_Inspection_Report_{timestamp}.txt"

with&nbsp;open(report_file,&nbsp;'w', encoding='utf-8')&nbsp;as&nbsp;f:
# 写入报告标题
&nbsp; &nbsp; &nbsp; &nbsp; f.write("="&nbsp;*&nbsp;60&nbsp;+&nbsp;"\n")
&nbsp; &nbsp; &nbsp; &nbsp; f.write(" &nbsp;Windows 系统一键巡检报告\n")
&nbsp; &nbsp; &nbsp; &nbsp; f.write("="&nbsp;*&nbsp;60&nbsp;+&nbsp;"\n")
&nbsp; &nbsp; &nbsp; &nbsp; f.write(f"报告时间:&nbsp;{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
&nbsp; &nbsp; &nbsp; &nbsp; f.write(f"Python版本:&nbsp;{platform.python_version()}\n")
&nbsp; &nbsp; &nbsp; &nbsp; f.write("="&nbsp;*&nbsp;60&nbsp;+&nbsp;"\n\n")

# 写入各章节内容
for&nbsp;key, title&nbsp;in&nbsp;section_titles.items():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write("\n"&nbsp;+&nbsp;"="&nbsp;*&nbsp;60&nbsp;+&nbsp;"\n")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write(f" &nbsp;{title}\n")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.write("="&nbsp;*&nbsp;60&nbsp;+&nbsp;"\n\n")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write_dict_to_file(f, data[key])

&nbsp; &nbsp; &nbsp; &nbsp; f.write("\n"&nbsp;+&nbsp;"="&nbsp;*&nbsp;60&nbsp;+&nbsp;"\n")
&nbsp; &nbsp; &nbsp; &nbsp; f.write(" &nbsp;巡检完成\n")
&nbsp; &nbsp; &nbsp; &nbsp; f.write("="&nbsp;*&nbsp;60&nbsp;+&nbsp;"\n")

&nbsp; &nbsp; print(f"\n{'='&nbsp;*&nbsp;60}")
&nbsp; &nbsp; print(f"巡检报告已保存到:&nbsp;{report_file}")
return&nbsp;report_file

defmain():
"""主函数"""
&nbsp; &nbsp; print("="&nbsp;*&nbsp;60)
&nbsp; &nbsp; print(" &nbsp;Windows 系统一键巡检脚本")
&nbsp; &nbsp; print("="&nbsp;*&nbsp;60)
&nbsp; &nbsp; print(f"开始巡检:&nbsp;{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
&nbsp; &nbsp; print()

ifnot&nbsp;check_os():
&nbsp; &nbsp; &nbsp; &nbsp; print("错误: 此脚本仅适用于Windows操作系统")
&nbsp; &nbsp; &nbsp; &nbsp; input("\n按回车键退出...")
return

&nbsp; &nbsp; inspection_data = {
'system_info': get_system_info(),
'uptime': get_uptime(),
'license_status': get_license_status(),
'processor_info': get_processor_info(),
'memory_info': get_memory_info(),
'disk_info': get_disk_info(),
'network_info': get_network_info(),
'process_service_status': get_process_service_status(),
'security_updates': get_security_updates(),
'users_login': get_users_login(),
'system_logs': get_system_logs(),
'performance_metrics': get_performance_metrics()
&nbsp; &nbsp; }

&nbsp; &nbsp; section_titles = {
'system_info':&nbsp;'1. 系统基本信息',
'uptime':&nbsp;'2. 系统运行时间',
'license_status':&nbsp;'3. 许可证与激活状态',
'processor_info':&nbsp;'4. 处理器信息',
'memory_info':&nbsp;'5. 内存信息',
'disk_info':&nbsp;'6. 磁盘存储信息',
'network_info':&nbsp;'7. 网络配置信息',
'process_service_status':&nbsp;'8. 进程与服务状态',
'security_updates':&nbsp;'9. 安全与更新',
'users_login':&nbsp;'10. 用户与登录',
'system_logs':&nbsp;'11. 系统日志(最近24小时)',
'performance_metrics':&nbsp;'12. 性能指标'
&nbsp; &nbsp; }

for&nbsp;key, title&nbsp;in&nbsp;section_titles.items():
&nbsp; &nbsp; &nbsp; &nbsp; print_section(title, inspection_data[key])

&nbsp; &nbsp; generate_report(inspection_data, section_titles)

&nbsp; &nbsp; print("\n"&nbsp;+&nbsp;"="&nbsp;*&nbsp;60)
&nbsp; &nbsp; print(f"巡检完成:&nbsp;{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
&nbsp; &nbsp; print("="&nbsp;*&nbsp;60)
&nbsp; &nbsp; input("\n按回车键退出...")

if&nbsp;__name__ ==&nbsp;'__main__':
&nbsp; &nbsp; main()
  • Python版本3.13,可以在cmd中直接执行。
  • 也可以在PyCharm中直接执行。输出会保存在一个txt文本中。

免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:运维星火燎原 刘军军 刘军军《Windows 系统Python一键巡检报告》

评论:0   参与:  0