Infoblox REST API with Perl

Infoblox

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 for 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 
#
# 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!

{ 0 comments }

DNS Loops – how to not configure DNS forwarding

steelcoaster-1228930-scale

I'm continually amazed by how much I don't know and by all the little issues and problems that I encounter in my day to day tasks.  You never know what's going to pop-up. I recently stumbled over a very poorly configured DNS environment and thought I would share how to not configuring DNS forwarding. There was a standalone Microsoft domain which had four domain controllers which were set to forward their requests to another pair of Microsoft DNS servers which eventually forwarded those requests to a fairly new Infoblox DNS environment. Upon looking at the Infoblox reports I noticed a number […] Read More

{ 2 comments }

Launching the Network Broadcast Storm Podcast

nbsp-thumbnail

Over four years ago I had the idea of trying my hand at a podcast. I enlisted a number of IT professionals from the Network Infrastructure Forums to join me including Dominik Pickhardt, then real life intruded and the project fell idle just before we were going to launch. Since that time Dominik has launched Network Autobahn, which has been a large success due in part to his hard work and efforts. At the recent Networking Field Day 11 I had a frank discussion with Dominik about the possibility of trying to jump start the project and we collectively decided to give it a […] Read More

{ 0 comments }
lock-1463086-scale

The past two weeks have been insane thanks to CryptoWall and the many variants that are out in the wild. It started early Monday morning with a UK office, hit the US West coast mid-week and then finally came calling to the US East coast by weeks end. All told we had to restore some 250,000+ files on multiple Windows File Shares from either Shadow Copies or from backups. The bigger issue for us are the Windows File Shares from which the infected clients have mapped drives. So I've spent the past 2 days researching CryptoWall and trying to figure […] Read More

{ 3 comments }

Big Switch Networks – Big Tap Monitoring Fabric

bigswitchlogo

While attending Networking Field Day 11 we stopped in to see the folks from Big Switch Networks. Big Switch has applied their SDN fabric solution to provide a very cost effective means of providing a virtual network tap within the Data Center. We heard from Kyle Forester, Big Switch Founder, and Rob Sherwood, CTO, among others. I walked away with the following quote, I apologize that I didn't write down exactly who said it. We're "getting away from being the dumb plumber".  Big Switch is using their fabric to make multiple copies of traffic and pass those copies to a services […] Read More

{ 1 comment }