Infoblox and Powershell

Spread the love

I haven’t written anything for a while so I decided to look through some of my old gists on github. This was prompted by a cooking stream that I subscribe to on Twitch, and the host had put her recipe up on

So looking through my own gists, I found one that I created last year around Infoblox and comparing to Active Directory hostnames. The full API can be found on Infoblox’s support website around their NIOS.

# Author: G.S
# Date: 01-26-2017
# Script to get infoblox names
# Import subprocess module so we can call the curl command from the operating system

import subprocess
import sys, getopt, os
import pycurl
import json
from pprint import pprint
from StringIO import StringIO
from io import BytesIO
import getpass
import re


# Create a NULL for the stdout from the Powershell command later
FNULL = open(os.devnull, 'w')
# Main Loop

def main():
    # Initial Setup
    # Local variables to Main Function
    maxrecords = 0
    somelist = []
    somelist2 = []
    # Check the arguments passed
         opts, args = getopt.getopt(sys.argv[1:],"u:s:m:", ['user=', 'search=', 'maxrecords='])
    except getopt.GetoptError:
         print 'Wrong Arguments'

    if len(sys.argv) == 1:
           print 'No Args'
    for o1, a1 in opts:
           if o1 == "-u":
               username = a1
           if o1 == "-s":
               searchterm = a1
               URL2 = URL + "&name~=" + searchterm + "&_max_results=-500000"
           if o1 == "-m":
               maxrecords = a1
    # Obtain password

    password = getpass.getpass()
    # Curl options: curl.HTTPGET, curl.PUT, curl.POST. SSL_VERIFYPEER and HOST to ignore bogus SSL cert
    # Don't know why its bogus. Either Infoblox is a sham or SSL library for pycurl is... who cares.
    # as it works for now.
    #data = StringIO()
    #data = BytesIO()

    # Call to infoblox and receive a text JSON response
    # Dump the response to a local file just in case we need it.
    PyCurlGet(URL,URL2, username, password)

    # Run it through a real JSON function to produce key value pairs
    with open("testout.json") as json_data:
        d = json.load(json_data)

    if maxrecords == 0:
        maxrecords = len(d)
    # JSON output only name and ipv4addrs
    #for indexes in range(len(d)):
    #    for key, value in d[indexes].items():
    #        print value

    if int(maxrecords) > len(d):
        maxrecords = len(d)

    # Grab the first few characters until the period
    #for indexes in range(int(maxrecords)):
    for i in range(int(maxrecords)):
        result ='^[^.]*', str(d[i]['name']))

    #### Powershell call for Get-ADComputer ####
    for num in range(int(len(somelist))):
        #print somelist[num]
        rs =["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "Get-ADComputer", somelist[num], "| Format-List DNSHostName"], stdout=FNULL)

    outfile = open("testout.csv", "w")
    #for indexes in range(len(d)):
    for indexes in range(int(maxrecords)):
        print str(d[indexes]['name']) + "|" + str(d[indexes]['ipv4addrs'][0]['ipv4addr'] + "|" + somelist2[indexes])
        outfile.write(str(d[indexes]['name']) + "|" + str(d[indexes]['ipv4addrs'][0]['ipv4addr'] + "|" + somelist2[indexes])+"\n")

def usage():
    print " -u <username> -p <password> -s <search term> -m <maxrecords>\n"
    print "example:\n"
    print " -u Foo -p Bar -s UHSPC01 -m 100"

def PyCurlGet(URL,URL2,username, password):
    data = open("testout.json", "wb")
    curl = pycurl.Curl()
    curl.setopt(curl.URL, URL2)
    curl.setopt(curl.USERPWD, '%s:%s' % (username,password))
    curl.setopt(curl.HTTPHEADER, ["Content-Type: application/json"])
    curl.setopt(curl.HTTPGET, True)
    curl.setopt(curl.SSL_VERIFYPEER, 0)
    curl.setopt(curl.SSL_VERIFYHOST, 0)
    #curl.setopt(curl.WRITEFUNCTION, data.write)
    curl.setopt(curl.WRITEDATA, data)

if __name__ == "__main__":


Written by


Technology, Science and Philosophy