<?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: Passing search results to external python script in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83886#M21381</link>
    <description>&lt;P&gt;@annalisefolsen , I don't know what you are trying to achieve - but if you look at this example here &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.4.1/Search/Customsearchcommandshape"&gt;http://docs.splunk.com/Documentation/Splunk/6.4.1/Search/Customsearchcommandshape&lt;/A&gt; you will learn how to pass a Splunk search result into a python script, do stuff with the result and return it into Splunk.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jun 2016 20:37:13 GMT</pubDate>
    <dc:creator>MuS</dc:creator>
    <dc:date>2016-06-29T20:37:13Z</dc:date>
    <item>
      <title>Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83878#M21373</link>
      <description>&lt;P&gt;I know it's just my lack of knowledge with Splunk causing me some grief here but...&lt;/P&gt;

&lt;P&gt;I want to pass search results to an external python script.&lt;/P&gt;

&lt;P&gt;Here is my search:&lt;/P&gt;

&lt;P&gt;sourcetype="*WinEventLog:Security" (EventCode=528 OR EventCode=4624) AND Logon_Type=10 | eval event_date = strftime(_time, "%D %T %P") | eval User = if(isnull(Account_Name), User_Name, mvindex(Account_Name,1)) | script python alogin User Source_Network_Address host&lt;/P&gt;

&lt;P&gt;My python script merely sends me an email with what I thought would be the field values I passed (User, Source_Network_Address and host).&lt;/P&gt;

&lt;P&gt;But I'm actually getting the words "User", "Source_Network_Address" and "Host" in the email.&lt;/P&gt;

&lt;P&gt;Of course I tested the script and if I run the script - "python alogin.py nicholas 10.0.10.99 SERVER"  it works and I'm sent an email with the three values passed as expected.&lt;/P&gt;

&lt;P&gt;What am I missing here?&lt;/P&gt;

&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 11:28:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83878#M21373</guid>
      <dc:creator>utpress</dc:creator>
      <dc:date>2020-09-28T11:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83879#M21374</link>
      <description>&lt;P&gt;Had any luck with this?&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2013 13:52:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83879#M21374</guid>
      <dc:creator>dcparker</dc:creator>
      <dc:date>2013-03-20T13:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83880#M21375</link>
      <description>&lt;P&gt;I have a similar requirement of passing search results as arguments to a python script. Has anyone been able to workaround this problem ?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2015 03:26:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83880#M21375</guid>
      <dc:creator>sanurd</dc:creator>
      <dc:date>2015-06-19T03:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83881#M21376</link>
      <description>&lt;P&gt;Hi utpress,&lt;/P&gt;

&lt;P&gt;your script must import &lt;CODE&gt;splunk.Intersplunk&lt;/CODE&gt; and use the following two lines to read results from previous searches:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;myresults,dummyresults,settings = splunk.Intersplunk.getOrganizedResults() # getting search results form Splunk
for r in myresults: # loop the results
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope that helps ...&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2015 04:21:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83881#M21376</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2015-06-19T04:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83882#M21377</link>
      <description>&lt;P&gt;Hi MuS, &lt;/P&gt;

&lt;P&gt;I tried to import Intersplunk, but it can't find the import (the module cannot be found). Is there a special download that you need other than the SDK? Also, if you can direct me to more information on this module that would be wonderful! I have not been able to find any documentation so far. &lt;/P&gt;

&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 13:34:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83882#M21377</guid>
      <dc:creator>annalisefolsen</dc:creator>
      <dc:date>2016-06-20T13:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83883#M21378</link>
      <description>&lt;P&gt;@annalisefolsen,&lt;/P&gt;

