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!

Customer Experience | Splunk 2024: New Onboarding Resources

In 2023, we were routinely reminded that the digital world is ever-evolving and susceptible to new ...

Celebrate CX Day with Splunk: Take our interactive quiz, join our LinkedIn Live ...

Today and every day, Splunk celebrates the importance of customer experience throughout our product, ...

How to Get Started with Splunk Data Management Pipeline Builders (Edge Processor & ...

If you want to gain full control over your growing data volumes, check out Splunk’s Data Management pipeline ...