<?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: python SDK parse xml output in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31542#M1328</link>
    <description>&lt;P&gt;You can change the output mode to csv which will parse it for you.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.client as client
import splunklib.results as results
import csv
service = client.connect(
 host=HOST,
 port=PORT,
 username=un,
 password=pwd
query= """search {+enter your query here}​"""

results_kwargs = {
 "earliest_time": "-30min",
# or "earliest_time": datetime.datetime(2015, 6, 29).isoformat()
 "latest_time": "now",
 "search_mode": "normal",
 "output_mode": "csv"
}
oneshotsearch_results = service.jobs.oneshot(query, **results_kwargs)
f=open('myresults.csv', 'w')
f.write(oneshotsearch_results.read())
f.close()​
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 10 Aug 2018 21:50:16 GMT</pubDate>
    <dc:creator>to914868</dc:creator>
    <dc:date>2018-08-10T21:50:16Z</dc:date>
    <item>
      <title>python SDK parse xml output</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31538#M1324</link>
      <description>&lt;P&gt;I am attempting to parse the results from a search using the python sdk.&lt;BR /&gt;
I can successfully run a search against splunk and return the results via xml output.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!c:/Python26/python.exe -u
import splunk.client as client
import sys, datetime
from pprint import pprint
HOST = "1.2.3.4"
PORT = 8090
USERNAME = "admin"
PASSWORD = "aaaaaaaa"

service = client.connect(
   host=HOST,
   port=PORT,
   username=USERNAME,
   password=PASSWORD)
oldtime = datetime.datetime.now()
search = 'search  index="firewall_rules" table=rule disable=no | dedup name,host |table    host,name,rulegroup,appcode,_time | head 2' 
job = service.jobs.create(search, exec_mode="blocking")
#job = service.jobs.create(search)

while True: 
  stats = job.read('isDone') 
  if stats['isDone'] == str(1): 
    break 
content = str(job.results(output_mode='xml'))
print "Output: %s" % content

Output: &amp;lt;?xml version='1.0' encoding='UTF-8'?&amp;gt;
&amp;lt;results preview='0'&amp;gt;
&amp;lt;meta&amp;gt;
&amp;lt;fieldOrder&amp;gt;
&amp;lt;field&amp;gt;host&amp;lt;/field&amp;gt;
&amp;lt;field&amp;gt;name&amp;lt;/field&amp;gt;
&amp;lt;field&amp;gt;rulegroup&amp;lt;/field&amp;gt;
&amp;lt;field&amp;gt;appcode&amp;lt;/field&amp;gt;
&amp;lt;field&amp;gt;_time&amp;lt;/field&amp;gt;
&amp;lt;/fieldOrder&amp;gt;
&amp;lt;/meta&amp;gt;
    &amp;lt;result offset='0'&amp;gt;
            &amp;lt;field k='host'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;FW1&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
            &amp;lt;field k='name'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;&amp;amp;apos;Deny All&amp;amp;apos;&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
            &amp;lt;field k='rulegroup'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;&amp;amp;apos;&amp;amp;apos;&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
            &amp;lt;field k='_time'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;2011-12-03T03:24:02.000-05:00&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
    &amp;lt;/result&amp;gt;
    &amp;lt;result offset='1'&amp;gt;
            &amp;lt;field k='host'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;FW2&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
            &amp;lt;field k='name'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;WEB1&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
            &amp;lt;field k='rulegroup'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;WEB1&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
            &amp;lt;field k='appcode'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;ABC123&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
            &amp;lt;field k='_time'&amp;gt;
                    &amp;lt;value&amp;gt;&amp;lt;text&amp;gt;2011-12-03T03:24:02.000-05:00&amp;lt;/text&amp;gt;&amp;lt;/value&amp;gt;
            &amp;lt;/field&amp;gt;
    &amp;lt;/result&amp;gt;
&amp;lt;/results&amp;gt;
Elapsed Time: 0:00:04.500000
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have attempted to parse the xml output into a structure that I can use to insert into another system (mysql).  I have tried xmlminidom and etree without success, is there an easier way to access the values with python?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2011 17:58:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31538#M1324</guid>
      <dc:creator>EricPartington</dc:creator>
      <dc:date>2011-12-08T17:58:51Z</dc:date>
    </item>
    <item>
      <title>Re: python SDK parse xml output</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31539#M1325</link>
      <description>&lt;P&gt;There is an example here: &lt;BR /&gt;
&lt;A href="http://wiki.splunk.com/Dev:PySearch"&gt;http://wiki.splunk.com/Dev:PySearch&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;
You might find useful using xmlDoc&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2011 13:51:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31539#M1325</guid>
      <dc:creator>rroberts</dc:creator>
      <dc:date>2011-12-14T13:51:42Z</dc:date>
    </item>
    <item>
      <title>Re: python SDK parse xml output</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31540#M1326</link>
      <description>&lt;P&gt;Here is my latest attempt at using the sdk, python.. SplunkDev helped out a great deal with this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!c:/Python26/python.exe -u
import splunk.client as client
import splunk.results as results
import sys, datetime
from pprint import pprint

HOST = "localhost"
PORT = 8090
USERNAME = "admin"
PASSWORD = "abc123"

service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)
# ----------------------------------------
oldtime = datetime.datetime.now()

