<?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: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7 in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29916#M344</link>
    <description>&lt;P&gt;Thanks Lowell. You made my day and definately deserve credit for the answer. I would greatly appreciate your help in re-writing the script to read from the context of splunk search instead of CSV file from stdin. I am posting it in the below section&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jan 2011 10:19:56 GMT</pubDate>
    <dc:creator>bansi</dc:creator>
    <dc:date>2011-01-24T10:19:56Z</dc:date>
    <item>
      <title>Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29912#M340</link>
      <description>&lt;P&gt;I have a Scripted Lookup Script which  works great when ran with Python 2.7 version but fails under Splunk Python version 2.6
Here is the error&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    c:\Splunk\etc\system\bin&amp;gt;splunk cmd python namelookup.py memberId memberName &amp;gt; memberInput.csv
Traceback (most recent call last):
  File "namelookup.py", line 7, in &amp;lt;module&amp;gt;
    import pyodbc
ImportError: DLL load failed: The specified module could not be found.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So from the error its quite understandable,
the pyodbc, the python extension module needs to be run under Splunk environment&lt;/P&gt;

&lt;P&gt;But Splunk Python version 2.6 doesnt support pyodbc module on 64bit OS hence i have to install  Python 2.7 with pyodbc module underneath it and the below scripted lookup script works fine&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;C:\Python27\python.exe namelookup.py memberId memberName &amp;lt; memberInput.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So how would i make the same scripted lookup script  work under Splunk environment i.e. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; splunk cmd python namelookup.py memberId memberName &amp;gt; memberInput.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have made the following changes but no help&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;import os
del os.environ[PYTHONPATH]
del os.environ[LD_LIBRARY_PATH]&lt;/P&gt;

&lt;P&gt;ppath = "C:\Python27"
spath = "C:\Splunk\etc\system\bin\namelookup.py"&lt;/P&gt;

&lt;P&gt;os.execv(ppath, [spath] + os.argv[1:])&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;i have copied &lt;/P&gt;

&lt;P&gt;C:\Python27\lib\site-packages, 
TO
$SPLUNK_HOME\lib\Python2.6\site-packages&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;but still it doesnt help&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2011 04:03:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29912#M340</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2011-01-21T04:03:16Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29913#M341</link>
      <description>&lt;P&gt;Because an external lookup &lt;EM&gt;must&lt;/EM&gt; use the built-in python interpreter, it looks like you'll have to make a simple wrapper python script that runs an external command, which just also happens to be another python executable.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;namedlookup_wrapper.py&lt;/STRONG&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import os, sys

# Remove problematic environmental variables if they exist.
for envvar in ("PYTHONPATH", "LD_LIBRARY_PATH"):
    if envvar in os.environ:
        del os.environ[envvar]

python_executable = r"C:\Python27\python.exe"
# Attempt to figure out the location of the real script; or you can just hard-code the full path here.  (I think this works, but I didn't double check it.
real_script = os.path.join(os.path.dirname(__file__), "namedlookup.py")

os.execv(python_executable, [ python_executable, real_script ] + sys.argv[1:])
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then replace "namedlookup.py" with "namedlookup_wrapper.py" in your lookup definition.&lt;/P&gt;

&lt;P&gt;FYI.  Here is a similar issue with scripted inputs:&lt;/P&gt;

&lt;P&gt;&lt;A rel="nofollow" href="http://answers.splunk.com/questions/2821/python-scripted-inputs-run-with-the-wrong-version-of-python-in-splunk-4-1-2"&gt;http://answers.splunk.com/questions/2821/python-scripted-inputs-run-with-the-wrong-version-of-python-in-splunk-4-1-2&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Alternately, you could use a different python database driver.  If you're connecting to SQL Server, I'd suggested pymssql.  If you really need true ODBC support, you could look into the commercial mxODBC driver (but that will cost you).  I've done this for a number of database scripted inputs that I use.&lt;/P&gt;

