'how to check service running on other server with python

I have a problem with checking my service on other windows or Linux servers.

My problem is that I have to make a request from one server to the other servers and check if the vital services of those servers are active or disabled.

I wrote Python code to check for services, which only works on a local system.

import psutil
 
def getService(name):

    service = None
    try:
        service = psutil.win_service_get(name)
        service = service.as_dict()
    except Exception as ex:
        print(str(ex))
    return service

service = getService('LanmanServer')
if service:
    print("service found")
else:
    print("service not found")

if service and service['status'] == 'running':
    print("service is running")
else:
    print("service is not running")

Does this code have this feature? Or suggest another code؟

I have reviewed suggestions such as using server agents (influx, ...), which are not working for my needs.



Solution 1:[1]

You can use the following code for your service. i think these codes will help you in your problem.

            ip = your_ip
            server_user = your_serviceuser
            server_pass = your_pass
            command = f"net use \\\\{ip} {server_pass} /USER:{server_user}"
            os.system(command)
            command = f"SC \\\\{ip} query SQLSERVERAGENT"
            process = subprocess.Popen(command, stdout=subprocess.PIPE)
            output, err = process.communicate()
            
            output = str(str(str(str(output)[2:-1].replace(' ', '')).replace('\\t', '')).replace('\\r', '')).split('\\n')
            if output[3] != 'STATE:4RUNNING':
                print("service is running...")

Solution 2:[2]

As far as I know, psutil can only be used for gathering information about local processes, and is not suitable for retrieving information about processes running on other hosts. If you want to check whether or not a process is running on another host, there are many ways to approach this problem, and the solution depends on how deep you want to go (or need to go), and what your local situation is. From the top of my head, here are some ideas:

If you are only dealing with network services with exposed ports:

  • A very simple solution would involve using a script and a port scanner (nmap); if a port that a service is listening behind, is open, then we can assume that the service is running. Run the script every once in a while to check up on the services, and do your thing.

  • If you want to stay in Python, you can achieve the same end result by using Python's socket module to try and connect to a given host and port to determine whether or not the port that a service is listening behind, is open.

  • A Python package or tool for monitoring network services on other hosts like this probably already exists.

If you want more information and need to go deeper, or you want to check up on local services, your solution will have to involve a local monitor process on each host, and connecting to that process to gather information.

  • You can use your code to implement a server that lets clients connect to it, to check up on the services running on that host. (Check the socket module's official documentation for examples on how to implement clients and servers.)

Here's the big thing though. Based on your question and how it was asked, I would assume that you do not have the experience nor the insight to implement this in a secure way yet. If you're using this for a simple hobby/student project, roll out your own solution, and learn. Otherwise, I would recommend that you check out an existing solution like Nagios, and follow the security recommendations very closely.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Alan Verresen