<?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 i can know splunk's method or function name inside splunk module if i need to write python script? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106981#M1587</link>
    <description>&lt;P&gt;hazekamp shows using an external script named long2ip. Hazekamp, would you mind sharing the contents of long2ip.py so I can learn from it?&lt;/P&gt;

&lt;P&gt;In trying and failing to reproduce long2ip.py by example, this is what I have done:&lt;/P&gt;

&lt;P&gt;&lt;A href="http://python-iptools.readthedocs.org/en/latest/#iptools.long2ip"&gt;http://python-iptools.readthedocs.org/en/latest/#iptools.long2ip&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I installed iptools and created a py file as simple as possible trying to follow the most simple examples that came with splunk. &lt;/P&gt;

&lt;P&gt;long2ip.py returns the expected result at the command line if I comment out the splunk specific lines:&lt;/P&gt;

&lt;P&gt;python long2ip.py&lt;BR /&gt;
127.0.0.1&lt;/P&gt;

&lt;P&gt;but when running from within splunk &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="passivecidr" decip="*"|fields + decip|long2ip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I get error code 1, instead of what I would expect, a 127.0.0.1 for every search result. &lt;/P&gt;

&lt;P&gt;long2ip.py is located in $SPLUNK_HOME/etc/apps/search/bin dir and this is v5.0.1&lt;/P&gt;

&lt;P&gt;In $SPLUNK_HOME/etc/apps/search/local/commands.conf &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[long2ip]
filename = long2ip.py
streaming = true
retainsevents = true
overrides_timeorder = false


# Copyright (C) 2005-2012 Splunk Inc. All Rights Reserved.  Version 4.0
import sys,splunk.Intersplunk
import iptools
try:
    results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()

    newresults = []
    newresults = iptools.long2ip(2130706433)

except:
    import traceback
    stack =  traceback.format_exc()
    results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack))

splunk.Intersplunk.outputResults(newresults)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 04 Dec 2012 12:27:12 GMT</pubDate>
    <dc:creator>data4734</dc:creator>
    <dc:date>2012-12-04T12:27:12Z</dc:date>
    <item>
      <title>How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106976#M1582</link>
      <description>&lt;P&gt;python script like this for convers form decimal to ip &lt;/P&gt;

&lt;P&gt;ipint = int(&amp;lt;&lt;STRONG&gt;input from splunk search&lt;/STRONG&gt;&amp;gt;) &lt;BR /&gt;
ip=""&lt;BR /&gt;
for i in range(4):&lt;BR /&gt;
        ip1 = ""&lt;BR /&gt;
        for j in range(8):&lt;BR /&gt;
               # print ipint % 2&lt;BR /&gt;
                ip1=str(ipint % 2)+ip1&lt;BR /&gt;
                ipint = ipint &amp;gt;&amp;gt; 1&lt;BR /&gt;
               # print ip1&lt;BR /&gt;
        print ip1&lt;BR /&gt;
        ip = str(int(ip1,2)) + "." + ip&lt;BR /&gt;
print ip.strip(".")&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;if i want to create command for use with splunk search!! What splunk module or method i need to use for python script.&lt;/P&gt;</description>
      <pubDate>Sat, 21 May 2011 07:44:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106976#M1582</guid>
      <dc:creator>claudius</dc:creator>
      <dc:date>2011-05-21T07:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106977#M1583</link>
      <description>&lt;P&gt;claudis,&lt;/P&gt;

&lt;P&gt;You could implement this using an external lookup or search command/script.  I have provided links below which detail both methods along with sample search syntax.&lt;/P&gt;

