<?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: Using field values with greater than and less than to do KV lookups in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-field-values-with-greater-than-and-less-than-to-do-KV/m-p/619999#M215500</link>
    <description>&lt;P&gt;Here's a somewhat kludgy example of how you can do the comparison LT and GT for port range on the MV fields you get from the lookup - most of this is just setup for an example to show MV fields for IPS/ranges.&lt;/P&gt;&lt;P&gt;You need the last 4 lines - note that this will allow for 10 port ranges per local IP in the foreach command&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval last_octet=(random() % 253 + 1)
| eval src_ip=(random() % 253 + 1).".".(random() % 253 + 1).".".(random() % 253 + 1).".".last_octet
| eval src_port=random() % 32768 + 512
| eval n=mvrange(1,11,1) 
| mvexpand n
| eval local_ip="10.1.".n.".".last_octet
| eval low_port=random() % (2048 * n) + 512
| eval high_port=random() % (4096 * n) + low_port
| stats list(local_ip) as local_ip list(low_port) as low_port list(high_port) as high_port by src_ip src_port
| eval ix=-1
| foreach 0 1 2 3 4 5 6 7 8 9 10 [ eval l=tonumber(mvindex(low_port, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)), h=tonumber(mvindex(high_port, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)), ix=if(ix=-1 AND src_port &amp;gt;= l AND src_port &amp;lt;= h, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, ix) ]
| eval local_addr=mvindex(local_ip, ix)
| fields - l h ix&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Nov 2022 23:12:52 GMT</pubDate>
    <dc:creator>bowesmana</dc:creator>
    <dc:date>2022-11-07T23:12:52Z</dc:date>
    <item>
      <title>How to use field values with greater than and less than to do KV lookups?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-field-values-with-greater-than-and-less-than-to-do-KV/m-p/619996#M215498</link>
      <description>&lt;P&gt;I have a KV store based lookup for&amp;nbsp;Port Address Translation.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Given the first 3 octets of a public facing IP and a port, I need to lookup the first 3 octets of the private address from this lookup.&lt;/P&gt;
&lt;P&gt;The lookup contains the first 3 octets of the public IP, the first 3 octets of the private IP, the maximum port for that private IP and the minimum port for that private subnet range.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Starting with a public_address of 123.45.67.8, port 1042 something like this works:&lt;BR /&gt;&lt;BR /&gt;| inputlookup PAT_translation_table&amp;nbsp;where public_address="123.45.67" lower_port&amp;lt;="1042" upper_port&amp;gt;="1042"&lt;BR /&gt;&lt;BR /&gt;It returns the field private_address with a value like 10.1.2 and then I append on the .8 to get the internal IP.&lt;BR /&gt;&lt;BR /&gt;I need to be able to do this with multiple results from other searches, however. Something like this:&lt;BR /&gt;&lt;BR /&gt;&amp;lt;initial search results that include src_ip and src_port&amp;gt;&lt;BR /&gt;| rex field=src_ip "(?&amp;lt;first3octets&amp;gt;\d{1,3}\.\d{1,3}\.\d{1,3})(?&amp;lt;lastoctet&amp;gt;\.\d{1,3})&lt;BR /&gt;|&amp;nbsp;inputlookup PAT_translation_table append=true where 'public_address'=first3octets&amp;nbsp; 'lower_port'&amp;lt;=src_port&amp;nbsp; 'upper_port'&amp;gt;=src_port&lt;/P&gt;
&lt;P&gt;In this example, inputlookup returns nothing. If I just use the lookup command, I can't use greater than or less than so it returns all the values as an mvfield for private_address, an mvfield for upper_port, and a separate mvfield for lower_port. How would I query that?!&lt;BR /&gt;&lt;BR /&gt;Do any of you have any suggestions how I can do this?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2022 15:10:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-field-values-with-greater-than-and-less-than-to-do-KV/m-p/619996#M215498</guid>
      <dc:creator>md</dc:creator>
      <dc:date>2022-12-12T15:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using field values with greater than and less than to do KV lookups</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-field-values-with-greater-than-and-less-than-to-do-KV/m-p/619999#M215500</link>
      <description>&lt;P&gt;Here's a somewhat kludgy example of how you can do the comparison LT and GT for port range on the MV fields you get from the lookup - most of this is just setup for an example to show MV fields for IPS/ranges.&lt;/P&gt;&lt;P&gt;You need the last 4 lines - note that this will allow for 10 port ranges per local IP in the foreach command&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval last_octet=(random() % 253 + 1)
