<?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: Why is Splunk Python SDK export not using field extraction? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205609#M59783</link>
    <description>&lt;P&gt;try your search with&lt;BR /&gt;&lt;BR /&gt;
    kwargs_export = {&lt;BR /&gt;
         "output_mode": "csv"&lt;BR /&gt;
     }&lt;/P&gt;

&lt;P&gt;It should give you all fields as a dict.&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 20:27:38 GMT</pubDate>
    <dc:creator>evuk</dc:creator>
    <dc:date>2020-09-29T20:27:38Z</dc:date>
    <item>
      <title>Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205602#M59776</link>
      <description>&lt;P&gt;Hi there, &lt;/P&gt;

&lt;P&gt;I am trying to use the Python Splunk-SDK to query results from a search, and return a specific field that i would generally see when i run the search in Splunk Web. For some reason it doesn't look like my fields are being separated when i run the search using the SDK, it only returns the &lt;CODE&gt;_raw&lt;/CODE&gt; message and other fields from Splunk. Is there a way to return fields with &lt;CODE&gt;service.jobs.export()&lt;/CODE&gt; that are normally extracted with the field extractors in Splunk Web? Here is the field extractor for the &lt;CODE&gt;_raw&lt;/CODE&gt; message that  : &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;suricata\[(?P&amp;lt;pid&amp;gt;\d+)\]: \[(?P&amp;lt;gid&amp;gt;\d+):(?P&amp;lt;sid&amp;gt;\d+):(?P&amp;lt;rev&amp;gt;\d+)\] (?P&amp;lt;msg&amp;gt;.+) \[Classification:(?P&amp;lt;classification&amp;gt;.+)\] \[Priority:(?P&amp;lt;priority&amp;gt; \d+)\] \{(?P&amp;lt;proto&amp;gt;.+)\} (?P&amp;lt;src_ip&amp;gt;[\d.]{7,15}):(?P&amp;lt;src_port&amp;gt;\d+) -&amp;gt; (?P&amp;lt;dst_ip&amp;gt;[\d.]{7,15}):(?P&amp;lt;dst_port&amp;gt;\d+)$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.client as client
import splunklib.results as results
import prettyprint
import time
import json

HOST = 'secrets'
PORT = 8089
USERNAME = 'moresecrets'
PASSWORD = 'evenmoresecrets'

service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

kwargs_export = {
    "search_mode": "normal",
    "preview": False,
}
searchquery = "search index=main sourcetype=suricata earliest= -1m"
resultexport = service.jobs.export(searchquery, **kwargs_export )

for i in results.ResultsReader(resultexport):
    time.sleep(5)
    if isinstance(i, dict) and 'ET' in i['_raw'] :
        prettyprint.pp(i)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;results : &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
    "_bkt": "main~7~343A73CE-58CA-4278-9DAC-CDF498B5BBF1", 
    "_cd": "7:4405902", 
    "_indextime": "1482452940", 
    "_raw": "Dec 22 17:28:56 2.2.2.2 Dec 22 17:28:56 myserver suricata[24186]: [1:2006402:10] ET POLICY Incoming Basic Auth Base64 HTTP Password detected unencrypted [Classification: Potential Corporate Privacy Violation] [Priority: 1] {TCP} 4.5.6.7:40247 -&amp;gt; 1.2.3.4:80", 
    "_serial": "28", 
    "_si": [
        "secrets.server", 
        "main"
    ], 
    "_sourcetype": "suricata", 
    "_time": "2016-12-22 17:28:56.000 MST", 
    "host": "x.x.x.x", 
    "index": "main", 
    "linecount": "1", 
    "source": "udp:514", 
    "sourcetype": "suricata", 
    "splunk_server": "secrets"
}
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Dec 2016 00:46:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205602#M59776</guid>
      <dc:creator>zhatsispgx</dc:creator>
      <dc:date>2016-12-23T00:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205603#M59777</link>
      <description>&lt;P&gt;The Splunk documentation for &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.5.1/RESTTUT/RESTsearches"&gt;REST searches&lt;/A&gt; implies that the rf parameter can do this:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;"Use the rf parameter to add required&lt;BR /&gt;