&lt;P&gt;Try to run this on your Splunk server (assuming it's linux) :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; /opt/splunk/bin/splunk cmd python -c "import splunk.Intersplunk"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will not fail. Remember to use Splunk's  Python in your script and not the OS one located in &lt;CODE&gt;/usr/bin/&lt;/CODE&gt;.&lt;BR /&gt;
You can find some documentations here &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.4.1/Search/Writeasearchcommand"&gt;http://docs.splunk.com/Documentation/Splunk/6.4.1/Search/Writeasearchcommand&lt;/A&gt; and here &lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEU2"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAEU2&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Hope this helps ...&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 21:42:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83883#M21378</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2016-06-20T21:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83884#M21379</link>
      <description>&lt;P&gt;Thank you. I've run it in Splunk and it is now able to find module. &lt;/P&gt;</description>
      <pubDate>Tue, 21 Jun 2016 15:26:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83884#M21379</guid>
      <dc:creator>annalisefolsen</dc:creator>
      <dc:date>2016-06-21T15:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83885#M21380</link>
      <description>&lt;P&gt;Hi MuS,&lt;/P&gt;

&lt;P&gt;I thought that Intersplunk could change the event data when it is output (and not just display). I have done this and nothing has changed. Is this possible, or do I have to find another way to do this?&lt;/P&gt;

&lt;P&gt;Thanks again for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 19:52:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83885#M21380</guid>
      <dc:creator>annalisefolsen</dc:creator>
      <dc:date>2016-06-29T19:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83886#M21381</link>
      <description>&lt;P&gt;@annalisefolsen , I don't know what you are trying to achieve - but if you look at this example here &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.4.1/Search/Customsearchcommandshape"&gt;http://docs.splunk.com/Documentation/Splunk/6.4.1/Search/Customsearchcommandshape&lt;/A&gt; you will learn how to pass a Splunk search result into a python script, do stuff with the result and return it into Splunk.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 20:37:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83886#M21381</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2016-06-29T20:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83887#M21382</link>
      <description>&lt;P&gt;This isn't the "right" way to do this, but it is the most expedient and it allows you to recycle your traditional alert script for use in regular searches, too.  This example assumes that you will be passing 2 fields to the scirpt: &lt;CODE&gt;scr_ip&lt;/CODE&gt; and &lt;CODE&gt;host&lt;/CODE&gt;; you will need to adjust slightly for the fields that you will be using.&lt;/P&gt;

&lt;P&gt;Insert this code to your MyAlertScript.py code right before your existing code that accesses the &lt;CODE&gt;results.csv.gz&lt;/CODE&gt; file in &lt;CODE&gt;argv[8]&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# In order to facilitate Event Workflow Actions using runshellscript,
# we will hijack the arguments in one special case as follows:
#    1(sys.argv[1]) = '1'
#    2(sys.argv[2]) = '"&amp;lt;src_ip&amp;gt;","&amp;lt;host&amp;gt;"'
#    3(sys.argv[3]) = 'Hack'
#    4(sys.argv[4]) = 'to'
#    5(sys.argv[5]) = 'run'
#    7(sys.argv[6]) = 'from'
#    6(sys.argv[7]) = 'runshellscript'
#    8(sys.argv[8]) = * &amp;lt;- DO NOT CHECK because splunk modifieds this on the way in
# If in this format we will pull the data directly out of the 2nd argument,
# instead of out of the restults file.
specialCase = False # initialize to FALSE
if ((sys.argv[1] == '1') and (sys.argv[3] == 'Hack') and (sys.argv[4] == 'to') and
    (sys.argv[5] == 'run') and (sys.argv[6] == 'from') and
    (sys.argv[7] == 'runshellscript')):
    print "SPECIAL CASE!\n" # Special case!
    specialCase = True # make sure we delete this file at the end!

    fnz = workdir + 'results.csv.gz'
    with gzip.open(fnz, 'wb') as OFH:
       OFH.write('src_ip,host\n')
       OFH.write(sys.argv[2])
    OFH.close()

    sys.argv[8] = fnz
    #print "SPECIAL CASE: ARGV8=&amp;lt;" + sys.argv[8] + "&amp;gt; fnz=&amp;lt;" + fnz + "&amp;gt;\n"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then at the bottom, add this, too:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;if (specialCase): os.remove(sys.argv[8]) # delete fake zip file we made
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Next you need a macro like this (to abstract away the trickery):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[MyScript]
definition = table src_ip host\
| map maxsearches=5000 search="|runshellscript MyAlertScript.py 1 \"\\\"\\\"$src_ip$\\\",\\\"$host$\\\"\\\"\" Hack to run from runshellscript 8"
iseval = 0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, to use it, you just do this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;My Search To Get Events With src_ip And host Here | `MyScript`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;SPECIAL WARNING!  This will not scale nicely if you pass a large number of results to the script because of the &lt;CODE&gt;map&lt;/CODE&gt; command but it works GREAT for small numbers of events.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Feb 2017 22:08:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83887#M21382</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-02-18T22:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83888#M21383</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;

&lt;P&gt;I have a similar requirement and came across below solution from a different post. Check if it helps.&lt;/P&gt;

&lt;P&gt;&lt;A href="https://answers.splunk.com/answers/149836/how-to-run-an-alert-script-on-field-values-generated-in-splunk.html"&gt;https://answers.splunk.com/answers/149836/how-to-run-an-alert-script-on-field-values-generated-in-splunk.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Mar 2018 01:34:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83888#M21383</guid>
      <dc:creator>suryaavinash</dc:creator>
      <dc:date>2018-03-21T01:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83889#M21384</link>
      <description>&lt;P&gt;can you MyAlertScript.py  full code for reference.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 16:56:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83889#M21384</guid>
      <dc:creator>srinivasup</dc:creator>
      <dc:date>2018-07-27T16:56:09Z</dc:date>
    </item>
    <item>
      <title>Re: Passing search results to external python script</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83890#M21385</link>
      <description>&lt;P&gt;created an add-on which will call python script with result file.&lt;/P&gt;

&lt;P&gt;you should have python/shell script in the bin directory to be called by this script with results.&lt;/P&gt;

&lt;P&gt;I will post this add-on to splunk base soon.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 18:29:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Passing-search-results-to-external-python-script/m-p/83890#M21385</guid>
      <dc:creator>thambisetty</dc:creator>
      <dc:date>2018-07-27T18:29:07Z</dc:date>
    </item>
  </channel>
</rss>

