<?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: Splunk Custom Search Commands: How can I write the generator.py script so that it calls the script.py? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/634995#M220602</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/233419"&gt;@PaulPanther&lt;/a&gt;&amp;nbsp;, with the inspired code above it still doesn't work, any help is much appreciated &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 18 Mar 2023 02:19:55 GMT</pubDate>
    <dc:creator>siu</dc:creator>
    <dc:date>2023-03-18T02:19:55Z</dc:date>
    <item>
      <title>Splunk Custom Search Commands: How can I write the generator.py script so that it calls the script.py?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/631206#M219240</link>
      <description>&lt;P&gt;Hi everyone,&lt;BR /&gt;&lt;BR /&gt;I have a script.py which requires one argument to run normally, for eg. script.py D:\Downloads\12-Dec-2022\1234\&lt;BR /&gt;&lt;BR /&gt;I am intending to create a custom search command so that I can have a Splunk Dashboard GUI which allows the user to input the file path i.e&amp;nbsp;D:\Downloads\12-Dec-2022\1234\ and then it will run in the backend this --&amp;gt;&amp;nbsp;script.py D:\Downloads\12-Dec-2022\1234\ and generate a csv file in which I will use the splunk search command to format the data.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;My question would be how can I write the generator.py script so that it calls the script.py&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I have a template I found:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;#!/usr/bin/env python

import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
from splunklib.searchcommands import \
    dispatch, GeneratingCommand, Configuration, Option, validators

@Configuration()
class %(command.title())Command(GeneratingCommand):
    """ %(synopsis)

    ##Syntax

    %(syntax)

    ##Description

    %(description)

    """
    def generate(self):
       # Put your event  code here

       # To connect with Splunk, use the instantiated service object which is created using the server-uri and
       # other meta details and can be accessed as shown below
       # Example:-
       #    service = self.service

       pass

dispatch(%(command.title())Command, sys.argv, sys.stdin, sys.stdout, __name__)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I am not sure like how to write it such that this command will accept an argument (eg. file path inputted by the user)&lt;BR /&gt;&lt;BR /&gt;So how I Forsee it is I have 3 things&lt;/P&gt;
&lt;P&gt;1. Custom search command named mycommand&lt;BR /&gt;2. my own script.py which accepts one argument (a file path) use to run and generate stats&amp;nbsp;&lt;BR /&gt;3. Splunk search command&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;So once I have the custom search command mycommand&lt;BR /&gt;&lt;BR /&gt;I can use it in splunk search&amp;nbsp;&lt;BR /&gt;| mycommand &amp;lt;user input&amp;gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;something like that..however writing the custom search command am not sure how to make it accept an argument inputted for the user in the splunk gui. can anyone help please?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2023 23:14:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/631206#M219240</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-02-16T23:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Custom Search Commands: How can I write the generator.py script so that it calls the script.py?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/631521#M219341</link>
      <description>&lt;P&gt;Just declare the field in the class like&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;filename = Option(require=True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then assign the user input as a variable to a new variable e.g.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;filename = self.filename&lt;/LI-CODE&gt;&lt;P&gt;sample code:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;#!/usr/bin/env python

import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
from splunklib.searchcommands import \
    dispatch, GeneratingCommand, Configuration, Option, validators

@Configuration()
class %(command.title())Command(GeneratingCommand):
    filename = Option(require=True)

    def generate(self):
       filename = self.filename
       # Put your event  code here

       # To connect with Splunk, use the instantiated service object which is created using the server-uri and
       # other meta details and can be accessed as shown below
       # Example:-
       #    service = self.service

       pass

dispatch(%(command.title())Command, sys.argv, sys.stdin, sys.stdout, __name__)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2023 08:30:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/631521#M219341</guid>
      <dc:creator>PaulPanther</dc:creator>
      <dc:date>2023-02-20T08:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Custom Search Commands: How can I write the generator.py script so that it calls the script.py?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/631861#M219473</link>
      <description>&lt;LI-CODE lang="markup"&gt;# Import the necessary modules
import splunklib.searchcommands as searchcommands
import subprocess

# Define a custom search command class
class MyCustomCommand(searchcommands.GeneratingCommand):

    # Define any options for your custom command
    filepath = searchcommands.Option(
        doc='''
        **Syntax:** **filepath=***&amp;lt;filepath&amp;gt;*
        **Description:** Path to the file to be processed by the Python script''',
        require=True, validate=validators.Fieldname())

    # Define the fields that your command will output
    @staticmethod
    def output_schema():
        return {'_time': searchcommands.DateTimeField(), 'output_field_1': searchcommands.StringField()}

    # Define the logic for your custom command
    def generate(self):
        # Call the Python script with the specified filepath as an argument
        result = subprocess.run(["python", "path/to/your/script.py", self.filepath], stdout=subprocess.PIPE)

        # Parse the output of the Python script
        output = result.stdout.decode().strip()

        # Create a new output record with the output of the Python script and the current timestamp
        output_record = {'_time': self._time, 'output_field_1': output}

        # Yield the output record to Splunk
        yield output_record

# Register the custom command with Splunk
dispatch(MyCustomCommand, sys.argv, sys.stdin, sys.stdout, __name__)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Thanks for your reply&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/233419"&gt;@PaulPanther&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;From your code I inspired to generate this code above&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 15:22:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/631861#M219473</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-02-22T15:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Custom Search Commands: How can I write the generator.py script so that it calls the script.py?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/634995#M220602</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/233419"&gt;@PaulPanther&lt;/a&gt;&amp;nbsp;, with the inspired code above it still doesn't work, any help is much appreciated &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2023 02:19:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/634995#M220602</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-03-18T02:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Custom Search Commands: How can I write the generator.py script so that it calls the script.py?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/636117#M220950</link>
      <description>&lt;P&gt;is there anything else to change/add?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Mar 2023 09:20:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/636117#M220950</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-03-27T09:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Custom Search Commands: How can I write the generator.py script so that it calls the script.py?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/641750#M222306</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/233419"&gt;@PaulPanther&lt;/a&gt;&amp;nbsp; any updates?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 12:09:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-Custom-Search-Commands-How-can-I-write-the-generator-py/m-p/641750#M222306</guid>
      <dc:creator>siu</dc:creator>
      <dc:date>2023-04-28T12:09:02Z</dc:date>
    </item>
  </channel>
</rss>

