修改URL和username password为自己的系统即可
#!/usr/bin/python3 # -*- coding: utf-8 -* # Copyright (C) 2023 - 2023 zhoujt, Inc. All Rights Reserved # @Date : 2023/5/15 18:03 # @Author : zhoujt # @Email : [email protected] # @FileName: ZabbixGet_all_host.py # @IDE : PyCharm import requests import json # 获取token class Zabbix: def __init__(self, user, password): self.user = user self.password = password self.session = requests.Session() self.host = 'http://zabbix.zhoujt.com' self.url = self.host + '/api_jsonrpc.php' self.login() def login(self): data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": self.user, "password": self.password }, "id": 1, "auth": None } response = self.session.post(self.url, json=data) # 获取结果中的token self.auth = response.json()['result'] def get_hosts(self): auth_token = self.auth # 构造API请求的数据 data = { 'jsonrpc': '2.0', 'method': 'host.get', 'params': { 'output': ['hostid', 'host', 'status'], 'selectInterfaces': ['ip'] }, 'auth': auth_token, 'id': 1 } url = self.url headers = {'Content-Type': 'application/json'} # 发送API请求 response = requests.post(url, headers=headers, data=json.dumps(data)) # 解析响应数据 result = json.loads(response.text) stop_host = [] # 提取主机信息 if 'result' in result: hosts = result['result'] sum_1 = 0 for host in hosts: sum_1 += 1 host_id = host['hostid'] host_name = host['host'] ip_address = host['interfaces'][0]['ip'] status = host['status'] print(f"Host ID: {host_id}, Name: {host_name}, IP: {ip_address}, status: {status}") if int(status) == 1: stop_host.append(ip_address) else: print("Failed to retrieve hosts.") print("Host sum: %s" % sum_1) stop_1 = 0 print("=======The stop hosts=======\n") for i in stop_host: stop_1 += 1 print(i) print("Stop hosts: %s" % stop_1) zab = Zabbix(user="zhoujt", password="password") zab.get_hosts()
标签:主机,self,stop,zabbix,host,hosts,全量,password,user From: https://www.cnblogs.com/security-guard/p/get_zabbix_host.html