Automatically filling in answers to interactive shell command questions
ted at REMOVE_THIS.sendmail.com
Tue Jul 22 20:16:24 CEST 2003
Thanks for all the useful suggestions!
I tried both approaches, piping to STDIN using 'addperson < person.data' and
using a pexpect script (below), and both work well. Using pexpect hides all
the STDOUT text, and piping to STDIN displays the STDOUT text (but doesn't
show the answers provided).
Some of the questions for my 'addperson' command are conditional...ie.
sometimes 'Birthday: ' is asked and sometimes it isn't. Therefore, pexpect
is a better fit for me.
PS> The actual command I was using is different. Regardless, this python
script should do the trick (it might have some minor flaws):
"""This automatically answers questions when running addperson
# Store list of answers about each person
answers = [
['Ted', '12/08/1977', '6\'0"', 'San Francisco, CA'],
['Jesus', '12/25/0000', '6\'2"', 'Bethleham'],
['Satan', 'n/a', 'n/a"', 'Hell'],
# Iterate over all the people
for a in answers:
done = 0
child = pexpect.spawn ('/usr/sbin/addperson')
# Iterate over all the questions for the command
while not done:
i = child.expect (['First Name: ',
if i>=0 or i<=3:
# Send answer for this question
#print 'Answered question #'+str(i+1)
done = 1
print 'Done with'+str(a)
"Erik Max Francis" <max at alcyone.com> wrote in message
news:3F1C8070.787A7293 at alcyone.com...
> Ted Weatherly wrote:
> > Is it possible to use python to automatically fill in the answers to
> > interactive shell command questions? For example, I am using a shell
> > command addperson that behaves as follows:
> > > addperson
> > First Name: Ted
> > Birthday: 12/08/1977
> > Height: 6'0"
> > Location: San Francisco, CA
> > >
> > I am prompted for "First Name: " and I enter "Ted". I am prompted for
> > "Birthday: " and I enter "12/08/1977". Imagine doing this for 100
> > people or more...it gets tiresome.
> > Assuming I can't change the addperson command to read data from a
> > file, is there any way to use python to fill in the data for the
> > expected questions? Perhaps a different language would be better?
> expect was designed for this. If the addperson program doesn't require
> an interactive shell, you can get away with it with something as simple
> addperson <<EOF
> San Francisco, CA
> Or by putting these one per line in a file,
> addperson < people.data
> One certainly _could_ use Python for this, but in this case there are
> probably better (and much simpler) tools for the job.
> Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
> __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
> / \ Behind an able man there are always other able men.
> \__/ (a Chinese proverb)
More information about the Python-list