【问题标题】:Checking for a range in Powershell在 Powershell 中检查范围
【发布时间】:2012-01-12 00:37:27
【问题描述】:

我正在尝试编写一个脚本来获取计算机的 IP 地址并检查它是否属于特定的 IP 范围。例如,如果机器的 IP 是 192.168.0.5,脚本将检查它是否在 192.168.0.10 到 192.168.0.20 之间。到目前为止,我的脚本只能获取远程机器的 IP,但我无法弄清楚如何检查 IP 是否在特定范围内。我会很感激任何建议。谢谢。

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    让 .NET 完成这项工作可能是最简单的 - 有一个 IPAddress 类可以将它们解析为它们的数值以进行比较。您可以将以下功能放入您的个人资料中(或添加到模块中,或者您更喜欢添加 PS 功能):

    function IsIpAddressInRange {
    param(
            [string] $ipAddress,
            [string] $fromAddress,
            [string] $toAddress
        )
    
        $ip = [system.net.ipaddress]::Parse($ipAddress).GetAddressBytes()
        [array]::Reverse($ip)
        $ip = [system.BitConverter]::ToUInt32($ip, 0)
    
        $from = [system.net.ipaddress]::Parse($fromAddress).GetAddressBytes()
        [array]::Reverse($from)
        $from = [system.BitConverter]::ToUInt32($from, 0)
    
        $to = [system.net.ipaddress]::Parse($toAddress).GetAddressBytes()
        [array]::Reverse($to)
        $to = [system.BitConverter]::ToUInt32($to, 0)
    
        $from -le $ip -and $ip -le $to
    }
    

    用法如下:

    PS> IsIpAddressInRange "192.168.0.5" "192.168.0.10" "192.168.0.20"
    False
    PS> IsIpAddressInRange "192.168.0.15" "192.168.0.10" "192.168.0.20"
    True
    

    【讨论】:

      【解决方案2】:

      这是 PowerShell 中 -Like 运算符的一项简单任务:

      例如:

      $ipaddress -like "192.168.1.*"
      

      【讨论】:

        【解决方案3】:

        我写了一个小函数来做到这一点:

        function Test-IpAddressInRange {
            [CmdletBinding()]
            param (
                [Parameter(Position = 0, Mandatory = $true)][ipaddress]$from,
                [Parameter(Position = 1, Mandatory = $true)][ipaddress]$to,
                [Parameter(Position = 2, Mandatory = $true)][ipaddress]$target
            )
            $f=$from.GetAddressBytes()|%{"{0:000}" -f $_}   | & {$ofs='-';"$input"}
            $t=$to.GetAddressBytes()|%{"{0:000}" -f $_}   | & {$ofs='-';"$input"}
            $tg=$target.GetAddressBytes()|%{"{0:000}" -f $_}   | & {$ofs='-';"$input"}
            return ($f -le $tg) -and ($t -ge $tg)
        }
        

        测试结果:

        PS C:\> Test-IpAddressInRange "192.168.0.1"  "192.168.0.100"  "192.168.0.1"
        True
        PS C:\> Test-IpAddressInRange "192.168.0.1"  "192.168.0.100"  "192.168.0.100"
        True
        PS C:\> Test-IpAddressInRange "192.168.90.1"  "192.168.100.100"  "192.168.101.101"
        False
        PS C:\>
        

        【讨论】:

          【解决方案4】:

          如果掩码是 255.255.255.0 就简单了 在这种情况下,您可以执行以下操作:

          $a = [ipaddress]"192.168.0.5"
          
          10..20 -contains $a.IPAddressToString.split('.')[3]
          
          true
          

          对于不同的子掩码,您必须检查每个 ip 的八位字节。

          【讨论】:

            【解决方案5】:

            例如,要查看 IP 是否在 192.168.1.10192.168.1.20 范围内,您可以使用正则表达式和 -match 运算符

             $ip -match "192\.168\.1\.0?(1\d)|20"
            

            0? 允许前导 0。

            同样,对于任何范围,您都可以使用正则表达式。

            对于非常简单的范围,在. 上使用字符串拆分并对组件进行操作。

            【讨论】:

              【解决方案6】:

              如果您喜欢整个简洁,这里是 AndyHerb 的评论和 E.Z. 的功能混合。哈特的回答:

              function IsIpAddressInRange {
                param(
                  [System.Version] $IPAddress,
                  [System.Version] $FromAddress,
                  [System.Version] $ToAddress
                )
                $FromAddress -le $IPAddress -and $IPAddress -le $ToAddress
              }
              

              例子:

              IsIpAddressInRange "192.168.1.50" "192.168.1.30" "192.168.1.100"
              True
              IsIpAddressInRange "192.168.25.75" "192.168.25.0" "192.168.25.255"
              True
              IsIpAddressInRange "192.168.36.240" "192.168.36.0" "192.168.36.100"
              False
              IsIpAddressInRange "192.168.36.240" "192.168.33.0" "192.168.37.0"
              True
              

              【讨论】:

                【解决方案7】:

                在谷歌搜索时遇到了这个,相当高的命中率。只是想说,至少在 powershell 4 和 5 中它要容易得多:

                $range = "10.10.140.0-10.11.15.0"
                $ipStart,$ipEnd = $range.Split("-")
                
                $ipCheck = "10.10.250.255"
                
                ($ipCheck -ge $ipStart) -AND ($ipCheck -le $ipEnd)
                

                如果您在 192.168.25.0-192.168.25.255 范围内提供 IP 192.168.25.75,这不起作用

                【讨论】:

                • 这会产生误报,因为它将所有内容都视为字符串。它只需要稍作调整即可工作:$range = "40.69.0.1-40.69.31.254" $ipStart,$ipEnd = $range.Split("-") $ipCheck = [System.Version]"40.69.177.20" ($ipCheck -ge [System.Version]$ipStart) -AND ($ipCheck -le [System.Version]$ipEnd)
                • 是的,它给出了误报。 $ipAddress = "10.101.141.255" $ipRange = "10.10.140.0-10.11.15.0" $ipStart, $ipEnd = $ipRange.Split("-") ($ipAddress -ge $ipStart) -and ($ipAddress -le $ipEnd) 给出 True 但它必须是 False
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-12-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-12-10
                相关资源
                最近更新 更多