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']}")
标签:url,ip,json,auth,token,zabbix,result,python3
From: https://www.cnblogs.com/dewan/p/18623988