简体   繁体   English

构建 IP 地址列表

[英]Building a list of IP addresses

I am trying to build an array of possible IP addresses based on a user's input.我正在尝试根据用户的输入构建一组可能的 IP 地址。 ie IP address along with a CIDR number.即 IP 地址以及 CIDR 编号。 My end goal is to compare this list with a separate list of addresses and find which are is missing.我的最终目标是将此列表与单独的地址列表进行比较,并找出缺少的地址。

Example user input: 192.168.1.0 /24 I want to build an array for all possible values for the /24 network (ie the IP address can be anywhere from 192.168.1.0 - 192.168.1.255)用户输入示例:192.168.1.0 /24 我想为 /24 网络的所有可能值构建一个数组(即 IP 地址可以是 192.168.1.0 - 192.168.1.255 之间的任何位置)

In order for this to work, I think I have to convert the IP address to binary and then find the bits that will be the host part of the network, which I've done here:为了使它工作,我想我必须将 IP 地址转换为二进制,然后找到将成为网络主机部分的位,我在这里完成了:


function ConvertToBinary{
    param($ipAddress)
   
    [string]$binaryIP = -join ($ipAddress.Split('.') | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')})

    return $binaryIP
}

function FindHost{
    param(
        [string]$binaryIPAddress,
        [int32]$CIDR
    )
    $hostBits = 32-$CIDR
    [string]$myHost = $binaryIPAddress.Substring($binaryIPAddress.Length-$hostBits)

    return $myHost
}


$myip = ConvertToBinary "192.168.3.1"
$myHost = FindHost  $myip 8

I'm a little stuck on how to proceed, so if anyone can help me out or point me in the right direction, it would be much appreciated我对如何进行有点困惑,所以如果有人可以帮助我或指出我正确的方向,将不胜感激

I wrote this using some similar questions to get the first and last address for any subnet given a random IP and mask:我用一些类似的问题写了这篇文章,以获取给定随机 IP 和掩码的任何子网的第一个和最后一个地址:

Function Get-SubnetAddresses {
Param ([IPAddress]$IP,[ValidateRange(0, 32)][int]$maskbits)

  # Convert the mask to type [IPAddress]:
  $mask = ([Math]::Pow(2, $MaskBits) - 1) * [Math]::Pow(2, (32 - $MaskBits))
  $maskbytes = [BitConverter]::GetBytes([UInt32] $mask)
  $DottedMask = [IPAddress]((3..0 | ForEach-Object { [String] $maskbytes[$_] }) -join '.')
  
  # bitwise AND them together, and you've got the subnet ID
  $lower = [IPAddress] ( $ip.Address -band $DottedMask.Address )

  # We can do a similar operation for the broadcast address
  # subnet mask bytes need to be inverted and reversed before adding
  $LowerBytes = [BitConverter]::GetBytes([UInt32] $lower.Address)
  [IPAddress]$upper = (0..3 | %{$LowerBytes[$_] + ($maskbytes[(3-$_)] -bxor 255)}) -join '.'

  # Make an object for use elsewhere
  Return [pscustomobject][ordered]@{
    Lower=$lower
    Upper=$upper
  }
}

Usage looks like:用法如下:

Get-IPAddresses 10.43.120.8 22

Lower       Upper        
-----       -----        
10.43.120.0 10.43.123.255

And I put this together to generate the whole list.我把它放在一起来生成整个列表。 I'm sure this could be done better, but the simple instructions run fast enough:我确信这可以做得更好,但简单的指令运行速度足够快:

Function Get-IPRange {
param (
  [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName)][IPAddress]$lower,
  [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName)][IPAddress]$upper
)
  # use lists for speed
  $IPList = [Collections.ArrayList]::new()
  $null = $IPList.Add($lower)
  $i = $lower

  # increment ip until reaching $upper in range
  while ( $i -ne $upper ) { 
    # IP octet values are built back-to-front, so reverse the octet order
    $iBytes = [BitConverter]::GetBytes([UInt32] $i.Address)
    [Array]::Reverse($iBytes)

    # Then we can +1 the int value and reverse again
    $nextBytes = [BitConverter]::GetBytes([UInt32]([bitconverter]::ToUInt32($iBytes,0) +1))
    [Array]::Reverse($nextBytes)

    # Convert to IP and add to list
    $i = [IPAddress]$nextBytes
    $null = $IPList.Add($i)
  }

  return $IPList
}

Converting to [IPAddress] on the last line is nice for validating all the results are real, but probably not necessary if you just want the ipv4 strings.在最后一行转换为[IPAddress]可以很好地验证所有结果是否真实,但如果您只想要 ipv4 字符串,则可能没有必要。 Usage:用法:

Get-SubnetAddresses 10.43.120.8 30 | Get-IPRange | Select -ExpandProperty IPAddressToString
10.43.120.8
10.43.120.9
10.43.120.10
10.43.120.11

Sources:资料来源:

I have tried to solve this issue using counting method.我尝试使用计数方法来解决这个问题。

https://github.com/cvasudev/SimpleIPProjects_Powershell/blob/main/Get-IPlist/Get-iplist.ps1 https://github.com/cvasudev/SimpleIPProjects_Powershell/blob/main/Get-IPlist/Get-iplist.ps1

Let me know if this helps.让我知道这是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM