需求
- 检测win系统依赖服务进程的运行情况,版本信息(进程检测器)
- 检测内外网连接情况
实现
进程检测
# 使用pywin32获取进程版本信息
def get_version_info(path):try:info = GetFileVersionInfo(path, '\\')ms = info['FileVersionMS']ls = info['FileVersionLS']version = f"{HIWORD(ms)}.{LOWORD(ms)}.{HIWORD(ls)}.{LOWORD(ls)}"return versionexcept Exception as e:print(f"无法获取版本信息: {e}")return Nonedef find_process_by_name(name, file):# 遍历活动进程for proc in psutil.process_iter(['pid', 'name', 'exe']):if name.lower() in proc.info['name'].lower():# 格式化输出print(f"{'-' * 40}进程 {name:^20} 存在.{'-' * 40}")print(f"找到进程: {proc.info['name']} (PID: {proc.info['pid']})")print(f"完整路径: {proc.info['exe']}")# 写入文件file.write(f"{'-' * 40}进程 {name:^20} 存在.{'-' * 40}\n")file.write(f"找到进程: {proc.info['name']} (PID: {proc.info['pid']})\n")file.write(f"完整路径: {proc.info['exe']}\n")# 获取版本信息version = get_version_info(proc.info['exe'])if version:print(f"版本信息: {version}")file.write(f"版本信息: {version}\n")print('-' * 112)print('\n')file.write('-' * 112 + '\n') file.write('\n')return Trueprint(f"\n未找到进程 {name}.\n")file.write(f"\n未找到进程 {name}.\n")return False
网络检测
def check_network(host, port=80):"""检查网络连接是否正常。"""try:socket.create_connection((host, port), 2)return Trueexcept socket.error:return False
主函数
if __name__ == '__main__':output_file = open("检测结果.txt", "w", encoding="utf-8")try: print("网络连接检测:") out_ = check_network("www.baidu.com")in_ = check_network("192.168.xxx.xxx")print("外网访问: ", "Success" if out_ else "Failed")print("内网访问: ", "Success" if in_ else "Failed")output_file.write(f"网络连接检测:\n外网访问: {'Success' if out_ else 'Failed'}\n"f"内网访问: {'Success' if in_ else 'Failed'}\n\n")# 定义要检查的进程列表processes = ["*******e.exe",.....]for process_name in processes:find_process_by_name(process_name, output_file)print("检测完成,结果已写入检测结果.txt")finally:output_file.close()input("按任意键退出...")
运行结果
打包exe
!!!!!!!管理员打开powershell!!!!!!!
设置策略:set-executionpolicy remotesigned (选择y或者点击是)
查看策略:get-executionpolicy
创建虚拟环境:python -m venv my_env
切换到虚拟环境目录下:cd my_env\Scripts
激活环境:activate
查看py环境依赖:pip list (干干净净没有其他的依赖包)
安装py文件依赖:psutil、pywin32、pyinstaller
执行打包命令:pyinstaller --noconfirm .\gadget.py (带依赖目录,启动较快,但是要带个尾巴)
pyinstaller -F .\gadget.py (单独一个exe包,但是启动巨慢)