# add your account id below# add a (zero trust read only) api token belowimporturllib.requestimportjsonaccount_id="foo"api_key="f00bar"headers={"Authorization":"Bearer {}".format(api_key),"content-type":"application/json",}defget_devices():list_complete=Falsecursor=""devices=[]whilelist_complete==False:req=urllib.request.Request("https://api.cloudflare.com/client/v4/accounts/{}/devices".format(account_id)+(""ifcursorisNoneelse"?cursor={}".format(cursor)),headers=headers,)withurllib.request.urlopen(req)asresponse:data=json.loads(response.read().decode())cursor=data["result_info"]["cursor"]ifcursor==data["result_info"]["cursor"]:list_complete=Truedevices.append(data["result"])return[itemforsublistindevicesforiteminsublist]defget_ip_addresses(devices):out=[]fordeviceindevices:device_id=device["id"]device_name=device["name"]req=urllib.request.Request("https://api.cloudflare.com/client/v4/accounts/{}/devices/{}/ip".format(account_id,device_id),headers=headers,)try:withurllib.request.urlopen(req)asresponse:output=json.loads(response.read().decode())excepturllib.error.HTTPErrorase:print("HTTPError: {}, {} accessing ID {}".format(e.code,e.reason,device_id))device_ipv4=output["result"]["result"]["ipv4"]device_ipv6=output["result"]["result"]["ipv6"]out.append({"id":device_id,"name":device_name,"ipv4":device_ipv4,"ipv6":device_ipv6,})returnoutif__name__=="__main__":devices=get_devices()ip_addresses=get_ip_addresses(devices)print(json.dumps(ip_addresses,indent=2))