I recently had the need to add a couple of thousand host objects to our Infoblox IPAM solution since we were missing almost all our store routers, switches, wireless switches, APC UPS, printers, etc. With over 500+ stores and not having any interns available I wasn’t looking forward to the challenge of navigating the WEB UI within Infoblox to create over 2,500+ objects. Instead I decided to let the computer do the work and I wrote a quick and dirty little Perl script that could essentially loop over the subnets in a /16 network and create the host objects for each device one at a time. This isn’t particularly pretty but it got the job done for me and I’m posting it here in hopes that it will help someone else traveling the same path.
I started with the following cURL request since I needed to know what format the data had to be in;
curl -k1 -u username:password -H "Content-Type: application/json" -X POST https://infoblox.acme.com/wapi/v2.1/record:host -d '{"ipv4addrs":[{"ipv4addr":"10.1.1.250"}],"name":"store1-printer.acme.com","comment":"HP LaserJet Printer"}'
That cURL request would create a host object at 10.1.1.250 with the name of store1-printer.acme.com. I took that structure, and built some Perl code around it and got the following script;
#!/usr/bin/perl # # Filename: /usr/local/etc/infoblox-rest.pl # # Purpose: Perl code to create Infoblox host objects utilizing REST API # interface. Used to create DNS objects across multiple subnets. # This code will create host objects for all the routers, switches, # APC UPS, servers, PC, etc. Since the IP address of those devices # utilize the same last octect in each IP subnet and there is a # standard naming convention to easily follow programatically. # # Notes: This code is really just an example of what's possible using # the Infoblox REST API interface. I don't expect anyone to actually # use this code directly, but hopefully they will learn how to # use the REST API interface in Perl through this example. # # Author: Michael McNamara (mfm@michaelfmcnamara.com) # # Date: April 15, 2016 # # Version: 1.0 # # License: # Copyright (C) 2016 Michael McNamara (mfm@michaelfmcnamara.com) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/> # # Changes: # # April 30, 2016 (M.McNamara) # clean up the code, add license # # Load Modules use strict; use warnings; use utf8; use REST::Client; use JSON; use MIME::Base64; use Data::Dumper; # Infoblox username and password my $username = 'InfobloxUsername'; my $password = 'InfobloxPassword'; # Declare Variables my $IP; my $HOST; my $NUM; # Declare Infoblox URL # replace infoblox.acme.com with your Infoblox URL or IP address my $host = 'https://infoblox.acme.com/wapi/v2.1'; # Declare the HTML headers needed my $headers = {Content_Type => 'application/json', Accept => 'application/json', Authorization => 'Basic ' . encode_base64($username . ':' . $password)}; # Declare the REST object my $client = REST::Client->new(); # Link the REST object to the Infoblox URL $client->setHost($host); # Loop across all the subnets in the /16 network for (my $i=1; $i<=100; $i++) { # The IP is based on which subnet we're loop through $IP = "10.246.$i.250"; # In some cases it's necessary to perform some arithmetic on the IP $NUM = $i - 0; # Let's determine what the DNS name should be for this object $HOST = sprintf("store46%02d-printer.acme.com",$NUM); # Let's echo to the screen the IP address and DNS name print "IP = $IP \t HOST = $HOST\n"; # Let's set the JSON data set we're going to send to the REST API my %json_data = ( ipv4addrs => [{ ipv4addr => $IP }], name => $HOST, comment => 'HP LasterJet Printer' ); # Let's encode the JSON data before we send it my $data = encode_json(\%json_data); # Let's call the REST API interface and create the host object $client->POST('/record:host',($data,$headers)); # Let's decode the result so we can see what happened (if debugging) #my $response = decode_json ($client->responseContent()); # Let's print the respone print Dumper( $client->responseContent() ); } #end for exit; ###################################################
Cheers!