Powershell-基于CSV文件检查IP地址范围

Powershell-基于CSV文件检查IP地址范围,powershell,Powershell,我一直在开发虚拟机供应脚本。我的问题是:我这里有如下字符串。现在,我想根据ip地址范围添加路由。我正在使用带有BACKUPIP列的CSV文件 如果备份IP在10.10.104.1到10.10.107.254的范围内,它将工作route add xx.xx.xx.xx mask 255.255.0 xx.xx.xx.xx-p 如果备份IP在10.10.180.1到10.10.185.254的范围内,它将工作路由添加yy.yy.yy掩码255.255.255.0 yy.yy.yy-p 这是我的剧本:

我一直在开发虚拟机供应脚本。我的问题是:我这里有如下字符串。现在,我想根据ip地址范围添加
路由
。我正在使用带有
BACKUPIP
列的CSV文件

如果备份IP在
10.10.104.1
10.10.107.254
的范围内,它将工作
route add xx.xx.xx.xx mask 255.255.0 xx.xx.xx.xx-p

如果备份IP在
10.10.180.1
10.10.185.254
的范围内,它将工作
路由添加yy.yy.yy掩码255.255.255.0 yy.yy.yy-p

这是我的剧本:

Import-Csv -Path .\vm.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {


    # Create the VM, store result in $vm


    if($($row.IP) -eq '???'){
            route add xx.xx.xx.xx mask 255.255.255.0 xx.xx.xx.xx -p
            }
    else{
            
            route add yy.yy.yy.yy mask 255.255.255.0 yy.yy.yy.yy -p
            
            }


}
最近更新:

$rangeFrom104 = '10.10.104.1'
$rangeTo107 = '10.10.107.254'

$rangeFrom180 = '10.10.180.1'
$rangeTo185 = '10.10.185.254'

     

if (([version]$rangeFrom104) -lt ([version]$($row.IP)) -and ([version]$($row.IP)) -lt ([version]$rangeTo107) )
{

route add xx.xx.xx.xx mask 255.255.255.0 xx.xx.xx.xx -p

}

elseif (([version]$rangeFrom180) -lt ([version]$($row.IP)) -and ([version]$($row.IP)) -lt ([version]$rangeTo185) )
{

route add yy.yy.yy.yy mask 255.255.255.0 yy.yy.yy.yy -p

}

非常喜欢建议的
[Version]
方法

下面是另一种将IP地址转换为数值的方法:

function Convert-IPv4ToDecimal ([string]$IpAddress){
    # helper function to return the numeric value (uint32) of a dotted IP
    # address string used for testing if an IP address is in range.
    $n = [uint32[]]$IpAddress.Split('.')
    # or use: $n = [uint32[]]([IpAddress]$IpAddress).GetAddressBytes()

    # to get the obsolete property ([IpAddress]$IpAddress).Address
    # you need to do the math in reversed order.
    # return [uint32] ($n[3] -shl 24) + ($n[2] -shl 16) + ($n[1] -shl 8) + $n[0]

    # for comparing different ranges as in this question, do not reverse the byte order
    return [uint32] ($n[0] -shl 24) + ($n[1] -shl 16) + ($n[2] -shl 8) + $n[3]
}



$startRange1 = Convert-IPv4ToDecimal '172.25.104.1'
$endRange1   = Convert-IPv4ToDecimal '172.25.107.254'

$startRange2 = Convert-IPv4ToDecimal '172.25.112.1'
$endRange2   = Convert-IPv4ToDecimal '172.25.115.254'

Import-Csv -Path .\vm.csv -UseCulture | ForEach-Object {
    # Create the VM, store result in $vm

    # convert the .BACKUPIP to numeric value
    $backupIp = Convert-IPv4ToDecimal $_.BACKUPIP
    # test the IP range
    if ($backupIp -ge $startRange1 -and $backupIp -le $endRange1) {
        Write-Host "BACKUPIP '$($_.BACKUPIP)' is in Range 1"
        route add xx.xx.xx.xx mask 255.255.255.0 xx.xx.xx.xx -p
    }
    elseif ($backupIp -ge $startRange2 -and $backupIp -le $endRange2) {
        Write-Host "BACKUPIP '$($_.BACKUPIP)' is in Range 2"
        route add yy.yy.yy.yy mask 255.255.255.0 yy.yy.yy.yy -p
    }
    else {
        Write-Warning "No range defined for IP address '$($_.BACKUPIP)'"
    }
}

非常喜欢建议的
[Version]
方法

下面是另一种将IP地址转换为数值的方法:

function Convert-IPv4ToDecimal ([string]$IpAddress){
    # helper function to return the numeric value (uint32) of a dotted IP
    # address string used for testing if an IP address is in range.
    $n = [uint32[]]$IpAddress.Split('.')
    # or use: $n = [uint32[]]([IpAddress]$IpAddress).GetAddressBytes()

    # to get the obsolete property ([IpAddress]$IpAddress).Address
    # you need to do the math in reversed order.
    # return [uint32] ($n[3] -shl 24) + ($n[2] -shl 16) + ($n[1] -shl 8) + $n[0]

    # for comparing different ranges as in this question, do not reverse the byte order
    return [uint32] ($n[0] -shl 24) + ($n[1] -shl 16) + ($n[2] -shl 8) + $n[3]
}



$startRange1 = Convert-IPv4ToDecimal '172.25.104.1'
$endRange1   = Convert-IPv4ToDecimal '172.25.107.254'

$startRange2 = Convert-IPv4ToDecimal '172.25.112.1'
$endRange2   = Convert-IPv4ToDecimal '172.25.115.254'

