Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Automatically filling in answers to interactive shell command questions

Reply
Thread Tools

Automatically filling in answers to interactive shell command questions

 
 
Ted Weatherly
Guest
Posts: n/a
 
      07-21-2003
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
 
Reply With Quote
 
 
 
 
Jegenye 2001 Bt
Guest
Posts: n/a
 
      07-21-2003
If you use Linux then "expect" (a Tcl thing) is written just for this.



 
Reply With Quote
 
 
 
 
Trent Mick
Guest
Posts: n/a
 
      07-21-2003

Check out pexpect, a Python version of the popular Tcl-land Expect
program:
http://pexpect.sourceforge.net/

I haven't used it but I get the feeling that pexpect is Un*x-only if
that affects you at all.

Trent

[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?
>
> -Ted
> --
> http://mail.python.org/mailman/listinfo/python-list


--
Trent Mick


 
Reply With Quote
 
Ravi
Guest
Posts: n/a
 
      07-21-2003
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?
>
> -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

 
Reply With Quote
 
Jp Calderone
Guest
Posts: n/a
 
      07-21-2003
On Mon, Jul 21, 2003 at 03:23:25PM -0700, 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?
>


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,

>>> import os
>>> outf = os.popen('addperson', 'w')
>>> inf = file('answer_file', 'r')
>>> outf.write(inf.read())
>>> inf.close()
>>> outf.close()


Jp

--
"If you find a neighbor in need, you're responsible for serving that
neighbor in need, you're responsible for loving a neighbor just like you'd
like to love yourself." -- George W. Bush, Sept. 16, 2002

 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      07-22-2003
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
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.

--
Erik Max Francis && && 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)
 
Reply With Quote
 
Antti Kaihola
Guest
Posts: n/a
 
      07-22-2003
> >>> import os
> >>> outf = os.popen('addperson', 'w')
> >>> inf = file('answer_file', 'r')
> >>> outf.write(inf.read())
> >>> inf.close()
> >>> outf.close()


Would this work:

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

 
Reply With Quote
 
Ted Weatherly
Guest
Posts: n/a
 
      07-22-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.

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[i])
#print 'Answered question #'+str(i+1)
if i==3:
done = 1
print 'Done with'+str(a[0])


"Erik Max Francis" <> wrote in message
news:...
> 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
> 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.
>
> --
> Erik Max Francis && && 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)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PDF forms filling with ASP.NET, interactive forms, fill and write to database (iText or FDF Toolkit or ...) Toni Pohl ASP .Net 1 08-10-2008 01:40 PM
filling forms automatically generated using javascript John Python 1 11-22-2006 05:17 AM
Shell not filling less than 5KG cylinders RayC NZ Computing 12 06-21-2006 09:32 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM
Automatically Filling website page and triggering events =?Utf-8?B?RGFu?= ASP .Net 1 08-10-2004 06:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57