import requests from requests import request import json from collections import OrderedDict url = ‘https://zabbix-xn.test.bestpay.net/api_jsonrpc.php‘ headers = {"Content-Type": "application/json-rpc"} def log_zabbix(): data = { "jsonrpc": "2.0", "method": "user.login", "id": 1, "params": { "user": "quemengqiao", "password": "Password6!" } } dict_to_str = json.dumps(data) res = requests.post(url,data = dict_to_str, headers=headers) content_str = res.text print(content_str) str_to_dict = json.loads(content_str) print(str_to_dict) return str_to_dict[‘result‘] print(log_zabbix()) def host_get_id(): _auth = log_zabbix() print(_auth) data = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["host"] }, "auth":"%s" % _auth, "id": 1 } dict_to_str = json.dumps(data) res = requests.post(url,data=dict_to_str,headers=headers) content_str = res.text str_to_dict = json.loads(content_str) print(str_to_dict) host_id_ip=(str_to_dict[‘result‘]) print((host_id_ip)) aaa=json.dumps(host_id_ip) for i in host_id_ip: ip_list ="172.17.244.168" ccc = dict(i) if ip_list == ccc[‘host‘]: print(ccc[‘hostid‘]) print(ccc[‘host‘]) return ccc[‘hostid‘] print(host_get_id()) def group_add(): _auth=log_zabbix() _host_id = host_get_id() print(_host_id) data = { "jsonrpc": "2.0", "method": "hostgroup.massadd", "params": { "groups":[ { "groupid":"18", } ], "hosts":[ { "hostid":"%s" %_host_id } ] }, "auth":"%s" %_auth, "id": 1 } dict_to_str = json.dumps(data) res = requests.post(url, data=dict_to_str, headers=headers) content_str = res.text str_to_dict = json.loads(content_str) host_id_ip = (str_to_dict[‘result‘]) print(host_id_ip) group_add()