&lt;P&gt;Search w/ command: &lt;A href="http://www.splunk.com/base/Documentation/latest/Developer/SearchScripts"&gt;Search Scripts&lt;/A&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;your search&amp;gt; | long2ip in=src_long out=src_ip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Search w/ lookup: &lt;A href="http://www.splunk.com/base/Documentation/4.2.1/Knowledge/Addfieldsfromexternaldatasources"&gt;External Lookups&lt;/A&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;your search&amp;gt; | lookup long2ip src_long OUTPUT src_ip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This can also be implemented as a macro:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;## macros.conf
[long2ip(2)]
args = long, ip
definition = eval $long$temp=$long$ | eval $ip$0=round($long$temp/16777216) | eval $long$temp=$long$temp-($ip$0*16777216) | eval $ip$1=round($long$temp/65536) | eval $long$temp=$long$temp-($ip$1*65536) | eval $ip$2=round($long$temp/256) | eval $ip$3=$long$temp-($ip$2*256) | strcat $ip$0 "." $ip$1 "." $ip$2 "." $ip$3 $ip$ | fields - $ip$0,$ip$1,$ip$2,$ip$3,$long$temp
iseval = 0

## Search
&amp;lt;your search&amp;gt; | `long2ip(src_long, src_ip)`
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2011 21:19:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106977#M1583</guid>
      <dc:creator>hazekamp</dc:creator>
      <dc:date>2011-05-23T21:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106978#M1584</link>
      <description>&lt;P&gt;Splunk allows the creation of &lt;A href="http://www.splunk.com/base/Documentation/latest/SearchReference/Aboutcustomsearchcommands"&gt;custom search commands&lt;/A&gt;.  &lt;/P&gt;

&lt;P&gt;Alternatively, you can do this using the existing Splunk search language to solve this problem.  &lt;/P&gt;

&lt;P&gt;The below solution is intentionally verbose to show the different steps. &lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;First, break the binary into 4 equal
parts (each having a length of 8).&lt;/LI&gt;
&lt;LI&gt;Next, convert each octet to its
decimal form. &lt;/LI&gt;
&lt;LI&gt;Finally, combine the
results.&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;This example would take an &lt;CODE&gt;ip_binary=11000000101010000000000000000001&lt;/CODE&gt; and create an &lt;CODE&gt;ip_decimal=192.168.0.1&lt;/CODE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...
| eval ip_binary=substr(ip_binary,1,8) + "." + substr(ip_binary,9,8) + "." + substr(ip_binary,17,8) + "." + substr(ip_binary,25,8) 
| makemv delim="." ip_binary 
| eval ip_decimal=tostring(tonumber(mvindex(ip_binary,0),2)) + "." + tostring(tonumber(mvindex(ip_binary,1),2)) + "." + tostring(tonumber(mvindex(ip_binary,2),2)) + "." + tostring(tonumber(mvindex(ip_binary,3),2))
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2011 21:33:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106978#M1584</guid>
      <dc:creator>bwooden</dc:creator>
      <dc:date>2011-05-23T21:33:45Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106979#M1585</link>
      <description>&lt;P&gt;Here's an example script I write to do a reverse DNS lookup for an IP, it adds a new field dns_name. You can refactor it with your code.&lt;/P&gt;

&lt;P&gt;search &amp;gt; * | long2ip field=ip&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk.Intersplunk
import sys
import socket 

def resolve(ip):
    name = 'unresolved'
    try:
        name = socket.gethostbyaddr(ip)[0] 
    except:
        pass
    return name

try:   
    # get the field 
    field = None

    if len(sys.argv) &amp;gt; 1:
            for a in sys.argv:
            if a.startswith("field="):
                        where = a.find('=')
                        field = a[where+1:len(a)].strip()

    if not field:
        results = splunk.Intersplunk.generateErrorResults("no field specified")
    else:
        results,unused1,unused2 = splunk.Intersplunk.getOrganizedResults()

        for result in results:
            result['dns_name'] = resolve(result[field])

        splunk.Intersplunk.outputResults(results)

except Exception, e:
    results = splunk.Intersplunk.generateErrorResults(str(e))
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 May 2011 11:46:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106979#M1585</guid>
      <dc:creator>Marinus</dc:creator>
      <dc:date>2011-05-24T11:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106980#M1586</link>
      <description>&lt;P&gt;Great....too many ideas, thanks and thanks everyone for good answer.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2011 15:12:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106980#M1586</guid>
      <dc:creator>claudius</dc:creator>
      <dc:date>2011-05-26T15:12:08Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106981#M1587</link>
      <description>&lt;P&gt;hazekamp shows using an external script named long2ip. Hazekamp, would you mind sharing the contents of long2ip.py so I can learn from it?&lt;/P&gt;

