Stepping through interactive commands in Python

9 11 2012

This week’s gem: Python pexpect module (

Annoyed by some test software that forces me to step through commands in interactive mode (telnet-like) I decided to look for something in Python to make my life easier. I started looking in subprocess but didn’t find anything useful. Someone in a forum suggested pexpect instead and I was thrilled….

So, for example, instead of typing “telnet localhost 7777” manually again and again (see here), I could’ve written a code below once, and use it as many time as I want, with very little typing:

import pexpect as pe

ch = pe.spawn('telnet localhost 7777')
ch.sendline('verbose yes')

out = ch.before


“out” contains now a string that can be parsed to get whatever information I want.

Me 1:0 Computer