Scripting : InfoBlox DHCP > Windows DHCP

Well another issue to overcome this was to move from InfoBlox DHCP to Windows DHCP and this was a weird one, there are may guides and application about moving from Windows DHCP to InfoBlox but no the other way around.

There are lots of posts on the InfoBlox forums asking for this, but that question is left unanswered and I cannot tell if that is intentional as they want to you move to InfoBlox but not off it, its like a hotel where you can check in but not check out - this seems weird.

Export InfoBlox Configuration

Anyhow, lets export the configuration for the DHCP service which can be done like this:

  1. Login to InfoBlox
  2. Data Management
  3. DHCP
  4. Select the Member
  5. View DHCP Configuration
  6. Choose either IPv4 or IPv6 depending on your configuration
That should look something like this, the green box shows the option you need to click:


That will then start a new tab with all the configuration displayed in a CLI view as below, yes this is blurred but you get the idea:


Select all this data using Ctrl+A and then give the copy command and paste that into a new notepad document with Ctrl+V name the file infoblox.conf then save the document, you have not got all the name, ranges, exclusions and options in that file.

Export Windows DHCP configuration

You now need to export the Windows DHCP configuration to see what format it is expecting and like the InfoBlox configuration you can review you options, which you can do with this command from a command prompt as an administrator, and here you have the import and export commands:

Export Configuration : netsh dhcp server export c:\DHCP\dhcp.txt all
Import Configuration : netsh dhcp server import c:\DHCP\dhcp.txt all

If you do not have a DHCP server then add the role add a sample scope and then export the configuration, this will give you a file called dhcp.txt, now lets take a look at this file:


Well that is not good, it would appear that this export is binary data which means it has probably come from a database and is not really human readable that is a problem for the export/import process as we need the file in binary data.

All hope is lost, well not its not, we need to look at this from a different perspective.....

Script to create Windows configuration data

The ubfoblox.txt file is text-based and contains all the DHCP configuration with all the various parameters and options set for the DHCP server to use, this will include elements like:

  1. Global configuration options (e.g., ddns-update-style, default-lease-time)
  2. Option definitions (e.g., option domain-name, option domain-name-servers)
  3. Failover peer configuration
  4. Specific client class definitions

This means we can write a script to read this configuration file and translates the commands from the InfoBlox configuration to Powershell commands you can run on the Windows DHCP server.

The Script : Infoblox-CommandGenerator.ps1

# Define the path to the Infoblox configuration file
$infobloxConfigPath = "infoblox.conf"

# Read the Infoblox configuration file
$infobloxConfigContent = Get-Content -Path $infobloxConfigPath

# Initialize arrays to hold configuration details
$globalOptions = @{}
$options = @()
$failover = @{}
$classes = @()
$subnets = @()

# Define regular expressions for parsing
$globalPattern = '^(option\s+\S+|\S+)\s+(.+);'
$optionPattern = '^option\s+(\S+)\s+(.+);'
$failoverStartPattern = '^failover peer "([^"]+)"\s*\{'
$failoverEndPattern = '^\}'
$classStartPattern = '^class\s+"([^"]+)"\s*\{'
$classEndPattern = '^\}'
$subnetStartPattern = '^subnet\s+(\d+\.\d+\.\d+\.\d+)\s+netmask\s+(\d+\.\d+\.\d+\.\d+)\s*\{'
$subnetEndPattern = '^\}'
$rangePattern = 'range\s+(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+);'

# Initialize state variables
$inFailover = $false
$inClass = $false
$inSubnet = $false
$currentClass = $null
$currentSubnet = $null

# Define a mapping from Infoblox option names to Windows DHCP option IDs
$optionMapping = @{
    "domain-name-servers" = 6
    "routers" = 3
    "default-lease-time" = 51
    "max-lease-time" = 51
    "min-lease-time" = 51
    "IPT_Avaya_Option_242" = 242
    "IPT_Avaya_Option_176" = 176
    # Add more mappings as needed
}

# Parse each line of the Infoblox configuration
foreach ($line in $infobloxConfigContent) {
    $line = $line.Trim()
    if ($line -match "^#" -or [string]::IsNullOrEmpty($line)) {
        continue
    }

    if ($line -match $subnetStartPattern) {
        $inSubnet = $true
        $subnetAddress = $matches[1]
        $subnetNetmask = $matches[2]
        $currentSubnet = @{
            Address = $subnetAddress
            Netmask = $subnetNetmask
            Options = @{}
            Ranges = @()
        }
        $subnets += $currentSubnet
        continue
    }

    if ($inSubnet -and $line -match $subnetEndPattern) {
        $inSubnet = $false
        $currentSubnet = $null
        continue
    }

    if ($inSubnet -and $line -match $rangePattern) {
        $rangeStart = $matches[1]
        $rangeEnd = $matches[2]
        $currentSubnet.Ranges += @{ Start = $rangeStart; End = $rangeEnd }
        continue
    }

    if ($inSubnet) {
        if ($line -match $optionPattern) {
            $key = $matches[1]
            $value = $matches[2]
            $currentSubnet.Options[$key] = $value
            continue
        }
    }

    if (-not $inFailover -and -not $inClass -and -not $inSubnet) {
        if ($line -match $globalPattern) {
            $key = $matches[1]
            $value = $matches[2]
            $globalOptions[$key] = $value
            continue
        }
    }

    if ($line -match $optionPattern) {
        $key = $matches[1]
        $value = $matches[2]
        $options += @{ Key = $key; Value = $value }
        continue
    }

    if ($line -match $failoverStartPattern) {
        $inFailover = $true
        $failover.Peer = $matches[1]
        continue
    }

    if ($inFailover -and $line -match $failoverEndPattern) {
        $inFailover = $false
        continue
    }

    if ($inFailover) {
        $key, $value = $line -split "\s+", 2
        $failover[$key] = $value.TrimEnd(';')
        continue
    }

    if ($line -match $classStartPattern) {
        $inClass = $true
        $classMatch = $matches[1]
        $currentClass = @{ Name = $classMatch; Options = @{} }
        $classes += $currentClass
        continue
    }

    if ($inClass -and $line -match $classEndPattern) {
        $inClass = $false
        $currentClass = $null
        continue
    }

    if ($inClass) {
        $key, $value = $line -split "\s+", 2
        $currentClass.Options[$key] = $value.TrimEnd(';')
        continue
    }
}

