脚本内容如下:
#!/usr/bin/python #-*- coding:utf8 -*- import json,sys,argparse from zabbix_api import ZabbixAPI server = "http://172.16.206.128/zabbix" username = "Admin" password = "zabbix" zapi = ZabbixAPI(server=server, path="", log_level=0) zapi.login(username, password) def get_args(): parser = argparse.ArgumentParser() parser.add_argument("-H", "--host", help="host name") # 解析所传入的参数 args = parser.parse_args() if not args.host: args.host = raw_input(‘host: ‘) return args def get_host_id(host): get_host_id = zapi.host.get( { "output": "hostid", "filter": { "host":host.split(",") } } ) host_id = [] host_id.append([I[‘hostid‘] for I in get_host_id]) return host_id[0] def delete_host(hosts_id): hosts_delete = zapi.host.delete(hosts_id) return "host delete success!" if __name__ == "__main__": args = get_args() host_id = get_host_id(args.host) print delete_host(host_id)
脚本使用:
# python host_delete.py --host=‘aaa,bbb‘ host delete success!
时间: 2024-10-11 02:51:48