zabbix批量添加交换机脚本

原创作者:运维工程师 谢晋

zabbix批量添加交换机脚本

#!/usr/bin/env python
import csv
from pyzabbix import ZabbixAPI

# Zabbix API credentials
ZABBIX_SERVER = 'http://10.11.100.41/zabbix'    # 修改为zabbixIP
ZABBIX_USER = 'Admin'                           # 账号
ZABBIX_PASSWORD = 'zabbix'                      # 密码

# Zabbix template and host group names
TEMPLATE_NAME = 'Template Net HP Comware HH3C SNMP'   # 板名称
HOST_GROUP_NAME = 'Switches'                    # 群主名称

# SNMP credentials
SNMP_COMMUNITY = 'jxpr'                         # 团体名称

# Read CSV file with switch names and IP addresses
with open('switches.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        switch_name = row[0]
        switch_ip = row[1]

        # Connect to Zabbix API
        zapi = ZabbixAPI(ZABBIX_SERVER)
        zapi.login(ZABBIX_USER, ZABBIX_PASSWORD)

        # Get template ID
        template_id = zapi.template.get(filter={'name': TEMPLATE_NAME})[0]['templateid']

        # Get host group ID
        hostgroup_id = zapi.hostgroup.get(filter={'name': HOST_GROUP_NAME})[0]['groupid']

        # Create host
        host_create = zapi.host.create(
            host=switch_name,
            interfaces=[{
                "type": 2,
                "main": 1,
                "useip": 1,
                "ip": switch_ip,
                "dns": "",
                "port": 161,  # SNMP port
                "details": {
                    "version": 2,  # SNMP version
                    "community": SNMP_COMMUNITY  # SNMP community string
                }
            }],
            groups=[{
                "groupid": hostgroup_id
            }],
            templates=[{
                "templateid": template_id
            }]
        )

        print("Switch added: %s" % switch_name)
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值