获取zabbix全量主机
修改URL和username password为自己的系统即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #!/usr/bin/python3 # -*- coding: utf-8 -* # Copyright (C) 2023 - 2023 zhoujt, Inc. All Rights Reserved # @Date : 2023/5/15 18:03 # @Author : zhoujt # @Email : zhouxiaobu1022@gmail.com # @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() |
本文作者:Security
本文链接:https://www.cnblogs.com/security-guard/p/get_zabbix_host.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步