Splunk Dev

In Splunk SDK, why is my query causing the following error: "Unknown Command Index Error"

qzhang25
New Member

Hi Guys,

I am using Splunk SDK to export a query to get the following data:

import time
import splunklib.client as client
import splunklib.results as results
import json
import requests
from dateutil.parser import parse

HOST = "xxxx"
PORT = xxxx
USERNAME = "xxxx"
PASSWORD = "xxxx"

service = client.connect(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD)

query='index=*    earliest=-5m        | regex "(?i)\D(?:0[1-9]|[12]\d|3[01])([\/.-])(?:0[1-9]|1[1-9])\1(?:19)\d\d|(?:19)\d\d([\/.-])(?:0[1-9]|[12]\d|3[01])([\/.-])(?:0[1-9]|[12]\d|3[01])\D" | rex "(?i)(?(19\d\d([\/.-])\d\d([\/.-])\d{1,2})|(\d\d([\/.-])\d{1,2}([\/.-])19\d\d))"|search dob=* AND dob!="1969-12-31"| table dob _raw index host source| stats first(dob) first(_raw) by index host source' 

while True:

    rr = results.ResultsReader(service.jobs.export(query))
    for result in rr:
        print(result)
0 Karma

bjoernjensen
Contributor

Hey,

you have to start the query with a regular spl command: search index=...

In the UI this is automatically inserted (but not shown).

All the best,
Björn

Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...