我在其他 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,...),但这些建议无法满足我的需求。
最佳答案
您可以使用以下代码来为您提供服务。我认为这些代码会对你有帮助 在你的问题中。
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...")
关于python - 如何使用python检查其他服务器上运行的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71393915/