会挽雕弓如|

园龄:粉丝:关注:

[python3] 获取zabbix上所有主机的ip

get_zabbix_hosts.py

import requests                                                                                                                                            
import json                                                                                                                                                
                                                                                                                                                           
# Zabbix API的URL和认证信息                                                                                                                                
zabbix_url = ''                                                                                                                                            
zabbix_user = ''                                                                                                                                           
zabbix_password = r''                                                                                                                                      
                                                                                                                                                           
# 获取Zabbix API的认证令牌                                                                                                                                 
def get_auth_token(url, user, password):                                                                                                                   
    payload = {                                                                                                                                            
        "jsonrpc": "2.0",                                                                                                                                  
        "method": "user.login",                                                                                                                            
        "params": {                                                                                                                                        
            "user": user,                                                                                                                                  
            "password": password                                                                                                                           
        },                                                                                                                                                 
        "id": 1,                                                                                                                                           
        "auth": None                                                                                                                                       
    }                                                                                                                                                      
    headers = {'content-type': 'application/json'}                                                                                                         
    response = requests.post(url, data=json.dumps(payload), headers=headers)                                                                               
    result = response.json()                                                                                                                               
    return result['result']                                                                                                                                
                                                                                                                                                           
# 使用Zabbix API获取所有主机信息                                                                                                                           
def get_all_hosts(url, auth_token):                                                                                                                        
    payload = {                                                                                                                                            
        "jsonrpc": "2.0",                                                                                                                                  
        "method": "host.get",                                                                                                                              
        "params": {                                                                                                                                        
            "output": ["hostid", "name", "interfaces"],                                                                                                    
            "selectInterfaces": ["ip"]                                                                                                                     
        },                                                                                                                                                 
        "id": 2,                                                                                                                                           
        "auth": auth_token                                                                                                                                 
    }                                                                                                                                                      
    headers = {'content-type': 'application/json'}                                                                                                         
    response = requests.post(url, data=json.dumps(payload), headers=headers)                                                                               
    result = response.json()                                                                                                                               
    return result['result']                                                                                                                                
                                                                                                                                                           
# 主程序                                                                                                                                                   
if __name__ == "__main__":                                                                                                                                 
    # 获取认证令牌                                                                                                                                         
    auth_token = get_auth_token(zabbix_url, zabbix_user, zabbix_password)                                                                                  
                                                                                                                                                           
    # 获取所有主机信息                                                                                                                                     
    hosts = get_all_hosts(zabbix_url, auth_token)                                                                                                          
                                                                                                                                                           
    # 打印所有主机的IP地址                                                                                                                                 
    for host in hosts:                                                                                                                                     
        for interface in host['interfaces']:                                                                                                               
            print(f"Host: {host['name']} - IP: {interface['ip']}") 

本文作者:武平宁

本文链接:https://www.cnblogs.com/dewan/p/18623988

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   武平宁  阅读(9)  评论(0编辑  收藏  举报
历史上的今天:
2023-12-23 Linux: 自己动手写个命令行翻译程序
2023-12-23 Linux Shell: 写程序 背单词
2022-12-23 TCP 链接关闭 -- 客户端为什么需要60秒的time_wait状态
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起