Import-Csv -Path .\vm.csv -UseCulture | ForEach-Object {
    # Create the VM, store result in $vm

    # convert the .BACKUPIP to numeric value
    $backupIp = Convert-IPv4ToDecimal $_.BACKUPIP
    # test the IP range
    if ($backupIp -ge $startRange1 -and $backupIp -le $endRange1) {
        Write-Host "BACKUPIP '$($_.BACKUPIP)' is in Range 1"
        route add xx.xx.xx.xx mask 255.255.255.0 xx.xx.xx.xx -p
    }
    elseif ($backupIp -ge $startRange2 -and $backupIp -le $endRange2) {
        Write-Host "BACKUPIP '$($_.BACKUPIP)' is in Range 2"
        route add yy.yy.yy.yy mask 255.255.255.0 yy.yy.yy.yy -p
    }
    else {
        Write-Warning "No range defined for IP address '$($_.BACKUPIP)'"
    }
}
在.Net中存在以下问题:

正如@Theo所评论的,该属性已过时:

此属性已被弃用。它依赖于地址族。
请使用IPAddress.Equals方法执行比较

我猜这是因为遵守了IPv6(但我认为该属性不会轻易消失,因为这可能会破坏一些遗留程序)。无论如何,这并不意味着整个
[System.Net.IPAddress]
类都不受欢迎。这意味着您也可以使用
GetAddressBytes
方法,我认为该方法比自定义函数或依赖(smart!
[version]
)类型更好,但也都限于IPv4(~4字节)

使用
GetAddressBytes
方法,您可以简单地将字节转换为字符串,只要字节数组大小相同(例如,两个IPv4),该字符串的格式是可比较的(例如
'10'-gt'0A'
):

如果您需要使您的脚本兼容并将IP地址与IPv4范围和IPv6范围进行比较,您可以考虑将所有IP地址映射到IPv6地址:使用:<代码> $MyIPAdv.MtotoIPv6().GETAddiSsByTeSe()/代码>(<代码> -IPv6 < /Cult>开关):


更新2020-09-06:
目前尚不清楚该房产是否真的过时了。请参阅:。
无论如何,在使用
Address
属性进行比较时存在一个陷阱,因为该地址似乎是以小尾端格式从内存中读取的,请参见:,从而导致
10.10.104.1
1
)中的最后一个字节变得最重要。 这意味着,如果相关IP地址中的多个字节之间存在差异,则比较
Address
属性可能会得出错误的结果:

([IPAddress]'0.0.0.1').Address -lt ([IPAddress]'0.0.1.0').Address
False
在.Net中存在以下问题:

正如@Theo所评论的,该属性已过时:

此属性已被弃用。它依赖于地址族。
请使用IPAddress.Equals方法执行比较

我猜这是因为遵守了IPv6(但我认为该属性不会轻易消失,因为这可能会破坏一些遗留程序)。无论如何,这并不意味着整个
[System.Net.IPAddress]
类都不受欢迎。这意味着您也可以使用
GetAddressBytes
方法,我认为该方法比自定义函数或依赖(smart!
[version]
)类型更好,但也都限于IPv4(~4字节)

使用
GetAddressBytes
方法,您可以简单地将字节转换为字符串,只要字节数组大小相同(例如,两个IPv4),该字符串的格式是可比较的(例如
'10'-gt'0A'
):

如果您需要使您的脚本兼容并将IP地址与IPv4范围和IPv6范围进行比较,您可以考虑将所有IP地址映射到IPv6地址:使用:<代码> $MyIPAdv.MtotoIPv6().GETAddiSsByTeSe()/代码>(<代码> -IPv6 < /Cult>开关):


更新2020-09-06:
目前尚不清楚该房产是否真的过时了。请参阅:。
无论如何,在使用
Address
属性进行比较时存在一个陷阱,因为该地址似乎是以小尾端格式从内存中读取的,请参见:,从而导致
10.10.104.1
1
)中的最后一个字节变得最重要。 这意味着,如果相关IP地址中的多个字节之间存在差异,则比较
Address
属性可能会得出错误的结果:

([IPAddress]'0.0.0.1').Address -lt ([IPAddress]'0.0.1.0').Address
False

您遇到了什么问题或错误?请使用
[version]
类型加速器。是的,真的!这是有效的…>>><代码>[version]'172.25.104.1'-le[version]'172.25.106.99'-ge[version]'172.25.107.254'您遇到了什么问题或错误?请使用
[version]
类型加速器。是的,真的!这是有效的…>>><代码>[version]'172.25.104.1'-le[version]'172.25.106.99'-ge[version]'172.25.107.254':)同时,我已经更新了我的脚本。与你的剧本相比,我的剧本是业余的。@Arbelac很棒!我看你采纳了[版本]的想法。我自己从没想过。遗憾的是,李·戴利没有把这个作为答案,我肯定会把它投得更高。与你的剧本相比,我的剧本是业余的。:)@阿贝拉克一点也不业余。我将此作为备选方案。@Theo-您可以使用此>>
([ipaddress]'172.25.104.1')获取ip地址的十进制值。地址
:)同时,我已经更新了脚本。与你的剧本相比,我的剧本是业余的。@Arbelac很棒!我看你采纳了[版本]的想法。我自己从没想过。遗憾的是,李·戴利没有把这个作为答案,我肯定会把它投得更高。与你的剧本相比,我的剧本是业余的。:)@阿贝拉克一点也不业余。我保留此选项。@Theo-您可以使用此>>
([ipaddress])获取ip地址的十进制值
([IPAddress]'0.0.0.1').Address -lt ([IPAddress]'0.0.1.0').Address
False