Splunk Dev

API Auth and Search Script

martillo_300
Explorer

Hello Experts, I'm trying to create a python script to run adhoc searches via a api request but the documentation has me opening webpages after webpages. I've created a token already. Can someone please help me with this task? Thank you in advance,Splunk Search

Labels (2)
0 Karma
1 Solution

martillo_300
Explorer

That worked! Thank you so much. This is exactly what I was needing. 

View solution in original post

0 Karma

marnall
Builder

Try modifying this CURL request to your needs (adjust the endpoint, search, and token)

curl -k -H 'Authorization: Splunk <your_token_here>' https://your_searchhead_here:8089/services/search/v2/jobs/export -d search="search index=* | head 10 | table host"

 

0 Karma

martillo_300
Explorer

Thanks. Is there a count that I can limit this to? I makes the call but never comes back with data where I have to kill the process. 

0 Karma

marnall
Builder

The /export endpoint will dispatch a search and then retrieve the results when the search is completed. If the search takes a lot of time, then likely the request will time out. You can either make your search faster or you can use two endpoints, one where you dispatch the search and another endpoint where you later retrieve the results.

To dispatch the search:

curl -k -H 'Authorization: Splunk <your_token_here>' https://your_searchhead_here:8089/services/search/jobs -d search="search index=* | head 10 | table host"

The above call will return you a search id (sid), which you'll need in the following call to retrieve the results:

curl -k -H 'Authorization: Splunk <your_token_here>' https://your_searchhead_here:8089/services/search/<yoursidhere>/results

Ref: https://docs.splunk.com/Documentation/Splunk/latest/RESTTUT/RESTsearches

0 Karma

martillo_300
Explorer

That worked! Thank you so much. This is exactly what I was needing. 

0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...