<?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: runshellscript search command does not return the results file ($8 argument) in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334328#M160858</link>
    <description>&lt;P&gt;From my (unaccepted) answer here, quoted verbatim:&lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answers/41949/passing-search-results-to-external-python-script.html"&gt;https://answers.splunk.com/answers/41949/passing-search-results-to-external-python-script.html&lt;/A&gt;&lt;/P&gt;

&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>Fri, 02 Jun 2017 14:28:35 GMT</pubDate>
    <dc:creator>woodcock</dc:creator>
    <dc:date>2017-06-02T14:28:35Z</dc:date>
    <item>
      <title>runshellscript search command does not return the results file ($8 argument)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334326#M160856</link>
      <description>&lt;P&gt;This &lt;A href="https://docs.splunk.com/Documentation/Splunk/6.6.1/SearchReference/Runshellscript"&gt;docs&lt;/A&gt; (&lt;A href="https://docs.splunk.com/Documentation/SplunkCloud/6.6.0/SearchReference/Runshellscript"&gt;https://docs.splunk.com/Documentation/SplunkCloud/6.6.0/SearchReference/Runshellscript&lt;/A&gt;) says $8 argument is the path where the search result file will be saved however after running the search command:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;* | head 1 | runshellscript test.py [ search * | stats count | return count ] 2 3 4 5 6 7 /var/www
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;does not create a results.csv&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 13:39:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334326#M160856</guid>
      <dc:creator>testadrianbelen</dc:creator>
      <dc:date>2017-06-02T13:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: runshellscript search command does not return the results file ($8 argument)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334327#M160857</link>
      <description>&lt;P&gt;Runshellscript is not supported as a search command.  I honestly have no idea what it is for.&lt;/P&gt;

&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/SplunkCloud/6.6.0/SearchReference/Runshellscript"&gt;https://docs.splunk.com/Documentation/SplunkCloud/6.6.0/SearchReference/Runshellscript&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Description&lt;BR /&gt;
For Splunk Enterprise deployments, executes scripted alerts. &lt;STRONG&gt;This command is not supported as a search command.&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;Instead you would save a search such as &lt;CODE&gt;* | head 1&lt;/CODE&gt;, and in trigger actions you would select "run a script" then you'd type in the name of your script that is in the bin folder of the app that the search is in.&lt;/P&gt;

&lt;P&gt;Then i your script you be able to use $7, or $ARGSV[7] to find the path where splunk wrote the results.csv.  &lt;/P&gt;

&lt;P&gt;csvPath=$7&lt;BR /&gt;
...code to read / manipulate data found in csvPath...&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 13:51:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334327#M160857</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-06-02T13:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: runshellscript search command does not return the results file ($8 argument)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334328#M160858</link>
      <description>&lt;P&gt;From my (unaccepted) answer here, quoted verbatim:&lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answers/41949/passing-search-results-to-external-python-script.html"&gt;https://answers.splunk.com/answers/41949/passing-search-results-to-external-python-script.html&lt;/A&gt;&lt;/P&gt;

&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>Fri, 02 Jun 2017 14:28:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334328#M160858</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-06-02T14:28:35Z</dc:date>
    </item>
    <item>
      <title>Re: runshellscript search command does not return the results file ($8 argument)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334329#M160859</link>
      <description>&lt;P&gt;so it is just an environment variable that can be accessed or used in the script, but it can't be used as an argument in a script. &lt;A href="http://docs.splunk.com/Documentation/SplunkCloud/latest/Alert/Configuringscriptedalerts"&gt;http://docs.splunk.com/Documentation/SplunkCloud/latest/Alert/Configuringscriptedalerts&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 23:32:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334329#M160859</guid>
      <dc:creator>testadrianbelen</dc:creator>
      <dc:date>2017-06-02T23:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: runshellscript search command does not return the results file ($8 argument)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334330#M160860</link>
      <description>&lt;P&gt;I typically write custom search commands when I need access to the data in the search pipe.  It's just easier and more versatile.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 23:45:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334330#M160860</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-06-02T23:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: runshellscript search command does not return the results file ($8 argument)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334331#M160861</link>
      <description>&lt;P&gt;Download my ta-webtools and look at the scripts in bin, and how I wore them up using commands.conf.&lt;/P&gt;

&lt;P&gt;If you know Python it will be easy to reuse the code.  If you don't know Python then it will be easier than starting from scratch but you can post the code and ask for help as needed.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 23:47:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/runshellscript-search-command-does-not-return-the-results-file-8/m-p/334331#M160861</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-06-02T23:47:21Z</dc:date>
    </item>
  </channel>
</rss>

