Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap a CLI program in Python (keeping the interactivity)?

I'd like to write a wrapper for an interactive CLI Program (the Asterisk CLI).

Basically, I need to keep the interaction with the CLI (including tab-completion) but I want to filter the output of Asterisk, in order to show only lines matching a given pattern.

I tried a select() based approach, using popen.popen4 and putting asterisk stdout_and_stderr and sys.stdin in the read_fs, but it sort of didn't work.

Can anyone give some good pointers to me?

Thanks a lot, Andrea

like image 605
Andrea Avatar asked Jun 28 '26 01:06

Andrea


1 Answers

Pexpect might be useful for you: http://sourceforge.net/projects/pexpect/

Description from the webpage: "Pexpect is a Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. Pexpect is pure Python."

like image 101
Filip Korling Avatar answered Jun 29 '26 13:06

Filip Korling