search = 'search  index="sidewinder_rules" table=rule disable=no earliest=-8d| dedup name,cluster |table cluster,name,rulegroup,sourcetype,appcode,_time' 

job = service.jobs.create(search, exec_mode="blocking", max_count=5000)
job_results = job.results(count=0, output_mode="xml")
reader = results.ResultsReader(job_results)

num_results = 0;
for kind, result in reader:
    if kind == results.RESULT:
        host = result.get("cluster", 0)
        rulegroup = result.get("rulegroup", 0)
        name = result.get("name", 0)
        sourcetype = result.get("sourcetype", 0)
        appcode = result.get("appcode", 0)
        pprint(host)

newtime = datetime.datetime.now()
print "Elapsed Time: %s" % (newtime - oldtime)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Still have to make some improvements as suggested by splunkDev but it functions for my needs.&lt;BR /&gt;
It also gets more than the default 100 rows when returning results (count=0) and sets a limit of 5000 rows.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2011 14:08:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31540#M1326</guid>
      <dc:creator>EricPartington</dc:creator>
      <dc:date>2011-12-14T14:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: python SDK parse xml output</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31541#M1327</link>
      <description>&lt;P&gt;Great!   &lt;A href="http://dev.splunk.com"&gt;http://dev.splunk.com&lt;/A&gt;  is cool.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2011 14:12:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31541#M1327</guid>
      <dc:creator>rroberts</dc:creator>
      <dc:date>2011-12-14T14:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: python SDK parse xml output</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31542#M1328</link>
      <description>&lt;P&gt;You can change the output mode to csv which will parse it for you.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.client as client
import splunklib.results as results
import csv
service = client.connect(
 host=HOST,
 port=PORT,
 username=un,
 password=pwd
query= """search {+enter your query here}​"""

results_kwargs = {
 "earliest_time": "-30min",
# or "earliest_time": datetime.datetime(2015, 6, 29).isoformat()
 "latest_time": "now",
 "search_mode": "normal",
 "output_mode": "csv"
}
oneshotsearch_results = service.jobs.oneshot(query, **results_kwargs)
f=open('myresults.csv', 'w')
f.write(oneshotsearch_results.read())
f.close()​
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Aug 2018 21:50:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/python-SDK-parse-xml-output/m-p/31542#M1328</guid>
      <dc:creator>to914868</dc:creator>
      <dc:date>2018-08-10T21:50:16Z</dc:date>
    </item>
  </channel>
</rss>

