DHCP Server Dump (Export) - Sorting IP using RegEx

  • Re: Can I sort IP addresses in numeric value

    When creating DHCP reservations, Microsoft stores them as they are created. The result leads to exports being out of IP sequential order. Here is an alternative solution to my previous (https://community.notepad-plus-plus.org/post/26434).

    The alternative method pads the IP with leading Zeros with “undo” option to target just those changes. The prior solution used an \x23 (ASCII 23 hash ‘#’) as a RegEx group substitution. However, the “unpad” (undo) I referenced would also remove comments in DHCP dump file, breaking it for importing later.

    Re: (https://community.notepad-plus-plus.org/post/25048)
    Kudos for the extensibility : @guy038 and @Scott-Sumner

    Here’s my new workflow to DHCP Server: export, pad, sort, unpad, import

    1. Export
    netsh dhcp server dump > "DHCP_Server_Export_%date:~10,4%.%date:~4,2%.%date:~7,2%_%time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2%.txt
    1. Pad (Notepad++ RegEx)
      Select Text between “ReservedIp to the Scope”
      RegEx Replace All In Selection
      SEARCH reservedip ((\d)?(\d)?\d)\.((\d)?(\d)?\d)\.((\d)?(\d)?\d)\.((\d)?(\d)?\d)
      REPLACE reservedip (?2:0)(?3:0)$1.(?5:0)(?6:0)$4.(?8:0)(?9:0)$7.(?11:0)(?12:0)$10
      The above effectively changes IP such as -->

    2. Sort (Notepad++ TextFX Tools)
      While selection is highlighted, “Sort lines case sensitive (at column)”

    3. UnPad (Notepad++ RegEx)
      While selection is highlighted,
      RegEx Replace All In Selection
      SEARCH reservedip 0*(\d{1,3})\.0*(\d{1,3})\.0*(\d{1,3})\.0*(\d{1,3})
      REPLACE reservedip $1.$2.$3.$4
      The above is needed since netsh exec on script import script file (step 6) fails with IPs having leading zeros. Unfortunate.

    4. Remove/Purge (PowerShell)
      Save/SaveAs Exported Text file as “DHCP_Server_Import…”
      Open PowerShell ISE as Administrator (replace ‘x’ as applicable)

    Remove-DhcpServerv4Reservation -ComputerName "xxxx.xxx.local" -ScopeId 192.168.xx.0
    1. Import (CMD)
      Open elevated Command Prompt
    netsh exec "DHCP_Server_Import..."

    Refresh DHCP tree and reservations are now in order (matching Import file).

    Hope this helps someone.

Log in to reply