Infoblox Exchange Cybersecurity Roadshow 2020 – Join us!
North America | Europe | Middle East/Africa | Asia-Pacific

API & Integration

Reply
Highlighted

Powershell - WAPI - Create the leaf network structure under a networks container

Adviser
Posts: 81
2900     0

It is sometimes usefull to automatically create leaf networks in a container.

 

In the example below I have created a /22 container in the IPAM GUI and I want it to be populated as divided as :
2 x /25
then 16 x /28
and finally 2 x /24

screenshot.png
 
 
screenshot_1.png
 
 
screenshot_2.png
 
 
screenshot_3.png
 
 

The code used is available on https://github.com/Infoblox-API/REST-PowerShell and below:

 

#requires -version 3

##########
#
# Author:  Nicolas Jeanselme & Don Smith
# Email :  njeanselme@infoblox.com & dsmith@infoblox.com
# This script is provided as is.  Use at your own risk.

##########
#
# Remember to enable powershell scripts to run on your system
#    example >> Set-ExecutionPolicy -ExecutionPolicy Unrestricted
#

##########
# Default values and command line parameters
#    switch ask_pw will securely prompt for the password
#    Standardize the following options:
#       g = grid_master, u = username, p = password, a = ask_pw, d = debug, c = container, v = vlan
#    Assume all other options would need fully qualification
Param (
    [string]$grid_master = "demo.se.infr.infoblox.com",
    [string]$username    = "admin",
    [string]$password    = "infoblox",
    [string]$wapi_ver    = "v1.6",
    [string]$max_results = "_max_results=10000",
    [string]$network     = "3.0.0.0/22",
    [int]$vlan           = 1500,
    [array]$site_structure,
    [switch]$ask_pw,
    [switch]$json,
    [switch]$debug
)

$site_structure=@{"cidr"=25;"num"=2},@{"cidr"=28;"num"=16},@{"cidr"=24;"num"=2}

write-host "Parameters set"

$dbg_time_elapsed = [System.Diagnostics.Stopwatch]:Smiley FrustratedtartNew()

##########
# Set defaults

##########
# Build the values that need built
if ($ask_pw) {
    $secure_pw = $( Read-Host -Prompt "Enter password" -AsSecureString )
    Write-Output ""
} else {
    $secure_pw = ConvertTo-SecureString $password -AsPlainText -Force
}
$credential = New-Object System.Management.Automation.PSCredential ($username, $secure_pw)
$uri_base = "https://$grid_master/wapi/$wapi_ver"

##########
# Do the following to ignore self-signed certificates
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#[System.Net.ServicePointManager]:Smiley FrustratederverCertificateValidationCallback = { $true }

##########################
########## MAIN ##########
##########################

$currentScriptName = $MyInvocation.MyCommand.Name
write-host "script: $currentScriptName"


########## Get network container ref ##########


# Request the transaction (GET request for data)
$uri = "$uri_base/networkcontainer?network=$network"

try {
    $results = Invoke-RestMethod -Uri $uri -Credential $credential -Method Get -SessionVariable ib_session
} catch {
    write-host "Unable to get the network container"
    write-host $_.ErrorDetails
    write-host $_.Exception 
    exit 1
}  

$ref = $results._ref
write-host "Authenticating as          : $username"

########## Get next available networks & create it ##########

foreach ($block in $site_structure) {

        $uri = $uri_base+"/"+$ref+"?_function=next_available_network&cidr="+$block.cidr+"&num="+$block.num

        try {
                $results = Invoke-RestMethod -Uri $uri -Method Post -WebSession $ib_session
        } catch {
            write-host "Unable to get the next available networks"
            write-host $results
               write-host $_.ErrorDetails
            write-host $_.Exception 
             exit 1
        }
        Write-Output "Availables: $($results.networks)"
        
        foreach ($network in $results.networks) {
            $uri = $uri_base+"/network?network="+$network

            try {
                $results = Invoke-RestMethod -Uri $uri -Method Post -WebSession $ib_session
            } catch {
                write-host "Unable to create the next available network"
                write-host $results
                   write-host $_.ErrorDetails
                write-host $_.Exception
                 exit 1
            }
            Write-Output "Created successfully $network"


            ##### Handle Extensible Attributes #####
            # Now update the object with the extensible attribute data
            # This data cannot be submitted when creating the object
            $extattrs = @{
                extattrs = @{
                    Site = @{ value = "test" };
                    VLAN = @{ value = "$vlan" };
                }
            }
            $vlan++
            $json_text = $extattrs | ConvertTo-Json
            $uri = $uri_base+"/$results"

               try {
                $results = Invoke-RestMethod -Uri $uri -ContentType "application/json" -Body $json_text -Method PUT -WebSession $ib_session
            } catch {
                write-host "Unable to set the VLAN EA to the next available network"
                write-host $results
                Write-Host $_.ErrorDetails
                Write-Host $_.Exception
                exit 1
            }

        }
}

if ($debug) {
    Write-Host "Total time elapsed: $($dbg_time_elapsed.Elapsed.ToString())"
}

Check out our new Tech docs website at http://docs.infobox.com for latest documentation on Infoblox products.

(No subject)

Adviser
Posts: 81
2901     0

Check out our new Tech docs website at http://docs.infobox.com for latest documentation on Infoblox products.
Showing results for 
Search instead for 
Do you mean 

Recommended for You