&lt;P&gt;But yeah, be careful messing around too much with Splunk's build in &lt;CODE&gt;site-packages&lt;/CODE&gt; folder.  Instead I recommend creating &lt;CODE&gt;.egg&lt;/CODE&gt; files, putting them in your own &lt;CODE&gt;lib&lt;/CODE&gt; folder, and explicitly doing a &lt;CODE&gt;sys.path.append("/your/lib/whatever.egg")&lt;/CODE&gt; before your &lt;CODE&gt;import&lt;/CODE&gt;.  But so far any approach I've found to extend the built in python environment has been a little bit delicate to maintain.  Of course, the wrapper approach is quite easy, but it has the implicit overhead of calling an extra python interpreter; so you have to choose what's most important to you.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2011 05:10:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29913#M341</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-01-21T05:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29914#M342</link>
      <description>&lt;P&gt;I am still getting errors: &lt;BR /&gt;
splunk cmd python namelookupWrapper.py&lt;BR /&gt;&lt;BR /&gt;
Error  File "C:\Splunk\Python-2.6\Lib\os.py", line 432, in &lt;STRONG&gt;delitem&lt;/STRONG&gt;&lt;BR /&gt;
    del self.data[key.upper()]&lt;BR /&gt;
KeyError: 'LD_LIBRARY_PATH'&lt;BR /&gt;
So i commented that line still error persist in next line&lt;BR /&gt;
splunk cmd python namelookupWrapper.py memberId memberName &amp;lt; memberInput.csv&lt;BR /&gt;
  File "c:\Splunk\etc\system\bin\namelookupWrapper.py", line 7, in &lt;MODULE&gt;&lt;BR /&gt;
    os.execv("C:\Python27\python.exe", [ "C:\Splunk\etc\system\bin\namelookup.py" ] + os.argv[1:])&lt;BR /&gt;
AttributeError: 'module' object has no attribute 'argv'&lt;/MODULE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 09:23:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29914#M342</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2020-09-28T09:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29915#M343</link>
      <description>&lt;P&gt;Whoops, that example python code was rather buggy.  I've updated it, and I'll go back and fix it in the places where I copied it from.  Please let me know if the "&lt;CODE&gt;__file__&lt;/CODE&gt;" thing works for scripted lookups.  I think it &lt;EM&gt;should&lt;/EM&gt; work, but I'd like confirmation.  (That trick assumes that both scripts are in the same directory, which is normally going to be true.)&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2011 23:09:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29915#M343</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-01-21T23:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29916#M344</link>
      <description>&lt;P&gt;Thanks Lowell. You made my day and definately deserve credit for the answer. I would greatly appreciate your help in re-writing the script to read from the context of splunk search instead of CSV file from stdin. I am posting it in the below section&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2011 10:19:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29916#M344</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2011-01-24T10:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29917#M345</link>
      <description>&lt;P&gt;LookUp Script NOT To Read from CSV File
Yes i am looking for script which reads from the context of Splunk Searching instead of taking the input from CSV file from stdin&lt;/P&gt;

&lt;P&gt;Here is the script which works from standalone but not from context of splunk search&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  # File namelookup.py
# ------------------------------
import os,csv
import sys
import logging
import logging.config
import pyodbc

FIELDS = [ "memberId", "memberName" ]
# Given a id, find its name
def lookupName(idf, cur):
        try:
        selString = "SELECT first_name FROM emp where Member_ID="
        cur.execute (selString + idf )
        row = cur.fetchone()
        return row[0]
    except:
        return []





def main():

       if len(sys.argv) != 3:
          print "Usage: python name_lookup.py [id field] [name field]"
          sys.exit(0)

       idf = sys.argv[1]
       namef = sys.argv[2]
       #THIS IS NOT DESIRED: r = csv.reader(sys.stdin)
       #r = csv.reader(open('C:\\Splunk\\etc\\system\\bin\\memberInput.csv'))
       #r = csv.reader(sys.stdin)
       #w = csv.DictWriter(sys.stdout, FEILDS)
       result = {}
       w = csv.DictWriter(sys.stdout, FIELDS)
       header = []
       first = True
       conn = pyodbc.connect('DSN=DBT1')
       cursor = conn.cursor()

       if len( idf):
            result[namef] = lookupName(idf, cursor)
            print("Result of"+ namef)
            print(result[namef])
            if len(result[namef]):
                w.writerow(result)



       cursor.close()
       conn.close()         

