The CreatorCon Call for Content is officially open! Get started here.

Infoblox get next IP address via REST

poyntzj
Kilo Sage

I was looking as part of an orchestration process to get the next available IP address from our Infoblox server.

I have used John-James Andersons Scriptable RESTMessage Library

Scriptable RESTMessage Library for ServiceNow-John James Andersen

 

The rest of this message are the small Background scripts that I have used that will get me the next IP and reserve it for my server.

Before impementation it will need some work, but until I know our data in our Infoblox is 100% we are going to allocate the IP address manually.

If we do decide to automate, I will add the updated code.

 

You use this REST message to get the _ref for the network you are searching for

 

var r = new RESTMessageScripted("get","https://infobloxserver/wapi/v1.4.1/network?network=10.0.0.0/24");
r.setBasicAuth("username","password");
r.setMIDServer("MIDServer");
r.execute();
var k = 1;
var response = r.getResponse();
this.debug("Initial response: " + response);
this.debug("Going to loop for a response from MID Server");
while (response == null) {
       this.debug("waiting … " + k + " seconds");
       response = r.getResponse(1000); //wait 1 second before looking for the response
       k++;

       if (k > 30) {
               gs.log("ERROR: Web Service did not respond after 30 seconds", this.LOGGER_SOURCE);
               break; // service did not respond after 30 tries
       }
}
gs.print(response.getBody())

 

and you will get the following reply

 

*** Script: [
       {
               "_ref": "network/ZG5zLm5ldHdvcmskMTAuMTkzLjE1Mi4wLzI0LzA:10.0.0.0/24/default", 
               "comment": "Comment about network", 
               "network": "10.0.0.0/24", 
               "network_view": "default"
       }
]

you will need to get the _ref from the response and then encode that into the following REST message so you can get the free IP address

(replace the network/ZG5zLm5ldHdvcmskMTAuMTkzLjE1Mi4wLzI0LzA with whatever is returned above)

 

 

var r = new RESTMessageScripted("post","https://infobloxserver/wapi/v1.4.1/network/ZG5zLm5ldHdvcmskMTAuMTkzLjE1Mi4wLzI0LzA?_function=next_available_ip");
r.addHeader("content-type", "application/json");
r.setContent('{"num":1}');
r.setBasicAuth("username","password");
r.setMIDServer("MIDServer");
r.execute();
var k = 1;
var response = r.getResponse();
this.debug("Initial response: " + response);
this.debug("Going to loop for a response from MID Server");
while (response == null) {
       this.debug("waiting … " + k + " seconds");
       response = r.getResponse(1000); //wait 1 second before looking for the response
       k++;

       if (k > 30) {
               gs.log("ERROR: Web Service did not respond after 30 seconds", this.LOGGER_SOURCE);
               break; // service did not respond after 30 tries
       }
}
gs.print(response.getBody())

 

And you will get the response

 

*** Script: {
       "ips": [
               "10.0.0.67"
       ]
}

Now you have the next IP address, use the next REST message using the IP address from the above and the name of your server

 

 

var r = new RESTMessageScripted("post","https://infobloxserver/wapi/v1.4.1/fixedaddress");
r.setContent('{"ipv4addr":"10.0.0.67","name":"lonipmp01101b","match_client":"RESERVED"}');
r.setBasicAuth("username","password");
r.setMIDServer("MIDServer");
r.execute();
var k = 1;
var response = r.getResponse();
this.debug("Initial response: " + response);
this.debug("Going to loop for a response from MID Server");
while (response == null) {
       this.debug("waiting … " + k + " seconds");
       response = r.getResponse(1000); //wait 1 second before looking for the response
       k++;

       if (k > 30) {
               gs.log("ERROR: Web Service did not respond after 30 seconds", this.LOGGER_SOURCE);
               break; // service did not respond after 30 tries
       }
}
gs.print(response.getBody())

 

and if successful, you will see the following

 

*** Script: "fixedaddress/ZG5zLmZpeGVkX2FkZHJlc3MkMTAuMTkzLjE1Mi42Ny4wLi4:10.0.0.67/default"

 

Hope it helps somebody

9 REPLIES 9

bianca_vaccarin
ServiceNow Employee
ServiceNow Employee

Thanks for sharing!


The infoblox documentation is a little interesting and took me a little while to fully get there.


nivdolgin
Kilo Explorer

Julian, I'm curious if you integrated the Infoblox inventory as CIs in SNOW's CMBD too?



Niv


Hi Niv, never did that.


We use discovery in the main DC's to get their equipment and that was all


We only used Infoblox to retrieve and then assign the next available IP address.



Cheers