Infoblox WAPI:如何搜索IP

2024-05-17 01:11:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我们的网络团队使用InfoBlox存储有关IP范围(位置、国家等)的信息 有一个可用的API,但是Infoblox的文档和示例不太实用。

我想通过API搜索有关IP的详细信息。首先-我很乐意从服务器上得到任何东西。我修改了the only example I 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

返回错误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\"}'"
}

如果有人能让这个API与Python一起工作,我将非常感谢。


更新:如果有人有一天需要像我这样做,下面是一段代码(它可以工作,但不好用,流线型,不能完美地检查错误等)。
def ip2site(myip): # argument is an IP we want to know the localization of (in extensible_attributes)
    baseurl = "https://the_infoblox_address/wapi/v1.0/"

    # first we get the network this IP is in
    r = requests.get(baseurl+"ipv4address?ip_address="+myip, auth=('youruser', 'yourpassword'), verify=False)
    j = simplejson.loads(r.content)
    # if the IP is not in any network an error message is dumped, including among others a key 'code'
    if 'code' not in j: 
        mynetwork = j[0]['network']
        # now we get the extended atributes for that 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"

Tags: theinipauthapifalsegetis