如何使用python检查其他服务器上运行的服务

发布于 2025-01-12 13:43:55 字数 711 浏览 3 评论 0原文

我在其他 Windows 或 Linux 服务器上检查我的服务时遇到问题。

我的问题是,我必须从一台服务器向其他服务器发出请求,并检查这些服务器的重要服务是否处于活动状态或禁用状态。

我编写了 Python 代码来检查服务,该代码仅适用于本地系统。

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")

这段代码有这个功能吗? 或者建议另一个代码�

我已经审查了诸如使用服务器代理(influx,...)之类的建议,这些建议无法满足我的需求。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带上头具痛哭 2025-01-19 13:43:55

您可以使用以下代码来为您提供服务。我认为这些代码会对你有帮助
在你的问题中。

            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...")

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...")
铃予 2025-01-19 13:43:55

据我所知,psutil只能用于收集本地进程的信息,不适合检索其他主机上运行的进程的信息。如果你想检查一个进程是否正在另一台主机上运行,​​有很多方法可以解决这个问题,解决方案取决于你想深入(或需要深入),以及你当地的情况是什么。从我的想法来看,这里有一些想法:

如果您只处理具有公开端口的网络服务:

  • 一个非常简单的解决方案将涉及使用脚本和端口扫描器(nmap);如果服务正在侦听的端口打开,那么我们可以假设该服务正在运行。每隔一段时间运行一次脚本来检查服务,然后做你的事情。

  • 如果您想继续使用 Python,您可以通过使用 Python 的 socket 模块尝试连接到给定的主机和端口来确定该端口是否有服务来实现相同的最终结果正在听后面,是开放的。

  • 用于监视其他主机上的网络服务的 Python 包或工具可能已经存在。

如果您想要更多信息并且需要更深入,或者您想要检查本地服务,您的解决方案将必须涉及每个主机上的本地监视器进程,并连接到该进程以收集信息。

  • 您可以使用代码来实现允许客户端连接到它的服务器,以检查该主机上运行的服务。 (查看socket 模块的官方文档,了解如何实现客户端和服务器的示例。)

不过,这才是最重要的。根据您的问题以及提出问题的方式,我认为您还没有以安全方式实施此问题的经验或洞察力。如果您将其用于简单的爱好/学生项目,请推出您自己的解决方案并学习。否则,我建议您查看现有的解决方案,例如 Nagios,并严格遵循安全建议。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文