Splunk Dev

How can I use tab-completion & command history in the python that is packaged with Splunk?

anirban_td
Explorer

Hello splunkers, 

 

How can I use tab-completion & command history in the python that is packaged with Splunk? 

The python version [./bin/splunk cmd python] with Splunk Enterprise v9 is 3.7.11
However, there is no tab-completion or command history.. 

tab is interpreted as 4 whitespace, while up/down arrow key is interpreted as ^[[A or ^[[B. 
even simple cursor positioning using right/left arrow keys are interpreted as [[D^ OR [[C^.. 

 

 

(dev2) splunk@host1:~ $ ./bin/python3
Python 3.7.11 (default, Jul 27 2022, 02:48:51)
[GCC 9.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ^[[A
  File "<stdin>", line 1
    ^
SyntaxError: invalid syntax
>>> ^[[B
  File "<stdin>", line 1

    ^
SyntaxError: invalid syntax
>>>

 

 

This is a simple requirement to have quick & dirty troubleshooting for python commands. Its a major pain to not have access to history or not being able to use L/R arrow keys to move cursor.

Please help. 
Thanks in advance!

Labels (1)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

FWIW, the cursor keys work as expected on one of my systems and not so well on the other.  On the system where cursor don't work, the behavior is the same for both the Splunk-supplied and system-installed Python interpreters.  It could be how the terminal definitions are set up on each system, but I didn't go into it that far.

---
If this reply helps you, Karma would be appreciated.
0 Karma

anirban_td
Explorer

 

@richgalloway thanks for your answer! 

i have tested on a handful of our splunk instances, and all of them have the same behavior: 

  • splunk python (v3.7.11)
    • arrow keys nav/history - does not work
    • tab-completion - does not work
  • server python (v2.7.5) - 

in the splunk python cli, readline & rlcompleter modules are not present: 

splunk@host1:~ $ ./bin/splunk cmd python
Python 3.7.11 (default, Jul 27 2022, 02:48:51)
[GCC 9.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'readline'
>>> import rlcompleter
>>> readline.parse_and_bind("tab: complete")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'readline' is not defined
>>>

 

so no arrow keys or tab-completion 😫

Get Updates on the Splunk Community!

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...