<?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: How to execute external script to manipulate file from search command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94936#M182448</link>
    <description>&lt;P&gt;Hello there,&lt;/P&gt;

&lt;P&gt;There is difference between a &lt;EM&gt;custom command&lt;/EM&gt; and invoking an external script with Splunk. There are also different applications to each one of these items. Personally, I use custom commands to receive search results and manipulate for presentation -where the search language does not meet my requirements or for specific functionality. I run external scripts when I want to pass results of a search to an external action or process.&lt;/P&gt;

&lt;P&gt;Without the context of your intent, here are the mechanics to either case.&lt;/P&gt;

&lt;P&gt;To use a custom command you will need to embed your code around a wrapper. This is explained &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/SearchScripts"&gt;here&lt;/A&gt;.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import sys,splunk.Intersplunk
results = []

try:
   results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()

############### YOUR CODE HERE ##############
   fo=open("c:/test.txt",'w')
   fo.write("hello")
   fo.close()

############### DATA MANIPULATION HERE ##############

except:
   import traceback
   stack =  traceback.format_exc()
   results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack))

splunk.Intersplunk.outputResults( results )
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;On the other hand, if you just want to run an script and pass some variables to it, the process is different. The Splunk Language provides a 'script' command which allows for the call. That is documented &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Script"&gt;here&lt;/A&gt;. You would use this on your search like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="blah" sourcetype="blah"... | script python test.py param1 param2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I hope this helps.&lt;/P&gt;

&lt;P&gt;Happy Splunkin'&lt;/P&gt;</description>
    <pubDate>Thu, 18 Oct 2012 16:29:26 GMT</pubDate>
    <dc:creator>Gilberto_Castil</dc:creator>
    <dc:date>2012-10-18T16:29:26Z</dc:date>
    <item>
      <title>How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94935#M182447</link>
      <description>&lt;P&gt;hello,&lt;/P&gt;

&lt;P&gt;i have a testing python script as:&lt;BR /&gt;
test.py&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  fo=open("c:/test.txt",'w')
  fo.write("hello")
  fo.close()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and put in \etc\apps\Search\bin&lt;/P&gt;

&lt;P&gt;\etc\apps\Search\local\commands.conf is:&lt;/P&gt;

&lt;P&gt;[test]&lt;BR /&gt;
filename= test.py&lt;/P&gt;

&lt;P&gt;I execute in search :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| test
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Error "you have insufficient privileges to perform this action", i tried the other way, but unsuccessful&lt;/P&gt;

&lt;P&gt;Can you show me what i should do to run that script ? (i run with admin role)&lt;/P&gt;</description>
      <pubDate>Thu, 18 Oct 2012 14:04:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94935#M182447</guid>
      <dc:creator>sieutruc</dc:creator>
      <dc:date>2012-10-18T14:04:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94936#M182448</link>
      <description>&lt;P&gt;Hello there,&lt;/P&gt;

&lt;P&gt;There is difference between a &lt;EM&gt;custom command&lt;/EM&gt; and invoking an external script with Splunk. There are also different applications to each one of these items. Personally, I use custom commands to receive search results and manipulate for presentation -where the search language does not meet my requirements or for specific functionality. I run external scripts when I want to pass results of a search to an external action or process.&lt;/P&gt;

&lt;P&gt;Without the context of your intent, here are the mechanics to either case.&lt;/P&gt;

&lt;P&gt;To use a custom command you will need to embed your code around a wrapper. This is explained &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/SearchScripts"&gt;here&lt;/A&gt;.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import sys,splunk.Intersplunk
results = []

try:
   results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()

############### YOUR CODE HERE ##############
   fo=open("c:/test.txt",'w')
   fo.write("hello")
   fo.close()

############### DATA MANIPULATION HERE ##############

except:
   import traceback
   stack =  traceback.format_exc()
   results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack))

splunk.Intersplunk.outputResults( results )
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;On the other hand, if you just want to run an script and pass some variables to it, the process is different. The Splunk Language provides a 'script' command which allows for the call. That is documented &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Script"&gt;here&lt;/A&gt;. You would use this on your search like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="blah" sourcetype="blah"... | script python test.py param1 param2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I hope this helps.&lt;/P&gt;

&lt;P&gt;Happy Splunkin'&lt;/P&gt;</description>
      <pubDate>Thu, 18 Oct 2012 16:29:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94936#M182448</guid>
      <dc:creator>Gilberto_Castil</dc:creator>
      <dc:date>2012-10-18T16:29:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94937#M182449</link>
      <description>&lt;P&gt;it's perfect &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; thank you so much .&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2012 13:05:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94937#M182449</guid>
      <dc:creator>sieutruc</dc:creator>
      <dc:date>2012-10-19T13:05:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94938#M182450</link>
      <description>&lt;P&gt;can i ask one more ? if i want to show on splunk web the message that indicates the status of the execution script , is it running or terminated with succesfull or errorous notification, what function should i use ?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Oct 2012 13:14:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94938#M182450</guid>
      <dc:creator>sieutruc</dc:creator>
      <dc:date>2012-10-19T13:14:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94939#M182451</link>
      <description>&lt;P&gt;Hello and sorry for the late reply. The school of thought is that if you are not looking to manipulate search results with the script, then the two methods above are not the best approach.&lt;/P&gt;

&lt;P&gt;Splunk has a scripted input mechanism which allows you to schedule the execution of a script and the capture the stdout message from the script. The result is a record of the notification obtained by the script and its execution state. That would give you the result you desire.&lt;/P&gt;

&lt;P&gt;If you do not wish to use Splunk for scheduling, dump the script results to a file and then have Splunk index that file.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2012 13:30:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94939#M182451</guid>
      <dc:creator>Gilberto_Castil</dc:creator>
      <dc:date>2012-10-24T13:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94940#M182452</link>
      <description>&lt;P&gt;This Link&lt;BR /&gt;
"To use a custom command you will need to embed your code around a wrapper. This is explained here."&lt;BR /&gt;
is no longer available can you please let me know where can I find this info.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Feb 2013 12:37:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94940#M182452</guid>
      <dc:creator>harshjets</dc:creator>
      <dc:date>2013-02-14T12:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94941#M182453</link>
      <description>&lt;P&gt;Hello harshjets, I have upated the link.&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/SearchScripts"&gt;http://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/SearchScripts&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2013 20:14:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94941#M182453</guid>
      <dc:creator>Gilberto_Castil</dc:creator>
      <dc:date>2013-03-07T20:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute external script to manipulate file from search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94942#M182454</link>
      <description>&lt;P&gt;I have splunk script below which takes input can you help how to use splunk intersplunk for this script to take input and provide output&lt;/P&gt;

&lt;P&gt;def base36encode(number):&lt;BR /&gt;
    if not isinstance(number, (int, long)):&lt;BR /&gt;
        raise TypeError('number must be an integer')&lt;BR /&gt;
    if number &amp;lt; 0:&lt;BR /&gt;
        raise ValueError('number must be positive')&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;alphabet, base36 = ['0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '']

while number:
    number, i = divmod(number, 36)
    base36 = alphabet[i] + base36

return base36 or alphabet[0]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;def base36decode(number):&lt;BR /&gt;
    return int(number, 36)&lt;/P&gt;

&lt;P&gt;print(base36encode(1412823931503067241))&lt;BR /&gt;
print(base36decode('AQF8AA0006EH'))&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 21:34:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-execute-external-script-to-manipulate-file-from-search/m-p/94942#M182454</guid>
      <dc:creator>shaileshmali</dc:creator>
      <dc:date>2016-06-23T21:34:27Z</dc:date>
    </item>
  </channel>
</rss>

