<?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: alert to remotely run a script in Alerting</title>
    <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281582#M5182</link>
    <description>&lt;P&gt;there's a way to log it through splunkd.log  or your own, etc.  for more details than even Splunk docs offer, just see python docs for logging.  &lt;A href="https://docs.python.org/2/library/logging.html"&gt;https://docs.python.org/2/library/logging.html&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Dec 2015 02:18:21 GMT</pubDate>
    <dc:creator>jkat54</dc:creator>
    <dc:date>2015-12-17T02:18:21Z</dc:date>
    <item>
      <title>alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281576#M5176</link>
      <description>&lt;P&gt;I have created a python script in order to ssh to a remote machine and run a script on that machine to unlock user accounts. &lt;BR /&gt;
I am only getting an &lt;CODE&gt;return code 255&lt;/CODE&gt;. &lt;/P&gt;

&lt;P&gt;I have eliminated all 'splunk code' from the script, and the python script by itself works just fine when ran, and unlocks the account on the remote machine. I am wondering what I am doing wrong. &lt;/P&gt;

&lt;P&gt;I have also copied the appropriate .ssh key to the remote machines in order to remove authentication when ssh to the remote machine. &lt;/P&gt;

&lt;P&gt;This is my script: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;'''

import sys
import splunk.Intersplunk
import subprocess
import logging
import os

# System logging
logger = logging.getLogger('testssh')
hdlr = logging.FileHandler('/tmp/testssh.txt')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

try:

    keywords,options = splunk.Intersplunk.getKeywordsAndOptions() # Get all the options passed

    # Check for passed parameters
    if not options.has_key('host'):
        splunk.Intersplunk.generateErrorResults("no host specified")
        exit(0)
    if not options.has_key('user'):
        splunk.Intersplunk.generateErrorResults("no user specified")
        exit(0)
   if not options.has_key('command'):
        splunk.Intersplunk.generateErrorResults("no command specified")
        exit(0)

    command = options.get('command', None)
    host = options.get('host', None)
    user = options.get('user', None)

    results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()
    for r in results:
        try:
            # Call the script passing all the necessary arguments
            p = subprocess.Popen(["ssh -i /idn/home/tmarlett/.ssh/id_rsa -q -t -t tmarlett@r[host] r[command]"],stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
            # Get the output from script and push it to new output column
            out,err = p.communicate()
            #logger.info(out.decode("utf-8"))
            r["output"]= out.decode("utf-8")
            r["error"]=err
            r["return_code"]=p.returncode;
        except ValueError, e:
            results = splunk.Intersplunk.generateErrorResults(str(e))
        except OSError, e:
            results = splunk.Intersplunk.generateErrorResults(str(e))

    #Output results back to Splunk
    splunk.Intersplunk.outputResults(results)

except Exception, e:
    results = splunk.Intersplunk.generateErrorResults(str(e))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And this is the output it shows me when running the script:&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/901i8FE0DA2E2515FF12/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Does anyone have any insight as to why this would be happening? &lt;/P&gt;</description>
      <pubDate>Wed, 16 Dec 2015 22:50:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281576#M5176</guid>
      <dc:creator>tmarlette</dc:creator>
      <dc:date>2015-12-16T22:50:21Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281577#M5177</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;Your script needs a shebang&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; #!/usr/local/python
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Dec 2015 01:33:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281577#M5177</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2015-12-17T01:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281578#M5178</link>
      <description>&lt;PRE&gt;&lt;CODE&gt; which python
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;To get the proper path&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 01:34:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281578#M5178</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2015-12-17T01:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281579#M5179</link>
      <description>&lt;P&gt;You'll also want to look at other apps on splunkbase to see how they implement the built in "splunk" python logger....&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; _logger....
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Dec 2015 01:37:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281579#M5179</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2015-12-17T01:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281580#M5180</link>
      <description>&lt;P&gt;Error 255 could coming from the subprocess too because subprocess returns 0-255.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 01:39:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281580#M5180</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2015-12-17T01:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281581#M5181</link>
      <description>&lt;P&gt;Or read the docs &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.1/AdvancedDev/ModInputsLog"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.1/AdvancedDev/ModInputsLog&lt;/A&gt; on &lt;CODE&gt;how to set up logging&lt;/CODE&gt; in python scripts &lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 01:41:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281581#M5181</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2015-12-17T01:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281582#M5182</link>
      <description>&lt;P&gt;there's a way to log it through splunkd.log  or your own, etc.  for more details than even Splunk docs offer, just see python docs for logging.  &lt;A href="https://docs.python.org/2/library/logging.html"&gt;https://docs.python.org/2/library/logging.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 02:18:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281582#M5182</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2015-12-17T02:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281583#M5183</link>
      <description>&lt;P&gt;I wish that was the case, however I have the shebang in there, and it's still doing this. I just left it out of my answer. Sorry for the confusion. &lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 15:37:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281583#M5183</guid>
      <dc:creator>tmarlette</dc:creator>
      <dc:date>2015-12-17T15:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281584#M5184</link>
      <description>&lt;P&gt;Truthfully, I believe that is what this is. I say that because, when I adjust the syntax of the subprocess, I can see some of the output in splunk, so the script itself looks like it's working.  For whatever reason It just doesn't like want to use the splunk search results accordingly. &lt;/P&gt;

&lt;P&gt;also, when I do run the script with these settings, I also see a failed login attempt in the destination machine's syslog logs.  here is the message:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;Failed password for tmarlett from  port 35168 ssh2&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 16:40:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281584#M5184</guid>
      <dc:creator>tmarlette</dc:creator>
      <dc:date>2015-12-17T16:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281585#M5185</link>
      <description>&lt;P&gt;mind you, the EXACT same line of the code is SUCCESSFUL when put into a python only script. &lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 16:41:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281585#M5185</guid>
      <dc:creator>tmarlette</dc:creator>
      <dc:date>2015-12-17T16:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281586#M5186</link>
      <description>&lt;P&gt;Can you recommend a good app to use as a reference for logging?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2016 16:51:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281586#M5186</guid>
      <dc:creator>tmarlette</dc:creator>
      <dc:date>2016-01-06T16:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: alert to remotely run a script</title>
      <link>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281587#M5187</link>
      <description>&lt;P&gt;I figured this out. This was an issue with passing ssh keys properly. Make sure the script you're using is passing the appropriate ssh key of the user that is running the Splunk instance on that search head. &lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2016 21:06:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/alert-to-remotely-run-a-script/m-p/281587#M5187</guid>
      <dc:creator>tmarlette</dc:creator>
      <dc:date>2016-06-02T21:06:00Z</dc:date>
    </item>
  </channel>
</rss>