fields to a search. Adding fields&lt;BR /&gt;
guarantees results for the endpoints&lt;BR /&gt;
that return events and a summary. (The&lt;BR /&gt;
required_fields parameter has been&lt;BR /&gt;
deprecated in favor of the rf&lt;BR /&gt;
parameter.)"&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Older Splunk answers posts advise to use the fields command in the search to ensure the required fields are returned...&lt;/P&gt;</description>
      <pubDate>Sun, 25 Dec 2016 11:38:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205603#M59777</guid>
      <dc:creator>gjanders</dc:creator>
      <dc:date>2016-12-25T11:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205604#M59778</link>
      <description>&lt;P&gt;I added the RF Parameter in &lt;CODE&gt;**kwargs_export&lt;/CODE&gt; and it still didn't work. i'm not sure if this is a bug or what&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2017 22:39:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205604#M59778</guid>
      <dc:creator>zhatsispgx</dc:creator>
      <dc:date>2017-01-06T22:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205605#M59779</link>
      <description>&lt;P&gt;I have the same problem as @zhatsispgx...&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2018 15:40:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205605#M59779</guid>
      <dc:creator>burtica</dc:creator>
      <dc:date>2018-01-12T15:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205606#M59780</link>
      <description>&lt;P&gt;Hi @zhatsispgx &amp;amp; @burtica &lt;/P&gt;

&lt;P&gt;Have you tried by adding &lt;CODE&gt;| table *&lt;/CODE&gt; in search in python code?&lt;/P&gt;

&lt;P&gt;Like for example code in question.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;searchquery = "search index=main sourcetype=suricata earliest= -1m | table * "
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2018 15:54:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205606#M59780</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2018-01-12T15:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205607#M59781</link>
      <description>&lt;P&gt;I have the same issue.  Tried adding "| table *" but then I get job["eventFieldCount"] of 0.  Before I added rf the count was 11.  After adding rf the count was 16 but the fields in rf were not in the results.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 18:40:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205607#M59781</guid>
      <dc:creator>slookingbill</dc:creator>
      <dc:date>2018-07-03T18:40:49Z</dc:date>
    </item>
    <item>
      <title>Re: Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205608#M59782</link>
      <description>&lt;P&gt;I was successful in getting results by using the following code, but let me know if it's not working for you:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;from splunklib.binding import connect
from splunklib.modularinput.utils import parse_parameters
from splunklib import client, results


def cleanup_tail(options):
    """ cleanup the tail of a recovery """

    if options['format'] == "csv":
        options['fd'].write("\n")
    elif options['format'] == "xml":
        options['fd'].write("\n&amp;lt;/results&amp;gt;\n")
    else:
        options['fd'].write("\n]\n")


def export(options, service, index):
    """ main export method: export any number of indexes """
    start = options['start']
    end = options['end']
    fixtail = options['fixtail']
    once = True

    squery = "search"
    squery = squery + "index=%s" % options['index']
    if (start != ""):
        squery = squery + " earliest_time=%s" % start
    if (end != ""):
        squery = squery + " latest_time=%s" % end

    print(squery)
    success = False

    while not success:
        # issue query to splunkd
        # count=0 overrides the maximum number of events
        # returned (normally 50K) regardless of what the .conf
        # file for splunkd says. 
        result = service.get('search/jobs/export', 
                             search=squery, 
                             output_mode=options['format'],
                             timeout=60,
                             earliest_time="0.000",
                             time_format="%s.%Q",
                             count=0)
        print(result.status)
        if result.status != 200:
            print("warning: export job failed: %d, sleep/retry" % result.status)
            time.sleep(60)
        else:
            success = True

    # write export file 
    while True:
        if fixtail and once:
            cleanup_tail(options)
            once = False
        content = result.body.read()
        if len(content) == 0: break
        options['fd'].write(content)
        options['fd'].write("\n".encode("utf-8"))

    options['fd'].flush()

options = {"host"    : None,
               "port"    : None,
               "username": None,
               "password": None,
               "format":"csv",
               "fixtail":False}
service = connect(**options)
export(options, service, index)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Jul 2018 18:08:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205608#M59782</guid>
      <dc:creator>evuk</dc:creator>
      <dc:date>2018-07-16T18:08:49Z</dc:date>
    </item>
    <item>
      <title>Re: Why is Splunk Python SDK export not using field extraction?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205609#M59783</link>
      <description>&lt;P&gt;try your search with&lt;BR /&gt;&lt;BR /&gt;
    kwargs_export = {&lt;BR /&gt;
         "output_mode": "csv"&lt;BR /&gt;
     }&lt;/P&gt;

&lt;P&gt;It should give you all fields as a dict.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 20:27:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-Splunk-Python-SDK-export-not-using-field-extraction/m-p/205609#M59783</guid>
      <dc:creator>evuk</dc:creator>
      <dc:date>2020-09-29T20:27:38Z</dc:date>
    </item>
  </channel>
</rss>

