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!

Splunk AI Assistant for SPL | Key Use Cases to Unlock the Power of SPL

Splunk AI Assistant for SPL | Key Use Cases to Unlock the Power of SPL  The Splunk AI Assistant for SPL ...

Buttercup Games: Further Dashboarding Techniques (Part 5)

This series of blogs assumes you have already completed the Splunk Enterprise Search Tutorial as it uses the ...

Customers Increasingly Choose Splunk for Observability

For the second year in a row, Splunk was recognized as a Leader in the 2024 Gartner® Magic Quadrant™ for ...