Win32_Services. Service retrieval through python from a remote machine

58 Views Asked by At

I am making a web application to monitor certain servers and part of that is monitoring specific services that should be running in those servers. I made a simple python script that tests the Win32_service function that should retrieve all running services. Here is the code:

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)

And this is my output: Successfully connected to 192.168.xxx.xxx Failed to connect to 192.168.xxx.xxx: <x_wmi: Unexpected COM Error (-2147217405, 'OLE error 0x80041003', None, None)>

Credentials are changed on purpose for security purposes but they are correct.

As you can see, the first connection is succesfull and this is because I have made every change necessary to grant permission for my computer to connect to the remote server. I have been searching for hours for a solution but I can't seem to find one. Does anyone know what permission I need to set up in order to be able to retrieve the services?

I tried giving permision to my local user from the remote service using dcomcnfg and wmimgmt.msc and allowing inbound rules in the firewall advance setiings as well as creating a new traffic WMI rule that should allow connections from unspecified hosts. (I know this is not secure I am testing the program first on a dummy server)

1

There are 1 best solutions below

1
Mikel Sanchez On

fixed it by running sc sdset SCMANAGER D:(A;;CCLCRPRC;;;AU)(A;;CCLCRPWPRC;;;SY)(A;;KA;;;BA)S:(AU;FA;KA;;;WD)(AU;OIIOFA;GA;;;WD) in the remote console