# Generate Windows DHCP PowerShell commands
$commands = @()

# Add global options
foreach ($key in $globalOptions.Keys) {
    if ($optionMapping.ContainsKey($key)) {
        $commands += "Set-DhcpServerv4OptionValue -OptionId $($optionMapping[$key]) -Value '$($globalOptions[$key])'"
    }
}

# Add option definitions
foreach ($option in $options) {
    if ($optionMapping.ContainsKey($option.Key)) {
        $commands += "Set-DhcpServerv4OptionValue -OptionId $($optionMapping[$option.Key]) -Value '$($option.Value)'"
    }
}

# Add failover configuration 
#if ($failover) {
#   $commands += "Add-DhcpServerv4Failover -Name '$($failover.Peer)' -PartnerServer #$($failover.address) -PartnerServerRole Active -LoadBalancePercent $($failover.split)"
#}

# Add subnet and range configurations
foreach ($subnet in $subnets) {
    $commands += "Add-DhcpServerv4Scope -Name 'Scope for $($subnet.Address)' -StartRange $($subnet.Ranges[0].Start) -EndRange $($subnet.Ranges[0].End) -SubnetMask $($subnet.Netmask) -State Active"
    foreach ($optionKey in $subnet.Options.Keys) {
        if ($optionMapping.ContainsKey($optionKey)) {
            $commands += "Set-DhcpServerv4OptionValue -ScopeId $($subnet.Address) -OptionId $($optionMapping[$optionKey]) -Value '$($subnet.Options[$optionKey])'"
        }
    }
}

# Add class definitions
foreach ($dhcpClass in $classes) {
    $commands += "Add-DhcpServerv4Class -Name '$($dhcpClass.Name)' -Description '$($dhcpClass.Description)'"
    foreach ($key in $dhcpClass.Options.Keys) {
        if ($optionMapping.ContainsKey($key)) {
            $commands += "Set-DhcpServerv4Class -Name '$($dhcpClass.Name)' -OptionId $($optionMapping[$key]) -Value '$($dhcpClass.Options[$key])'"
        }
    }
}

# Output the commands
$commands | Out-File -FilePath "Windows_DHCP_BuildCommands.ps1"

# Print the commands to the console for verification
$commands | ForEach-Object { Write-Output $_ }

Generated Failover Caution

If you are using InfoBlox that is part of a resilient pair (I think this called a super grid) then the script will accommodate that, however as Windows paring works differently it good for thr script to capture the configuration but Windows DHCP will not be able to use the commands from InfoBlox

If you would like these commands included then uncomment out the lines to include those commands the the failover commands will be included:

# Add failover configuration 
if ($failover) {
   $commands += "Add-DhcpServerv4Failover -Name '$($failover.Peer)' -PartnerServer $($failover.address) -PartnerServerRole Active -LoadBalancePercent $($failover.split)"
}

Script Execution : Seconds later you you have your commands

The script will then produce as an export a file called : Windows_DHCP_BuildCommands.ps1

When executed the contents of this file will then build the DHCP server configuration with all the Powershell commands you need, once the command is run that will look something like this:


This includes commands like this, which include creating the scope and then settings the scope options:

Add-DhcpServerv4Scope -Name 'Bear.local' -StartRange 10.55.1.10 -EndRange 10.55.1.240 -SubnetMask 255.255.255.0 -State Active
Set-DhcpServerv4OptionValue -ScopeId Bear.local -OptionId 3 -Value '10.55.1.249'
Set-DhcpServerv4OptionValue -ScopeId Bear.local -OptionId 6 -Value '10.55.1.1, 10.55.1.2'
Set-DhcpServerv4OptionValue -ScopeId Bear.local -OptionId 12 -Value 'bear.local'

Configure the DHCP Server

Now you have got here this last step is simple, from the DHCP navigate to the folder and run the script which will be the command:

./Infoblox-CommandGenerator.ps1

That will output the list of commands you require for configuration then you can run this to configure you DHCP server with all the options:

./Windows_DHCP_BuildCommands.ps1

Then when you check your DHCP server you will notice that all the scopes are now there and they should be active with the options configured.


Excellent, not you have moved the scope and the options from InfoBlox to Windows DHCP, obviously this will not include all the current leases and that is specific to Infoblox.
Previous Post Next Post

نموذج الاتصال