<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to calculate subnet from list of IP addresses? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-subnet-from-list-of-IP-addresses/m-p/645409#M223464</link>
    <description>&lt;P&gt;I am guessing that the machine learning remark is sarcasm, but a straight-face answer is no. &amp;nbsp;Machine learning deals with probabilistic computation, whereas this one is deterministic. &amp;nbsp;The challenge is that SPL may not be the best tool to perform this type of calculation. &amp;nbsp;But (most) any mathematical computation can be achieved if your only choice is SPL. (SPL does provide a couple convenience functions/commands.)&lt;/P&gt;&lt;P&gt;The following is an example of a labored "solution". &amp;nbsp;A small consideration is given in case the number of raw events is huge.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=zeek sourcetype=zeek_conn
| stats values(src) as src by resp_l2_addr
| mvexpand src ``` in order to take advantage of SPL's IP sort ```
| sort ip(src)
| stats list(src) as src by resp_l2_addr ``` src is sorted in IP order ```
| eval minip = mvindex(src, 0), maxip = mvindex(src, -1)
| eval mask = mvrange(1, 33) ``` do not consider 0 ```
| eval match = mvmap(mask, if(cidrmatch(minip . "/" . mask, minip) AND cidrmatch(minip . "/" . mask, maxip), "yes", null()))
| eval minsubnet = minip . "/" . mvcount(match)&lt;/LI-CODE&gt;&lt;P&gt;Note, the resultant minsubnet from your example will be based on minip 10.0.0.5, not on an arbitrarily determined network address such as 10.0.0.0. &amp;nbsp;This sort of violates IPv4's routing protocol. &amp;nbsp;But I suspect that you are just seeking a convenient notation rather than seeking to configure routing with this calculation. &amp;nbsp;If you want to have a conformant representation, you can subtract 1 from minip and add 1 to maxip.&lt;/P&gt;&lt;P&gt;Otherwise, you can arbitrarily force 0 onto the least significant octet of minip, and arbitrarily force 255 onto maxip. &amp;nbsp;The possibilities are endless yet any choice is arbitrary.&lt;/P&gt;&lt;P&gt;Here is an emulation using the sample data you provided. &amp;nbsp;You can play with it and compare with your real data&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time
| eval src=mvappend("10.0.0.5", "10.0.0.100", "10.0.1.5")
``` data emulation above ```&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 01 Jun 2023 16:47:28 GMT</pubDate>
    <dc:creator>yuanliu</dc:creator>
    <dc:date>2023-06-01T16:47:28Z</dc:date>
    <item>
      <title>How to calculate subnet from list of IP addresses?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-subnet-from-list-of-IP-addresses/m-p/645247#M223433</link>
      <description>&lt;P&gt;I'm trying to come up with a way to output to a lookup file a list of calculated network addresses given a list of IP addresses. By using a destination MAC address and a source IP, I'm able to group together a list of IPs that are using the same gateway with the following:&lt;/P&gt;
&lt;P&gt;`index=zeek sourcetype=zeek_conn&lt;BR /&gt;| stats values(src) by resp_l2_addr`&lt;/P&gt;
&lt;P&gt;But from here, I need to take those src values and have Splunk give me the smallest subnet that covers that range of addresses. For example, if I have 10.0.0.5 and 10.0.1.5 in the same list, I would need the query to say, based on these two addresses, there is a 10.0.0.0/23 network.&lt;/P&gt;
&lt;P&gt;I feel this is more of a machine learning type of problem, but wanted to see if anyone has come up with something similar that could solve this. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 13:55:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-subnet-from-list-of-IP-addresses/m-p/645247#M223433</guid>
      <dc:creator>bald_balrog</dc:creator>
      <dc:date>2023-05-31T13:55:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate subnet from list of IP addresses?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-subnet-from-list-of-IP-addresses/m-p/645409#M223464</link>
      <description>&lt;P&gt;I am guessing that the machine learning remark is sarcasm, but a straight-face answer is no. &amp;nbsp;Machine learning deals with probabilistic computation, whereas this one is deterministic. &amp;nbsp;The challenge is that SPL may not be the best tool to perform this type of calculation. &amp;nbsp;But (most) any mathematical computation can be achieved if your only choice is SPL. (SPL does provide a couple convenience functions/commands.)&lt;/P&gt;&lt;P&gt;The following is an example of a labored "solution". &amp;nbsp;A small consideration is given in case the number of raw events is huge.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=zeek sourcetype=zeek_conn
| stats values(src) as src by resp_l2_addr
| mvexpand src ``` in order to take advantage of SPL's IP sort ```
| sort ip(src)
| stats list(src) as src by resp_l2_addr ``` src is sorted in IP order ```
| eval minip = mvindex(src, 0), maxip = mvindex(src, -1)
| eval mask = mvrange(1, 33) ``` do not consider 0 ```
| eval match = mvmap(mask, if(cidrmatch(minip . "/" . mask, minip) AND cidrmatch(minip . "/" . mask, maxip), "yes", null()))
| eval minsubnet = minip . "/" . mvcount(match)&lt;/LI-CODE&gt;&lt;P&gt;Note, the resultant minsubnet from your example will be based on minip 10.0.0.5, not on an arbitrarily determined network address such as 10.0.0.0. &amp;nbsp;This sort of violates IPv4's routing protocol. &amp;nbsp;But I suspect that you are just seeking a convenient notation rather than seeking to configure routing with this calculation. &amp;nbsp;If you want to have a conformant representation, you can subtract 1 from minip and add 1 to maxip.&lt;/P&gt;&lt;P&gt;Otherwise, you can arbitrarily force 0 onto the least significant octet of minip, and arbitrarily force 255 onto maxip. &amp;nbsp;The possibilities are endless yet any choice is arbitrary.&lt;/P&gt;&lt;P&gt;Here is an emulation using the sample data you provided. &amp;nbsp;You can play with it and compare with your real data&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time
| eval src=mvappend("10.0.0.5", "10.0.0.100", "10.0.1.5")
``` data emulation above ```&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 01 Jun 2023 16:47:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-subnet-from-list-of-IP-addresses/m-p/645409#M223464</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-06-01T16:47:28Z</dc:date>
    </item>
  </channel>
</rss>

