Automatically filling in answers to interactive shell command questions

T

Ted Weatherly

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?

-Ted
 
R

Ravi

Ted said:
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:

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?

-Ted

As a last resort, you can always use PExpect. It pretends to be a user
sitting at a terminal. http://pexpect.sf.net

Ravi
 
J

Jp Calderone

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:

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?

Ahh, but addperson already reads data from a file! stdin! Have you
tried:

cat answer_file | addperson

or even

addperson < answer_file

or, to keep it Python related,

Jp
 
E

Erik Max Francis

Ted said:
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:

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
as:

addperson <<EOF
Ted
12/08/1977
6'0"
San Francisco, CA
EOF

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.
 
A

Antti Kaihola

import os
Would this work:

import os, shutil
shutil.copyfileobj(file('answer_file'), os.popen('addperson', 'w'))
 
T

Ted Weatherly

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.

Thanks again!

-Ted

PS> The actual command I was using is different. Regardless, this python
script should do the trick (it might have some minor flaws):

#!/usr/bin/env python
# addperson.py
"""This automatically answers questions when running addperson
"""

import pexpect
import re

# 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: ',
'Birthday: ',
'Height: ',
'Location: '])
if i>=0 or i<=3:
# Send answer for this question
child.sendline (a)
#print 'Answered question #'+str(i+1)
if i==3:
done = 1
print 'Done with'+str(a[0])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top