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 😫

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

How much can you really learn in 3 minutes?

Observability can certainly be hard to understand – there's a lot of jargon and buzzwords and it seems to ...

Event Series: The Agentic SOC: Trust Before Autonomy

AI is fundamentally changing security operations, but true progress requires more than just automation—it ...

Free Professional Services for .conf26 Attendees

This year at .conf26, we are doing something a little different. We are bringing the best minds from ...