main()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Pl let me know how to rewrite the script to read/write from the context of splunk search&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2011 10:24:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29917#M345</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2011-01-24T10:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29918#M346</link>
      <description>&lt;P&gt;Just a security note:  Please be aware that your current code is open to a sql-injection type of attack.  For example, if one of your events contained a malicious "memberId" field with the literal value:  "&lt;CODE&gt;5; DROP TABLE emp;&lt;/CODE&gt;" you could end up allowing very bad things to happen to your database.  This is easily avoidable, simply use a parameter-based query instead of doing string concatenation (i.e. &lt;CODE&gt;selString + idf&lt;/CODE&gt;). You should be able to find some examples of how to do this in the &lt;CODE&gt;pyodbc&lt;/CODE&gt; docs.  BTW, using parameters also allows additional SQL optimizations.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2011 01:18:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29918#M346</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-01-25T01:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29919#M347</link>
      <description>&lt;P&gt;Please clarify what you mean by "reads from the context of Splunk Searching".  I don't know what you are asking.  If you are looking to gain access to the events/results of your search, then you want to make a custom search command rather than a lookup.   See: &lt;A href="http://answers.splunk.com/questions/tagged/custom-search-script"&gt;http://answers.splunk.com/questions/tagged/custom-search-script&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2011 01:20:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29919#M347</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-01-25T01:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29920#M348</link>
      <description>&lt;P&gt;Or, if you want to do something with the final results of your command, then you can look at running an alert script.  See &lt;A href="http://answers.splunk.com/questions/tagged/alert-scripts"&gt;http://answers.splunk.com/questions/tagged/alert-scripts&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2011 01:30:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29920#M348</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-01-25T01:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29921#M349</link>
      <description>&lt;P&gt;Thanks once again for quick response. I greatly appreciate it."The context of Splunk Search" means from Splunk Web the search command "source="Test_Log.txt" | xmlkv entry | lookup namelookupWrapper memberId | table memberId, memberName" doesn't return any data under "memberName" column. So i am not sure how to check if Splunk Web successfully invokes namelookupWrapper script which you suggested. Also the real script i.e. namelookup.py has code snippet "r = csv.reader(sys.stdin)" so wondering how would namelookWrapper.py would re-direct sys.stdin to real script namelookup.py.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2011 01:57:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29921#M349</guid>
      <dc:creator>bansi</dc:creator>
      <dc:date>2011-01-25T01:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29922#M350</link>
      <description>&lt;P&gt;Guys,&lt;/P&gt;

&lt;P&gt;Its simple,&lt;BR /&gt;
Copy your required packages/files from &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;&lt;STRONG&gt;/usr/local/lib/python2.7/dist-packages&lt;/STRONG&gt; &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;&lt;STRONG&gt;/opt/splunk/lib/python2.7/site-packages/&lt;/STRONG&gt;&lt;/P&gt;

&lt;HR /&gt;</description>
      <pubDate>Fri, 27 Sep 2013 14:09:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29922#M350</guid>
      <dc:creator>asifhj</dc:creator>
      <dc:date>2013-09-27T14:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Lookup Script doesn't work with Splunk Python version  but works fine with Python 2.7</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29923#M351</link>
      <description>&lt;P&gt;works great! Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2013 10:35:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Scripted-Lookup-Script-doesn-t-work-with-Splunk-Python-version/m-p/29923#M351</guid>
      <dc:creator>stefano_guidoba</dc:creator>
      <dc:date>2013-10-10T10:35:38Z</dc:date>
    </item>
  </channel>
</rss>

