Check if IP falls in netmask

Welcome Forums General PowerShell Q&A Check if IP falls in netmask

This topic contains 13 replies, has 4 voices, and was last updated by

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #113191

    Participant
    Points: 0
    Rank: Member

    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.

  • #113198

    Participant
    Points: 10
    Rank: Member
  • #113201
    js

    Participant
    Points: 6
    Rank: Member

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

  • #113207

    Participant
    Points: 0
    Rank: Member

    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
    
  • #113291

    Participant
    Points: 0
    Rank: Member

    @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.

     

     

  • #113293

    Participant
    Points: 10
    Rank: Member

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

  • #113296

    Participant
    Points: 0
    Rank: Member

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

  • #113366

    Participant
    Points: 0
    Rank: Member

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

    • #113452

      Participant
      Points: 0
      Rank: Member

      @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.

  • #113369
    js

    Participant
    Points: 6
    Rank: Member

    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'.
    
  • #113459

    Participant
    Points: 0
    Rank: Member
  • #113470

    Participant
    Points: 0
    Rank: Member

    @ 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?

  • #113473

    Participant
    Points: 0
    Rank: Member

    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
    }
    

You must be logged in to reply to this topic.