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.
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.
@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.
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?
# 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
}