首页 > 解决方案 > 我的python脚本中的变量没有被设置

问题描述

我有一个 python 脚本,它使用 pyzabbix 模块来查询 Zabbix 的 API。我有一个带有主机名的文本文件,我想在脚本中循环,但没有设置 hosts 变量。不知道我做错了什么。

from pyzabbix import ZabbixAPI, ZabbixAPIException
import sys

# The hostname at which the Zabbix web interface is available
ZABBIX_SERVER = 'https://zabbix.example.org'

zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.session.verify = False

# Login to the Zabbix API
zapi.login('Admin', 'password')

infile = open("data.txt", "r")
for line in infile:
    host_name = line
    hosts = zapi.host.get(filter={"host": host_name}, selectInterfaces=["interfaceid"]) 
    if hosts:
        host_id = hosts[0]["hostid"]
        print("Found host id {0}".format(host_id))
    else:
        print("No hosts found")

infile.close()

标签: python

解决方案


推荐阅读