Check if IP falls in netmask

Hi Guys… I am trying to workout how I can check if an IP falls within a range.

I have a list like this

10.4.144.0/24 -  Site 1

10.4.146.0/24 - Site 1

10.4.148.0/22 - Site 2

10.4.152.0/21 - Site 3

10.4.164.0/22 - Site 4

10.4.172.0/23 - Site 5

10.4.176.0/24 - Site 6

I’m trying to use the IP address of the client to look up the site that it is in but first, I need to work out if it falls in that subnet

My skills in subnetting are almost non-existent so it’s probably more a point of I just don’t know what I need to google, so any help or references would be good.

So, you are just trying to ping the whole subnet?

You can directly ping subnets to see what does and does not respond. There are lots of examples all over the web, even pre-built scripts for such an effort.

 

https://gallery.technet.microsoft.com/scriptcenter/Fast-asynchronous-ping-IP-d0a5cf0e

https://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-to-ping-15e0610a

https://www.powershellgallery.com/packages/PSnmap/1.3.1

https://www.thomasmaurer.ch/2011/11/powershell-ping-ip-range

https://www.sherweb.com/blog/fun-with-powershell-the-less-than-simple-way-to-scan-an-ip-range


 

Or buy this

 

https://www.vexasoft.com/pages/ping-subnet
As for subnetting, there are calulators for that. ;^}
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Subnet-db45ec74

https://gallery.technet.microsoft.com/scriptcenter/ipcalc-PowerShell-Script-01b7bd23


 

 

I would covert it to bytes and then and it with the subnet mask to find the network.

To answer this question we need to use 2 tests:

  1. Is the provided IP address have same network portion of the IP address as the test subnet?
  2. Is the provided IP address have same subnet mask as the test subnet?

Using functions of the AZSBTools PowerShell module we can test for both conditions:

$IPAddress  = '10.11.12.13'
$SubnetMask = '255.255.255.0'
$TestSubnet = '10.11.12.0/24'

Install-Module AZSBTools
$TestSubnetMask = Convert-MaskLengthToIpAddress -MaskLength ($TestSubnet.Split('/')[1])
$IPNetwork = (Get-IPv4Details -IPAddress $IPAddress -SubnetMask $SubnetMask).NetDottedDecimal
$TestNetwork = (Get-IPv4Details -IPAddress ($TestSubnet.Split('/')[0]) -SubnetMask $TestSubnetMask).NetDottedDecimal
if ($SubnetMask -eq $TestSubnetMask -and $IPNetwork -eq $TestNetwork) {
    "IP address $IPAddress (Mask $SubnetMask) IS in the same $TestSubnet subnet"
} else {
    "IP address $IPAddress (Mask $SubnetMask) is NOT in the same $TestSubnet subnet"
}

this returns

IP address 10.11.12.13 (Mask 255.255.255.0) IS in the same 10.11.12.0/24 subnet

using input

$IPAddress  = '10.11.12.13'
$SubnetMask = '255.255.255.224'
$TestSubnet = '10.11.12.0/24'

returns

IP address 10.11.12.13 (Mask 255.255.255.224) is NOT in the same 10.11.12.0/24 subnet

similarly, using input

$IPAddress  = '10.11.11.13'
$SubnetMask = '255.255.255.0'
$TestSubnet = '10.11.12.0/24'

returns

IP address 10.11.11.13 (Mask 255.255.255.0) is NOT in the same 10.11.12.0/24 subnet

@postanote no not pinging at all.
I am pulling info from a CMDB and each CI has an IP address which I want to reference back and provide an actual site name for.

I think that @Sams solution will work and I’ll try that first.

 

 

Ah!, understood, and ditto regarding sam’s reponse.

@Sam boutros does that SB-Tools plugin still exist?

@Gary, SB-Tools PS module has been retired. Most functions are now in AZSBTools module in the PS Gallery.

You can sort of do this stuff yourself.

$ip = '172.1.2.10'
$mask = '255.255.255.0'
$ipn = $ip.split('.')
$maskn = $mask.split('.')

$netn = 0,0,0,0

0..3 | foreach {
  $netn[$_] = $ipn[$_] -band $maskn[$_]
}


$net = $netn -join '.'

$net

# output
172.1.2.0

Hmm, is there a way to define this?

[int[]]$ipn -band [int[]]$maskn

Method invocation failed because [System.Int32[]] does not contain a method named 'op_BitwiseAnd'.

@Sam I was going off your sample and was looking for ‘Convert-MaskLengthToIpAddress’ maybe its renamed in the new module.

I’ll dig deeper and see if I can find it.

thanks.

