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 😫

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...