Getting Data In
Highlighted

How to pass value to python script from a Input.conf

Builder

i need to pass the host value in the URL from external file to the python script. how to pass it through conf file? please help

    import requests
    headers={
            "accept": "application/json",
            "content-type": "application/json"
        }
    res = requests.get('https://"<passvalue>"/home/method=post/end',headers=headers )

    print(res.text)


inputs.conf
[script://.\bin\gettinfdata.py]
interval = * * * * *
sourcetype = pythondata
disabled = False 
index = test
0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

Motivator

where is the external file located? Does the python script have an access to this file? You have to build all logic in the python script itself, the script stanza in inputs.conf doesn't accept any parameters, only the path to the script.

Other option is to use the .path file where you can use parameters:

cmd can also be a path to a file that ends with a ".path" suffix. A file
  with this suffix is a special type of pointer file that points to a command
  to be run. Although the pointer file is bound by the same location
  restrictions mentioned above, the command referenced inside it can reside
  anywhere on the file system. The .path file must contain exactly one line:
  the path to the command to run, optionally followed by command-line
  arguments. The file can contain additional empty lines and lines that begin
  with '#'. The input ignores these lines.

you can also use a wrapper script that first extract a host value from the file and pass it as parameter to the script

Highlighted

Re: How to pass value to python script from a Input.conf

Builder

@PavelP i tried using .path file to pass arguments, still it doesnt work, could you please check whether the stanza.

$SPLUNK_HOME\etc\apps\apps\bin\sample.py 'host1'

0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

Motivator

@premranjithj : are you sure the your sample.py script accept arguments? Hier is an example what I have in my .path file and it works:

/usr/sbin/tcpdump -pnns0 -i eth0 -tttt port 53

here is my inputs.conf:

[script://./bin/tcpdump.path]
interval = -1
disabled = 0 
sourcetype = port53tttt

here is my tcpdump.path file ($SPLUNKHOME/etc/apps/TAtcpdump/bin/tcpdump.path):

/usr/sbin/tcpdump -pnns0 -i eth0 -tttt port 53
0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

Ultra Champion
0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

Builder

@kamlesh_vaghela can you please help me here?

0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

SplunkTrust
SplunkTrust

@premranjithj

When dynamic parameter comes, I aways suggest Modular input over scripted input. It has its own features.

https://docs.splunk.com/Documentation/Splunk/6.4.3/AdvancedDev/ModInputsIntro

Input scripted input did you tried passing an argument in the script?

 [script://.\bin\gettinfdata.py HostName]
0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

Builder

@kamlesh_vaghela it worked.thanks

0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

SplunkTrust
SplunkTrust

Glad to help you @premranjithj 🙂

0 Karma
Highlighted

Re: How to pass value to python script from a Input.conf

Motivator

Hello @premranjithj

here is an example based on your original script.

Modify your script to:

import requests
import sys

domain = sys.argv[1]

# here you need to add some input validation checks!!! DO NOT SKIP THIS STEP!!

headers={
         "accept": "application/json",
         "content-type": "application/json"
}

res = requests.get('http://' + domain ,headers=headers )

print(res.text)

here is your inputs.conf:

[script://.\bin\gettinfdata.path]
interval = * * * * *
sourcetype = pythondata
disabled = False 
index = test

here is your gettinfdata.path:
.\bin\gettinfdata.path example.com

But if I understand you right you need to pass the host value in the URL from external file to the python script.

So instead I'd just modify your .py script and add a code part to read from an external file.

Please describe what is your use case and where is the external file located and what is its content.

0 Karma
Speak Up for Splunk Careers!

We want to better understand the impact Splunk experience and expertise has has on individuals' careers, and help highlight the growing demand for Splunk skills.