&lt;P&gt;In trying and failing to reproduce long2ip.py by example, this is what I have done:&lt;/P&gt;

&lt;P&gt;&lt;A href="http://python-iptools.readthedocs.org/en/latest/#iptools.long2ip"&gt;http://python-iptools.readthedocs.org/en/latest/#iptools.long2ip&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I installed iptools and created a py file as simple as possible trying to follow the most simple examples that came with splunk. &lt;/P&gt;

&lt;P&gt;long2ip.py returns the expected result at the command line if I comment out the splunk specific lines:&lt;/P&gt;

&lt;P&gt;python long2ip.py&lt;BR /&gt;
127.0.0.1&lt;/P&gt;

&lt;P&gt;but when running from within splunk &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="passivecidr" decip="*"|fields + decip|long2ip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I get error code 1, instead of what I would expect, a 127.0.0.1 for every search result. &lt;/P&gt;

&lt;P&gt;long2ip.py is located in $SPLUNK_HOME/etc/apps/search/bin dir and this is v5.0.1&lt;/P&gt;

&lt;P&gt;In $SPLUNK_HOME/etc/apps/search/local/commands.conf &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[long2ip]
filename = long2ip.py
streaming = true
retainsevents = true
overrides_timeorder = false


# Copyright (C) 2005-2012 Splunk Inc. All Rights Reserved.  Version 4.0
import sys,splunk.Intersplunk
import iptools
try:
    results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()

    newresults = []
    newresults = iptools.long2ip(2130706433)

except:
    import traceback
    stack =  traceback.format_exc()
    results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack))

splunk.Intersplunk.outputResults(newresults)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Dec 2012 12:27:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106981#M1587</guid>
      <dc:creator>data4734</dc:creator>
      <dc:date>2012-12-04T12:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106982#M1588</link>
      <description>&lt;P&gt;Here is the script we use (this can be configured to run automatically via props.conf as well):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;###### ip2long.py ######
'''
Copyright (C) 2005-2011 Splunk Inc. All Rights Reserved.
'''
import csv
import re
import sys

from ipMath import LongToIP, IPToLong

# set the maximum allowable CSV field size 
# 
# The default of the csv module is 128KB; upping to 10MB. See SPL-12117 for 
# the background on issues surrounding field sizes. 
# (this method is new in python 2.5) 
csv.field_size_limit(10485760)


if __name__ == '__main__':

    ipRex = re.compile('^(([0-1]\d{0,2}|2[0-4]\d|25[0-5]|\d{0,2})\.){3}([0-1]\d{0,2}|2[0-4]\d|25[0-5]|\d{0,2})$')

    ## Retrieve input
    inputResults = csv.reader(sys.stdin, lineterminator='\n')

    header = inputResults.next()

    ## Initialize output handlers
    csv.writer(sys.stdout, lineterminator='\n').writerow(header)
    output = csv.DictWriter(sys.stdout, header, lineterminator='\n', restval='')

    longKey = header.index('long')
    ipKey = header.index('ip')

    for inputResult in inputResults:
        ## Initialize and populate output lists/dictionary
        outputResult = {}

        if len(inputResult[longKey]) &amp;gt; 0:
            outputResult['long'] = inputResult[longKey]

            try:
                longVal = long(inputResult[longKey])

                if longVal &amp;gt;= 0 and longVal &amp;lt;= 4294967295:
                    outputResult['ip'] = LongToIP(longVal)   

            except:
                pass

        ## The reverse lookup
        else:
            outputResult['ip'] = inputResult[ipKey]

            if ipRex.match(inputResult[ipKey]):
                outputResult['long'] = IPToLong(inputResult[ipKey])

        output.writerow(outputResult)


