PowerShell Toolkit for Regex IP Networking

# PowerShell Toolkit for Regex IP Networking

# Function to validate an IPv4 address
function Test-IPv4Address {
    param (
        [Parameter(Mandatory=$true)]
        [string]$IPAddress
    )
    
    $regex = '^\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$'
    return $IPAddress -match $regex
}

# Function to validate an IPv6 address
function Test-IPv6Address {
    param (
        [Parameter(Mandatory=$true)]
        [string]$IPAddress
    )
    
    $regex = '^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$'
    return $IPAddress -match $regex
}

# Function to extract all IPv4 addresses from a string
function Get-IPv4Addresses {
    param (
        [Parameter(Mandatory=$true)]
        [string]$InputString
    )
    
    $regex = '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
    return $InputString | Select-String -Pattern $regex -AllMatches | ForEach-Object { $_.Matches.Value }
}

# Function to extract all IPv6 addresses from a string
function Get-IPv6Addresses {
    param (
        [Parameter(Mandatory=$true)]
        [string]$InputString
    )
    
    $regex = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'
    return $InputString | Select-String -Pattern $regex -AllMatches | ForEach-Object { $_.Matches.Value }
}

# Function to validate a subnet mask
function Test-SubnetMask {
    param (
        [Parameter(Mandatory=$true)]
        [string]$SubnetMask
    )
    
    $regex = '^(((255\.){3}(255|254|252|248|240|224|192|128|0+))|((255\.){2}(255|254|252|248|240|224|192|128|0+)\.0)|((255\.)(255|254|252|248|240|224|192|128|0+)(\.0+){2})|((255|254|252|248|240|224|192|128|0+)(\.0+){3}))$'
    return $SubnetMask -match $regex
}

# Function to validate CIDR notation
function Test-CIDRNotation {
    param (
        [Parameter(Mandatory=$true)]
        [string]$CIDR
    )
    
    $regex = '^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))$'
    return $CIDR -match $regex
}

# Function to extract network address from CIDR notation
function Get-NetworkAddress {
    param (
        [Parameter(Mandatory=$true)]
        [string]$CIDR
    )
    
    if (Test-CIDRNotation $CIDR) {
        $parts = $CIDR -split '/'
        $ip = $parts[0]
        $mask = [int]$parts[1]
        
        $ipBytes = [System.Net.IPAddress]::Parse($ip).GetAddressBytes()
        $maskBytes = [byte[]](,0xFF * 4)
        for ($i = 0; $i -lt 4; $i++) {
            if ($mask -lt 8) {
                $maskBytes[$i] = [byte]((0xFF -shl (8 - $mask)) -band 0xFF)
                $mask = 0
            } else {
                $mask -= 8
            }
        }
        
        $networkBytes = for ($i = 0; $i -lt 4; $i++) {
            $ipBytes[$i] -band $maskBytes[$i]
        }
        
        return [System.Net.IPAddress]($networkBytes)
    } else {
        Write-Error "Invalid CIDR notation"
        return $null
    }
}

# Example usage:
# Test-IPv4Address "192.168.1.1"
# Test-IPv6Address "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
# Get-IPv4Addresses "The IP addresses are 192.168.1.1 and 10.0.0.1"
# Get-IPv6Addresses "IPv6 addresses: 2001:0db8:85a3::8a2e:0370:7334 and fe80::1"
# Test-SubnetMask "255.255.255.0"
# Test-CIDRNotation "192.168.1.0/24"
# Get-NetworkAddress "192.168.1.100/24"

This toolkit provides the following functions:

  1. Test-IPv4Address: Validates an IPv4 address.
  2. Test-IPv6Address: Validates an IPv6 address.
  3. Get-IPv4Addresses: Extracts all IPv4 addresses from a given string.
  4. Get-IPv6Addresses: Extracts all IPv6 addresses from a given string.
  5. Test-SubnetMask: Validates a subnet mask.
  6. Test-CIDRNotation: Validates CIDR notation.
  7. Get-NetworkAddress: Extracts the network address from CIDR notation.

You can use these functions in your PowerShell scripts or directly in the PowerShell console. The example usage at the end of the script shows how to call each function.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *