powershell - 如何使用 Powershell 将 IP 范围转换为单个 CIDR 表示法?

标签 powershell ip ip-address cidr

我正在尝试将 IP 地址范围转换为单个 CIDR 表示法,但无法找到任何使用 Powershell 将其转换为 CIDR 表示法的方法。

startIP = '4.249.240.6'
endIP = '4.249.255.255'

Output: '4.249.241.0/24'

最佳答案

您可以使用此功能。不完全是您所期望的,因为它几乎没有其他子网信息。

#------------------------------------------------------------------------------
# Subroutine ip_range_to_prefix
# Purpose           : Return all prefixes between two IPs
function Get-IpSubnetsBetween {

    param(
        [ipaddress]$StartIp,

        [ipaddress]$EndIp
    )


    if ($StartIp.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork -or
          $EndIp.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork) {

        Write-Error -Message 'Function only works for IPv4 addresses'
    }


    # Get the IPs in 32-bit unsigned integers (big-endian)
    # The .Address property is little-endian, or host-endian, so avoid that.

    [uint32[]]$octets = $StartIp.GetAddressBytes()
    [uint32]$startIpAddress = ($octets[0] -shl 24) + ($octets[1] -shl 16) + ($octets[2] -shl 8) + $octets[3]

    [uint32[]]$octets = $EndIp.GetAddressBytes()
    [uint32]$EndIpAddress   = ($octets[0] -shl 24) + ($octets[1] -shl 16) + ($octets[2] -shl 8) + $octets[3]
    Remove-Variable -Name octets -ErrorAction SilentlyContinue


    while ($startIpAddress -le $endIPAddress -and $startIpAddress -ne [uint32]::MaxValue) {


        # Bitwise shift-right in a loop,
        # to find how many trailing 0 bits there are
        $numTrailingZeros = 0
        while ([uint32]($startIpAddress -shr $numTrailingZeros) % 2 -eq 0) {
            $numTrailingZeros++
        }
    
        # switch all those bits to 1, 
        # see if that takes us past the end IP address. 
        # Try one fewer in a loop until it doesn't pass the end.
        do {
            [uint32]$current = $startIpAddress -bor ([math]::Pow(2, $numTrailingZeros)-1)
            $numTrailingZeros--
        } while ($current -gt $endIpAddress)
    

        # Now compare this new address with the original,
        # and handwave idk what this is for
        $prefixLen = 0
        while (([uint32]($current -band [math]::Pow(2, $prefixLen))) -ne ([uint32]($startIpAddress -band [math]::Pow(2, $prefixLen)))) {
            $prefixLen++
        }
        $prefixLen = 32 - $prefixLen

    
        # add this subnet to the output
        [byte[]]$bytes = @(
            (($startIpAddress -band [uint32]4278190080) -shr 24),
            (($startIpAddress -band [uint32]16711680) -shr 16),
            (($startIpAddress -band [uint32]65280) -shr 8),
             ($startIpAddress -band [uint32]255)
        )

        [ipaddress]::new($bytes).IpAddressToString + "/$prefixLen"
    
        # Add 1 to current IP
        [uint32]$startIpAddress = [uint32]($current + 1)

    }
}

用法:

Get-IpSubnetsBetween -StartIp '4.249.240.6' -EndIp '4.249.255.255'

输出

Output

引用链接:r/Powershell - Reddit

关于powershell - 如何使用 Powershell 将 IP 范围转换为单个 CIDR 表示法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75281955/

相关文章:

powershell - 使用 powershell 将环境类型设置为 reg_expand_sz

html - Powershell:如何引用第n个元素

dns - DNS服务器可以有源IP吗?

ip - IPv6 地址的文本表示的最大长度?

c# - 通过 MSBuild 执行 Powershell 脚本时找不到 "Microsoft.SqlServer.BatchParser.dll"

powershell - Get-Process 到远程计算机不起作用,但 Invoke-Command 可以

docker - 在 DigitalOcean 上使用 Docker 运行 Shiny 应用程序

networking - ARP REQUEST 数据包中使用的发送方 IP 地址是什么?

Java:在不使用 Web 服务的情况下以编程方式获取一个人的外部 IP 地址

c++ - 为什么在删除适配器的默认 IP 时 DeleteIPAddress 会失败?