python - Infoblox WAPI: how to search for an IP -
our network team uses infoblox store information ip ranges (location, country, etc.) there api available infoblox's documentation , examples not practical.
i search via api details ip. start - happy server. modified the example found
import requests import json url = "https://10.6.75.98/wapi/v1.0/" object_type = "network" search_string = {'network':'10.233.84.0/22'} response = requests.get(url + object_type, verify=false, data=json.dumps(search_string), auth=('adminname', 'adminpass')) print "status code: ", response.status_code print response.text
which returns error 400
status code: 400 { "error": "admconprotoerror: invalid input: '{\"network\": \"10.233.84.0/22\"}'", "code": "client.ibap.proto", "text": "invalid input: '{\"network\": \"10.233.84.0/22\"}'" }
i appreciate pointers managed api work python.
update: following on solution, below piece of code (it works not nice, streamlined, not checks errors, etc.) if 1 day have need same did.
def ip2site(myip): # argument ip want know localization of (in extensible_attributes) baseurl = "https://the_infoblox_address/wapi/v1.0/" # first network ip in r = requests.get(baseurl+"ipv4address?ip_address="+myip, auth=('youruser', 'yourpassword'), verify=false) j = simplejson.loads(r.content) # if ip not in network error message dumped, including among others key 'code' if 'code' not in j: mynetwork = j[0]['network'] # extended atributes network r = requests.get(baseurl+"network?network="+mynetwork+"&_return_fields=extensible_attributes", auth=('youruser', 'youpassword'), verify=false) j = simplejson.loads(r.content) location = j[0]['extensible_attributes']['location'] ipdict[myip] = location return location else: return "error_ip_not_mapped_to_site"
by using requests.get , json.dumps, aren't sending request while adding json query string? essentially, doing a
get https://10.6.75.98/wapi/v1.0/network?{\"network\": \"10.233.84.0/22\"}
i've been using webapi perl, not python, if way code trying things, not work well. send json server, post , add '_method' argument 'get' value:
post https://10.6.75.98/wapi/v1.0/network content: { "_method": "get", "network": "10.233.84.0/22" } content-type: application/json
or, don't send json server , send
get https://10.6.75.98/wapi/v1.0/network?network=10.233.84.0/22
which guessing achieve dropping json.dumps code , handing search_string requests.get directly.
Comments
Post a Comment