<?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 How to run an alert script on field values generated in Splunk? in Alerting</title>
    <link>https://community.splunk.com/t5/Alerting/How-to-run-an-alert-script-on-field-values-generated-in-Splunk/m-p/174873#M2899</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have an alert that calls a script when invoked.&lt;/P&gt;

&lt;P&gt;The result have the 1st column as ip address [host]. I want the script to run on all IP addresses in the result.&lt;/P&gt;

&lt;P&gt;Because host is a field generated by default in Splunk. What's the best way to call a script?&lt;/P&gt;

&lt;P&gt;Is something like :&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;myscript $Host&lt;/STRONG&gt; &lt;/P&gt;

&lt;P&gt;or whatever that argument is for ip address is, possible?&lt;/P&gt;</description>
    <pubDate>Thu, 07 Aug 2014 17:29:16 GMT</pubDate>
    <dc:creator>aniketb</dc:creator>
    <dc:date>2014-08-07T17:29:16Z</dc:date>
    <item>
      <title>How to run an alert script on field values generated in Splunk?</title>
      <link>https://community.splunk.com/t5/Alerting/How-to-run-an-alert-script-on-field-values-generated-in-Splunk/m-p/174873#M2899</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have an alert that calls a script when invoked.&lt;/P&gt;

&lt;P&gt;The result have the 1st column as ip address [host]. I want the script to run on all IP addresses in the result.&lt;/P&gt;

&lt;P&gt;Because host is a field generated by default in Splunk. What's the best way to call a script?&lt;/P&gt;

&lt;P&gt;Is something like :&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;myscript $Host&lt;/STRONG&gt; &lt;/P&gt;

&lt;P&gt;or whatever that argument is for ip address is, possible?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Aug 2014 17:29:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-to-run-an-alert-script-on-field-values-generated-in-Splunk/m-p/174873#M2899</guid>
      <dc:creator>aniketb</dc:creator>
      <dc:date>2014-08-07T17:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to run an alert script on field values generated in Splunk?</title>
      <link>https://community.splunk.com/t5/Alerting/How-to-run-an-alert-script-on-field-values-generated-in-Splunk/m-p/174874#M2900</link>
      <description>&lt;P&gt;So you can't directly call your script with an argument the way you described above, but you can get pretty close with a simple wrapper script.&lt;/P&gt;

&lt;P&gt;Here is something you could get started with:&lt;/P&gt;

&lt;P&gt;run_my_script_per_host.py:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import gzip
import csv
from subprocess import call

def openany(p):
    if p.endswith(".gz"):
        return gzip.open(p)
    else:
        return open(p)

results_file = sys.argv[8]      # file with search results

for row in csv.DictReader(openany(results_file)):
    # Build a command line to call based on fields from splunk output
    my_command = [ "myscript", row["host"], ]
    call(my_command)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This script will execute &lt;CODE&gt;myscript &amp;lt;HOST&amp;gt;&lt;/CODE&gt; for every result returned by your search.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:17:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-to-run-an-alert-script-on-field-values-generated-in-Splunk/m-p/174874#M2900</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2020-09-28T17:17:24Z</dc:date>
    </item>
  </channel>
</rss>

