调用zabbx的api接口获取IP地址和主机名
Posted 我的紫霞辣辣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用zabbx的api接口获取IP地址和主机名相关的知识,希望对你有一定的参考价值。
调用zabbx的api接口,获取zabbix上所有主机的IP和主机名
#coding:utf-8
#获取zabbix上所有主机的IP和主机名
import requests
import json
import csv
import time
def get_token():
data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": username,
"password": password
},
"id": 0
}
r = requests.get(zaurl, verify=False, headers=header, data=json.dumps(data))
auth = json.loads(r.text)
return auth["result"]
def getHosts(token):
data = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": token,
}
request = requests.post(zaurl, verify=False, headers=header, data=json.dumps(data))
dict = json.loads(request.content)
#print (dict['result'])
return dict['result']
if __name__ == "__main__":
zaurl="https://1.1.1.1/zabbix/api_jsonrpc.php"
header = {"Content-Type": "application/json"}
username = "nana"
password = "123"
token = get_token()
hostlist = getHosts(token)
datafile = "zabbix.txt"
fdata = open(datafile,'w')
for i in hostlist:
# hostid = i['hostid']
name = i['host']
hostIP = i['interfaces']
hostip = hostIP[0]["ip"]
fdata.write(name + ' ' + hostip + '\\n')
fdata.close()
以上是关于调用zabbx的api接口获取IP地址和主机名的主要内容,如果未能解决你的问题,请参考以下文章
python3调用zabbix api接口对自动发现添加的主机修改主机名