Discontiguous IP ranges in one device group


#1

I have several blocks of IP space that I’m looking to create an application for. To begin I’m trying to dynamically grab the devices in a single device group
10.102.0.0/20 10.200.41.0/24 10.200.42.0/23 10.200.43.0/24

I know there’s gotta be a regex answer but every time I’ve built one it drops from almost 300 devices known back to zero.


#2

Did you really mean /23 on the 10.200.42.0 subnet - as I believe that would overlap with the 10.200.43.0/24.


#3

Ahh yes, I was typing a bit fast. It’s 10.200.41-44, so I meant 41.0/24, 42.0/23, and 44.0/24


#4

Assuming you meant /24 on the 10.200.42.0 subnet, I have been playing around with this REGEX puzzle and although I’m not a REGEX guru I think I come up with something that will work:
\b10.102.([0-9]|10|11|12|13|14|15).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b|\b10.200.4[1-3].(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

I tested it at regexr.com and it seemed to match your ranges. It’s not pretty and probably not optimized for production, but it seems to work.


#5

So, it seems we’re looking for this:

10.200.41-44, so I meant 41.0/24, 42.0/23, and 44.0/24

That would be:

/^10\.200\.4[1-4]\./

Deconstructed

/    -- start of regex
^    -- match start of IP address
10   -- literal number `10`
\.   -- match a period
200  -- literal number `200`
\.   -- another period
4    -- literal number `4`
[1-4] -- match numbers `1` through `4` inclusive
\.   -- literal period
/    -- end of regex

Let us know if it works.


#6

With the string /^10.(102.\d|1[0-5]|200.4[1-4])./

I seem to get the 10.200.41-44.0 alright, and the 10.102.0-9 comes in correctly, but I’m missing some responses in the 10-15 3rd octet


#7

Final Answer:
/^10.(102.\d|102.1[0-5]|200.4[1-4])./

It didn’t seem to like the truncation in OR 1 OR 2 with OR 3 having the second and third octets change. It works, thanks all!