<?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 How to match an IP address to a lookup with IP ranges? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-match-an-IP-address-to-a-lookup-with-IP-ranges/m-p/643463#M222891</link>
    <description>&lt;P&gt;I have an IP field that I'm trying to match against a lookup that contains DHCP ranges.&lt;/P&gt;
&lt;P&gt;For example, assume the lookup contains the following:&lt;/P&gt;
&lt;P&gt;Start_Address,End_Address&lt;BR /&gt;10.0.0.5,10.0.0.254&lt;BR /&gt;10.2.0.5,10.3.0.254&lt;BR /&gt;10.4.0.5,10.4.0.254&lt;/P&gt;
&lt;P&gt;When matching the IP field, 10.0.0.10 would match, but 10.0.0.2 would not. That said, simply determining the CIDR notation (10.0.0.0/24) won't work. I've consider enumerating all IPs, resulting in a huge lookup, but I'm wondering if anyone has any other ideas.&lt;/P&gt;</description>
    <pubDate>Wed, 31 May 2023 03:08:07 GMT</pubDate>
    <dc:creator>ejwade</dc:creator>
    <dc:date>2023-05-31T03:08:07Z</dc:date>
    <item>
      <title>How to match an IP address to a lookup with IP ranges?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-match-an-IP-address-to-a-lookup-with-IP-ranges/m-p/643463#M222891</link>
      <description>&lt;P&gt;I have an IP field that I'm trying to match against a lookup that contains DHCP ranges.&lt;/P&gt;
&lt;P&gt;For example, assume the lookup contains the following:&lt;/P&gt;
&lt;P&gt;Start_Address,End_Address&lt;BR /&gt;10.0.0.5,10.0.0.254&lt;BR /&gt;10.2.0.5,10.3.0.254&lt;BR /&gt;10.4.0.5,10.4.0.254&lt;/P&gt;
&lt;P&gt;When matching the IP field, 10.0.0.10 would match, but 10.0.0.2 would not. That said, simply determining the CIDR notation (10.0.0.0/24) won't work. I've consider enumerating all IPs, resulting in a huge lookup, but I'm wondering if anyone has any other ideas.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 03:08:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-match-an-IP-address-to-a-lookup-with-IP-ranges/m-p/643463#M222891</guid>
      <dc:creator>ejwade</dc:creator>
      <dc:date>2023-05-31T03:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Matching an IP address to a Lookup with IP ranges</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-match-an-IP-address-to-a-lookup-with-IP-ranges/m-p/643467#M222893</link>
      <description>&lt;P&gt;What do you mean the CIDR notation won't work? &amp;nbsp;I break down my DHCP range into CIDR notations and everything works perfectly.&lt;/P&gt;&lt;TABLE border="1" width="75.69974554707379%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="30.025445292620866%"&gt;10.0.0.5 - 10.0.0.254&lt;/TD&gt;&lt;TD width="45.674300254452916%"&gt;&lt;PRE&gt;10.0.0.5/32
10.0.0.6/31
10.0.0.8/29
10.0.0.16/28
10.0.0.32/27
10.0.0.64/26
10.0.0.128/26
10.0.0.192/27
10.0.0.224/28
10.0.0.240/29
10.0.0.248/30
10.0.0.252/31
10.0.0.254/32&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="30.025445292620866%"&gt;10.2.0.5 - 10.3.0.254&lt;/TD&gt;&lt;TD width="45.674300254452916%"&gt;&lt;PRE&gt;10.2.0.5/32
10.2.0.6/31
10.2.0.8/29
10.2.0.16/28
10.2.0.32/27
10.2.0.64/26
10.2.0.128/25
10.2.1.0/24
10.2.2.0/23
10.2.4.0/22
10.2.8.0/21
10.2.16.0/20
10.2.32.0/19
10.2.64.0/18
10.2.128.0/17
10.3.0.0/25
10.3.0.128/26
10.3.0.192/27
10.3.0.224/28
10.3.0.240/29
10.3.0.248/30
10.3.0.252/31
10.3.0.254/32&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="30.025445292620866%"&gt;10.3.0.5 - 10.4.0.254&lt;/TD&gt;&lt;TD width="45.674300254452916%"&gt;&lt;PRE&gt;10.3.0.5/32
10.3.0.6/31
10.3.0.8/29
10.3.0.16/28
10.3.0.32/27
10.3.0.64/26
10.3.0.128/25
10.3.1.0/24
10.3.2.0/23
10.3.4.0/22
10.3.8.0/21
10.3.16.0/20
10.3.32.0/19
10.3.64.0/18
10.3.128.0/17
10.4.0.0/25
10.4.0.128/26
10.4.0.192/27
10.4.0.224/28
10.4.0.240/29
10.4.0.248/30
10.4.0.252/31
10.4.0.254/32&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;You can calculate these CIDR's using for example&amp;nbsp;&lt;A href="https://www.ipaddressguide.com/cidr" target="_blank" rel="noopener"&gt;https://www.ipaddressguide.com/cidr&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 06:50:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-match-an-IP-address-to-a-lookup-with-IP-ranges/m-p/643467#M222893</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-05-16T06:50:11Z</dc:date>
    </item>
    <item>
      <title>Re: Matching an IP address to a Lookup with IP ranges</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-match-an-IP-address-to-a-lookup-with-IP-ranges/m-p/645178#M223411</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/33901"&gt;@yuanliu&lt;/a&gt;this is a great approach. However, I need to be able to do this in SPL at search time.&lt;/P&gt;&lt;P&gt;I was able to develop a little external lookup to do this, using the native ipaddress Python module.&lt;/P&gt;&lt;P&gt;For those of you interested, here's the Python file "cidr_lookup.py".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#!/usr/bin/env python

from __future__ import print_function
import csv
import ipaddress
import sys


# Given start and end IP addresses, return the CIDRs
def lookup(ip_start, ip_end):
    try:
        cidrs = [str(ipaddr) for ipaddr in ipaddress.summarize_address_range(
            ipaddress.IPv4Address(ip_start),
            ipaddress.IPv4Address(ip_end)
            )
        ]
        cidr_list = ','.join(cidrs)
        return cidr_list
    except:
        return ''


def main():
    if len(sys.argv) != 4:
        print("Usage: python cidr_lookup.py "
              "[ip_start field] [ip_end field] [cidr_list field]")
        sys.exit(1)

    ip_start_field = sys.argv[1]
    ip_end_field = sys.argv[2]
    cidr_list_field = sys.argv[3]

    infile = sys.stdin
    outfile = sys.stdout

    r = csv.DictReader(infile)
    header = r.fieldnames

    w = csv.DictWriter(outfile, fieldnames=header)
    w.writeheader()

    for result in r:
        if result[ip_start_field] and result[ip_end_field]:
            result[cidr_list_field] = lookup(result[ip_start_field],
                                             result[ip_end_field])
            if result[cidr_list_field]:
                w.writerow(result)


main()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the transforms.conf.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[cidrlookup]
external_cmd = cidr_lookup.py ip_start ip_end cidr_list
fields_list = ip_start, ip_end, cidr_list&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a screenshot of the lookup in use.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ejwade_1-1685501790520.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/25621iC7447DC8E12178F7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ejwade_1-1685501790520.png" alt="ejwade_1-1685501790520.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 02:57:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-match-an-IP-address-to-a-lookup-with-IP-ranges/m-p/645178#M223411</guid>
      <dc:creator>ejwade</dc:creator>
      <dc:date>2023-05-31T02:57:10Z</dc:date>
    </item>
  </channel>
</rss>

