我正在制作一个 Web 应用程序来监视某些服务器,其中一部分是监视应在这些服务器中运行的特定服务。我制作了一个简单的 python 脚本来测试应该检索所有正在运行的服务的 Win32_service 函数。这是代码:
import wmi
def get_running_services(computer, user, password):
try:
# Connect to the remote WMI service
connection = wmi.WMI(computer=computer, user=user, password=password)
print(f"Successfully connected to {computer}")
# Get details about running services
running_services = []
for service in connection.Win32_Service(State="Running"):
running_services.append({
"name": service.Name,
"display_name": service.DisplayName,
"status": service.Status,
})
# Print details about each running service
for service in running_services:
print(f"Service Name: {service['name']}")
print(f"Display Name: {service['display_name']}")
print(f"State: {service['state']}")
print(f"Status: {service['status']}")
print("-" * 50)
return running_services
except wmi.x_wmi as e:
print(f"Failed to connect to {computer}: {str(e)}")
return None
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
return None
get_running_services(IP, User, Password)
这是我的输出:
成功连接192.168.xxx.xxx
无法连接到 192.168.xxx.xxx:
出于安全目的,凭证被故意更改,但它们是正确的。
如您所见,第一次连接成功,这是因为我已经进行了所有必要的更改,以授予我的计算机连接到远程服务器的权限。我已经花了几个小时寻找解决方案,但似乎找不到。有谁知道我需要设置什么权限才能检索服务?
我尝试使用 dcomcnfg 和 wmimgmt.msc 从远程服务向本地用户授予权限,并允许防火墙高级设置中的入站规则,以及创建一个新的流量 WMI 规则,该规则应允许来自未指定主机的连接。 (我知道这不安全,我首先在虚拟服务器上测试该程序)
通过运行 sc sdset SCMANAGER D:(A;;CCLCRPRC;;;AU)(A;;CCLCRPWPRC;;;SY)(A;;KA;;;BA)S:(AU;FA;KA;; 修复它;WD)(AU;OIIOFA;GA;;;WD) 在远程控制台