###### ipmath.py ######
'''
Copyright (C) 2005-2011 Splunk Inc. All Rights Reserved.
'''

import heapq
import re

def LongToIP(value):
    '''Convert long to dotted ip address.'''
    if isinstance(value, int):
        value = long(value)
    if isinstance(value, long):
        return '%d.%d.%d.%d' % ((value &amp;gt;&amp;gt; 24) % 256, (value &amp;gt;&amp;gt; 16) % 256, (value &amp;gt;&amp;gt; 8) % 256, value % 256)
    return None

### Conversion functions.
### Note that conversions should perform validation and return None
### in the event of failure.
def IPToLong(value):
    '''Convert dotted ip address to long.'''
    if is_valid_ip(value):
        ip = map(long, value.split('.'))
        return 16777216 * ip[0] + 65536 * ip[1] + 256 * ip[2] + ip[3]
    return None

###### transforms.conf ######
[ip2long]
external_cmd  = ip2long.py
external_type = python
fields_list   = ip,long
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This can be used to go from both ip-&amp;gt;long and long-&amp;gt;ip.  For example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[sourcetype]
LOOKUP-convert_ip_to_long = ip2long long as src_long OUTPUT ip as src_ip

[sourcetype]
LOOKUP-convert_long_to_ip = ip2long ip as src_ip OUTPUT long as src_long
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This definition can also be used in "| lookup" in the search language as well&lt;/P&gt;

&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Tue, 04 Dec 2012 16:23:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106982#M1588</guid>
      <dc:creator>hazekamp</dc:creator>
      <dc:date>2012-12-04T16:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106983#M1589</link>
      <description>&lt;P&gt;The macro answer shown first is good, but is prone to errors.  Change the definition to first ensure your temp value is a number and then uses floor() instead of round() to calculate the dotted decimal values.  Use of round() in the original solution will produce negative octet values in some cases.  Use of floor() imitates integer division, e.g. int(); alas a function several other community answers confirm is not available in Splunk, much to our lament:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;definition = eval $long$temp=tonumber($long$) | eval $ip$0=floor($long$temp/16777216) | eval $long$temp=$long$temp-($ip$0*16777216) | eval $ip$1=floor($long$temp/65536) | eval $long$temp=$long$temp-($ip$1*65536) | eval $ip$2=floor($long$temp/256) | eval $ip$3=$long$temp-($ip$2*256) | strcat $ip$0 "." $ip$1 "." $ip$2 "." $ip$3 $ip$ | fields - $ip$0,$ip$1,$ip$2,$ip$3,$long$temp
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jul 2017 18:59:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106983#M1589</guid>
      <dc:creator>bwmetz</dc:creator>
      <dc:date>2017-07-25T18:59:32Z</dc:date>
    </item>
    <item>
      <title>Re: How i can know splunk's method or function name inside splunk module if i need to write python script?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106984#M1590</link>
      <description>&lt;P&gt;Amendment to my suggestion to protect against invalid values in large data sets causing "..." to come out of the macro.  Adding a validation rule to the macro might be smarter, but more complex when you don't understand them.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval $long$temp=tonumber($long$) | eval $ip$0=floor($long$temp/16777216) | eval $long$temp=$long$temp-($ip$0*16777216) | eval $ip$1=floor($long$temp/65536) | eval $long$temp=$long$temp-($ip$1*65536) | eval $ip$2=floor($long$temp/256) | eval $ip$3=$long$temp-($ip$2*256) | strcat $ip$0 "." $ip$1 "." $ip$2 "." $ip$3 $ip$ | eval $ip$=if($ip$="...","",$ip$) | fields - $ip$0,$ip$1,$ip$2,$ip$3,$long$temp
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jul 2017 21:26:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-i-can-know-splunk-s-method-or-function-name-inside-splunk/m-p/106984#M1590</guid>
      <dc:creator>bwmetz</dc:creator>
      <dc:date>2017-07-25T21:26:13Z</dc:date>
    </item>
  </channel>
</rss>