I found something that works so far…
https://gallery.technet.microsoft.com/scriptcenter/Validate-an-Ipaddress-is-03481731

@ Gary:

PS D:\Sandbox> Get-Command -Module AZSBTools

CommandType     Name                                               Version    Source                                                                                             
-----------     ----                                               -------    ------                                                                                             
Function        Connect-Computer                                   1.128.100  AZSBTools                                                                                          
Function        Convert-IpAddressToMaskLength                      1.128.100  AZSBTools                                                                                          
Function        Convert-MaskLengthToIpAddress                      1.128.100  AZSBTools                                                                                          
Function        ConvertTo-EnhancedHTML                             1.128.100  AZSBTools                                                                                          
Function        ConvertTo-EnhancedHTMLFragment                     1.128.100  AZSBTools                                                                                          
Function        Deploy-AzureARMVM                                  1.128.100  AZSBTools                                                                                          
Function        Expand-Json                                        1.128.100  AZSBTools                                                                                          
Function        Export-SessionCommand                              1.128.100  AZSBTools                                                                                          
Function        Get-AzureRMDiskSpace                               1.128.100  AZSBTools                                                                                          
Function        Get-AzureRMVMBackup                                1.128.100  AZSBTools                                                                                          
Function        Get-DCList                                         1.128.100  AZSBTools                                                                                          
Function        Get-IPv4Details                                    1.128.100  AZSBTools                                                                                          
Function        Get-IPv4Summary                                    1.128.100  AZSBTools                                                                                          
Function        Get-PageFile                                       1.128.100  AZSBTools                                                                                          
Function        Get-SBADComputer                                   1.128.100  AZSBTools                                                                                          
Function        Get-SBCredential                                   1.128.100  AZSBTools                                                                                          
Function        Get-SBDisk                                         1.128.100  AZSBTools                                                                                          
Function        Get-SBWMI                                          1.128.100  AZSBTools                                                                                          
Function        Import-SessionCommands                             1.128.100  AZSBTools                                                                                          
Function        New-SBAZServicePrincipal                           1.128.100  AZSBTools                                                                                          
Function        Next-IP                                            1.128.100  AZSBTools                                                                                          
Function        Remove-PageFile                                    1.128.100  AZSBTools                                                                                          
Function        Set-PageFile                                       1.128.100  AZSBTools                                                                                          
Function        Tag-AzureVM                                        1.128.100  AZSBTools                                                                                          
Function        Test-SameSubnet                                    1.128.100  AZSBTools                                                                                          
Function        Test-SBNetConnection                               1.128.100  AZSBTools                                                                                          
Function        Validate-NameResolution                            1.128.100  AZSBTools                                                                                          
Function        Write-Log                                          1.128.100  AZSBTools  

See second function. Do you have the latest version?

I also neglected to mention there’s already a function to test this in the AZSBTools module called Test-SameSubnet, see

help Test-SameSubnet -Show

It returns the common subnet if the 2 input IP/mask pairs are in the same subnet, or FALSE if not. Examples:

$IP1        = '10.11.12.13'
$Mask1      = '255.255.255.224'
$TestSubnet = '10.11.12.0/24'

$IP2 = $TestSubnet.Split('/')[0]
$Mask2 = Convert-MaskLengthToIpAddress -MaskLength ($TestSubnet.Split('/')[1])
Test-SameSubnet -IP1 $IP1 -Mask1 $Mask1 -IP2 $IP2 -Mask2 $Mask2

returns the common subnet 10.11.12.0/27
whereas

$IP1        = '10.11.11.13'
$Mask1      = '255.255.255.224'
$TestSubnet = '10.11.12.0/24'

$IP2 = $TestSubnet.Split('/')[0]
$Mask2 = Convert-MaskLengthToIpAddress -MaskLength ($TestSubnet.Split('/')[1])
Test-SameSubnet -IP1 $IP1 -Mask1 $Mask1 -IP2 $IP2 -Mask2 $Mask2

returns False

so your test code can be something like:

# inside some loop that provides values for the following 3 variables such as:
# $IP1        = '10.11.12.13'
# $Mask1      = '255.255.255.224'
# $TestSubnet = '10.11.12.0/24'
if (Test-SameSubnet -IP1 $IPAddress -Mask1 $SubnetMask -IP2 ($TestSubnet.Split('/')[0]) -Mask2 (Convert-MaskLengthToIpAddress -MaskLength ($TestSubnet.Split('/')[1]))) {
    # IP IS in the given subnet
} else {
    # IP is MOT int he given subnet
}

I got the one that said current version from this link.
https://www.powershellgallery.com/packages/AZSBTools/1.118.100

I thought the higher ones must have been betas or something.

I’ll update now and try again.
Thanks.