Infoblox Community
Reply

Need help with get_host_by_ip

Techie
Posts: 5
Registered: ‎01-24-2017
rabakukk
Techie
Posts: 5

I have been successfully using the API to add and remove hosts but now I want to do a check before adding to make sure there isn't a host associated with a specific IP and if there is to delete it, then add the new host. I am doing this in a python webhooks app that is used by Scalr with Openstack. Given it is python I have looked to this page(https://github.com/Infoblox-Development/Infoblox-API-Python/blob/master/infoblox.py) for examples. I found the method get_host_by_ip in this document but have not been able to make it work. In this example, the url has "network_view" but I have no idea how to get this to include in the call. Any suggestions? Thanks.

Re: Need help with get_host_by_ip

Adviser
Posts: 310
Registered: ‎10-26-2007
Adviser
Posts: 310
The network view is probably just ”default” - if not it would be the name you see in a drop down on the UI (if there is only one the drop down doesn’t show).

You may want to look at https://github.com/infobloxopen/infoblox-client which is more actively maintained.

John

Re: Need help with get_host_by_ip

Techie
Posts: 5
Registered: ‎01-24-2017
rabakukk
Techie
Posts: 5

Hi John,

 

Thanks for the response. I have tried "default" previously but unfortunately it didn't work and I don't see a drop down in the UI. Thanks for the new code reference. I'll see what I can find there.

 

Thanks,

Ron

Re: Need help with get_host_by_ip

[ Edited ]
Techie
Posts: 5
Registered: ‎01-24-2017
rabakukk
Techie
Posts: 5

Hi John,

 

I tried network_view as default again and got this error: "Network View 'default' not found". I found this in the IPAM tab (image below) and was able to drill down from the network listed to the IP address I was trying to get. It seems like the Network View is "default" so I don't know why that error is being thrown. Any suggestions? Thanks.

 

Untitled.png

 

Thanks,

Ron

Re: Need help with get_host_by_ip

Adviser
Posts: 310
Registered: ‎10-26-2007
Adviser
Posts: 310
Not sure why that would be. It does look like it is ’default’. I am not familiar with that particular client library, perhaps it is sending the data in some unexpected manner. If there is a way to log the actual request and response that could help debug it. You could also try the newer client module.

Re: Need help with get_host_by_ip

Community Manager
Posts: 240
Registered: ‎09-15-2008
GHorne Community Manager
Community Manager
Posts: 240

network_view is an option field. You can usually leave it out unless you have more than one net view.

 

if it is missing, the API will just use the 'default' view and modify the records in there.

 

a code snippet would help to diagnose any further issues.

Re: Need help with get_host_by_ip

Techie
Posts: 5
Registered: ‎01-24-2017
rabakukk
Techie
Posts: 5

Sorry for the long delay in reply. I had other tasks take priority.

 

This is part of a webhooks app used with Scalr, so the code is getting the IP from an input payload provided by Scalr in the first line.

 

 

external_ip = self.payload["data"]["SCALR_EXTERNAL_IP"]
response = self.__get_host_for_ip(self.url + "ipv4address?ip_address='" + external_ip + "'")

if (response is None):
    log_message("Nothing was returned.")
else:
    log_message("Response: " + response)



def __get_host_for_ip(self, get_url):
    r = requests.get(url=get_url, auth=(self.username, self.password), verify=False)
    r_json = r.json()
    if r.status_code == 200 or r.status_code == 201:
        #Verify that there is an r_json object
        if r_json:
            log_message("Got a response")
            host_ref = r_json[0]['_ref']
            log_message("host_ref: " + host_ref)
            return host_ref
        else:
            return None
    else:
        if 'text' in r_json:
            log_message("!!!!! EXCEPTION in Infoblox Get Host for IP (1): " + r_json['text'])
        else:
            log_message("!!!!! EXCEPTION in Infoblox Get Host for IP (2): %s" % e)

 

I get the following output from this code:

 

URL parameter: https://10.9.0.224/wapi/v1.2.1/ipv4address?ip_address='10.48.87.95'
!!!!! EXCEPTION in Infoblox Get Host for IP (1): A network was not found for this address.

 

Is there anything else I need to add to the url sent via requests? Thanks.

 

Highlighted

Re: Need help with get_host_by_ip

Techie
Posts: 5
Registered: ‎01-24-2017
rabakukk
Techie
Posts: 5

Thanks for the previous help but I guess no one has any idea about my last post 2 weeks ago?

Re: Need help with get_host_by_ip

Community Manager
Posts: 240
Registered: ‎09-15-2008
GHorne Community Manager
Community Manager
Posts: 240

It just took a while to unravel the problem.

 

You're getting this error because of the way your Grid data is set up.

The API code looks to be correct, there is just no good data on the system.

 

To search for hosts by addresses you hve to have a network defined in IPAM for that address space.

You can't just search for a host by ip address, well, not by using /ipv4address.

 

if you create a network 10.48.0.0/16 it will probably work.

 

But, you could also search hosts by the ipaddress, with something like this:

 

/wapi/v2.3/record:host?_return_type=json-pretty&_return_as_object=1&ipv4addr=1.1.1.1

However that will only return hosts, not any other DNS records that use that address. searching /ipv4address is the way to get all matching object types.

Re: Need help with get_host_by_ip

Authority
Posts: 30
Registered: ‎05-19-2012
Authority
Posts: 30

Hello rabakukk,

 

Does the user you uses have the read permission on the parent network?

 

Regards

 

Nicolas