none
Using PowerShell to produce list of IP addresses from a regex expression RRS feed

  • Question

  • Hi,

    I would to write a script that can take a regex of an IP range and output a full list of IPs from it to use for validation. An example:

    IP: 192.168.240.224/28
    Range that should be produced: 192.168.240.224 thru 192.168.240.239

    regex: 192.168.240.2[2-3][4-9]

    The script could prompt for this regex value and then output a list of the full range of IP addresses. The format shown isn't technically regex but it is the format expected by System Center Configuration Manager (SCCM). Would the best approach be to parse the string and just setup nested for-loops to build the list? I would like to be able to test the regex I develop for accuracy and felt like a list would be a useful way to accomplish that. I don't need to do any large ranges so we're not talking about hundreds of pages of output.

    I'm just wondering the approach that some of you might recommend to see how far off-track I am in my thinking.

    Thank you for your help!

    Rob

    Tuesday, March 27, 2018 2:52 PM

Answers

  • So you want to automatically create the Regex that validates the CIDR then you want to test the IPs that it generates against the Regex. 

    That is what it sounds like you are asking.

    Why test?  If you have a valid CIDR then just use it or convert it as needed.  I think you may be over-thinking this.


    \_(ツ)_/

    • Marked as answer by robwm1 Tuesday, March 27, 2018 4:36 PM
    Tuesday, March 27, 2018 4:35 PM

All replies

  • just to keep you on your toes..... 192.168.240.224 and 239 not a usable addresses in that range.... so you're going to have to work that in as well.
    Tuesday, March 27, 2018 3:12 PM
  • There is no RegEx that can take an arbitrary IP subnet CIDR block and generate a list or range of IPs.  If you search you will find tools that can do this.


    \_(ツ)_/


    • Edited by jrv Tuesday, March 27, 2018 3:16 PM
    Tuesday, March 27, 2018 3:15 PM
  • DON'T YOU RAIN ON HIS PARADE JRV!
    Tuesday, March 27, 2018 3:17 PM
  • Sorry.  RegEx is just not designed to do bit manipulation.  This requires some fancy programming to generate a mask and a the enclosed range.

    Look in the Gallery for some scripts that can do some basic address generation.  Sub-netting a class C net is the easiest but still takes some bit arithmetic to accomplish,

    IP: 192.168.240.224/28

    The easiest way here is to take the mask length an generate the addresses of the NET ID>

    0..($len - 1) | %{ '192.168.240'. + (224 + $_) }

    Then throw away the first an last addresses or:

    1..($len -2) | %{ '192.168.240.' + (224 + $_) }


    \_(ツ)_/



    • Edited by jrv Tuesday, March 27, 2018 3:40 PM
    Tuesday, March 27, 2018 3:26 PM
  • Hi Grim,

    I'm not sure what you mean exactly. Are you thinking that 224 is the gateway IP and 239 is the broadcast IP? 

    I need SCCM to take in account the full IP range. The big picture is that I am trying to block SCCM from pushing large packages to prevent saturating these sites as they are low-bandwidth. This is being accomplished by using the IP Subnet value. Instead of listing each IP using individual queries, I can use some regex to make it less work in accounting for the range by using only one query, ideally.

    Am I understanding your comment correctly? My script doesn't really need any "intelligence". I just need a list to validate that my regex includes the correct IP addresses. If I get the regex wrong, I could be blocking computers that shouldn't be blocked. A simple tool is all I need.

    -Rob


    • Edited by robwm1 Tuesday, March 27, 2018 3:42 PM
    Tuesday, March 27, 2018 3:36 PM
  • If you want the full range then just generate all addresses.

    0..($len -1)| %{ '192.168.240.' + (224 + $_) }


    \_(ツ)_/

    Tuesday, March 27, 2018 3:39 PM
  • Generating all of the addresses is all I need. That is my goal here. Sorry if I wasn't clear.
    • Edited by robwm1 Tuesday, March 27, 2018 3:45 PM
    Tuesday, March 27, 2018 3:45 PM
  • Then you are set.  SCCM will never use addresses that are net IDs or subnet broadcast addresses.  The routers will reject this as the routers are really what is using the info.  The hosts really know nothing about the IP and mask.  The only place it is used is in the internal router and on the adapter.


    \_(ツ)_/

    Tuesday, March 27, 2018 3:47 PM
  • regex: 192.168.240.2[2-3][4-9]

    This is the format SCCM requires in this scenario. You don't feel like setting up a few loops by parsing this string could produce an accurate list? I'd like to test/validate this string before it is implemented and I'm try to reach this goal in the simplest terms. I only need to test variations of the string given as an example.

    Tuesday, March 27, 2018 4:05 PM
  • Are you asking how to generate a RegEx for any subnet?  That is a completely different question from what you asked at first.


    \_(ツ)_/

    Tuesday, March 27, 2018 4:08 PM
  • No, I get CIDR notation like 192.168.240.224/28 from Orion and then I come up with a regex format like 192.168.240.2[2-3][4-9] that I believe will match the range. I put that regex string through a script that outputs the possible IP address range. From there I should be able to quickly see if I got the string correct.

    I think a script like I am suggesting would be easier than giving it the CIDR notation and then have it hand me the regex string that SCCM understands. For me, I feel like converting CIDR notation to the string SCCM needs is a little beyond my ability. I don't see anything online that could test the string SCCM requires. I can't invest too much time figuring this out and I'm not asking the community to write it for me. :)

    Does that make sense?

    Tuesday, March 27, 2018 4:20 PM
  • So you want to automatically create the Regex that validates the CIDR then you want to test the IPs that it generates against the Regex. 

    That is what it sounds like you are asking.

    Why test?  If you have a valid CIDR then just use it or convert it as needed.  I think you may be over-thinking this.


    \_(ツ)_/

    • Marked as answer by robwm1 Tuesday, March 27, 2018 4:36 PM
    Tuesday, March 27, 2018 4:35 PM