| eval src_ip=(random() % 253 + 1).".".(random() % 253 + 1).".".(random() % 253 + 1).".".last_octet
| eval src_port=random() % 32768 + 512
| eval n=mvrange(1,11,1) 
| mvexpand n
| eval local_ip="10.1.".n.".".last_octet
| eval low_port=random() % (2048 * n) + 512
| eval high_port=random() % (4096 * n) + low_port
| stats list(local_ip) as local_ip list(low_port) as low_port list(high_port) as high_port by src_ip src_port
| eval ix=-1
| foreach 0 1 2 3 4 5 6 7 8 9 10 [ eval l=tonumber(mvindex(low_port, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)), h=tonumber(mvindex(high_port, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)), ix=if(ix=-1 AND src_port &amp;gt;= l AND src_port &amp;lt;= h, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, ix) ]
| eval local_addr=mvindex(local_ip, ix)
| fields - l h ix&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2022 23:12:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-field-values-with-greater-than-and-less-than-to-do-KV/m-p/619999#M215500</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2022-11-07T23:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using field values with greater than and less than to do KV lookups</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-field-values-with-greater-than-and-less-than-to-do-KV/m-p/623875#M216898</link>
      <description>&lt;P&gt;We ended up using having to use lookup instead of inputlookup.&lt;BR /&gt;&lt;BR /&gt;This is what we came up with:&lt;BR /&gt;&lt;BR /&gt;base search&lt;BR /&gt;| call_to_macro(ip_field, port_field)&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;The macro adds a field named translated_IP to the event with either the PAT translated IP or the original IP if it's not a PAT IP. For the macro:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;eval translated_IP=$ip_field$, port=$port_field$&lt;BR /&gt;| rex field=translated_IP "(?&amp;lt;first3octets&amp;gt;\d{1,3}\.\d{1,3}\.\d{1,3})(?&amp;lt;lastoctet&amp;gt;\.\d{1,3})"&lt;BR /&gt;```This lookup returns all port ranges for that group of the first 3 octets of the address as 3 separate multivalue fields.```&lt;BR /&gt;| lookup PAT_Address_Translation Public_Address AS first3octets OUTPUT Private_Address, Lower_Port, Upper_Port&lt;BR /&gt;```mvzip ties together the values of 2 multivalue fields in the order they appear. We have to nest them since we have 3 multivalue fields.```&lt;BR /&gt;| eval port_range=mvzip(mvzip(Private_Address,Lower_Port),Upper_Port)&lt;BR /&gt;```In cases where the input IP is not in the PAT IP range, we need to make sure port_range is not null or mvexpand errors out.```&lt;BR /&gt;| eval port_range=coalesce(port_range, "")&lt;/P&gt;&lt;P&gt;```mvexpand turns each possible value into it's own row.```&lt;/P&gt;&lt;P&gt;| mvexpand port_range&lt;/P&gt;&lt;P&gt;```we then separate the fields and use a where command to eliminate the values that the port_field isn't within the range of.```&lt;BR /&gt;| rex field=port_range "(?&amp;lt;Private_Address&amp;gt;\d{1,3}\.\d{1,3}\.\d{1,3}),(?&amp;lt;Lower_Port&amp;gt;\d+),(?&amp;lt;Upper_Port&amp;gt;\d+)"&lt;BR /&gt;| where (Lower_Port&amp;lt;=port AND Upper_Port&amp;gt;=port) OR port_range=""&lt;BR /&gt;| eval internal_IP=Private_Address.lastoctet&lt;BR /&gt;```Finally, we make sure translated_IP is either the translated result or the original IP if it's not in the PAT table```&lt;BR /&gt;| eval translated_IP=coalesce(internal_IP,translated_IP)&lt;BR /&gt;```Clean up of added fields```&lt;BR /&gt;| fields - Lower_Port,Upper_Port,Private_Address,first3octets,lastoctet,internal_IP,port_range&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 09 Dec 2022 20:35:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-field-values-with-greater-than-and-less-than-to-do-KV/m-p/623875#M216898</guid>
      <dc:creator>md</dc:creator>
      <dc:date>2022-12-09T20:35:03Z</dc:date>
    </item>
  </channel>
</rss>

