Automatically filling in answers to interactive shell command questions

Discussion in 'Python' started by Ted Weatherly, Jul 21, 2003.

  1. 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
     
    Ted Weatherly, Jul 21, 2003
    #1
    1. Advertising

  2. If you use Linux then "expect" (a Tcl thing) is written just for this.
     
    Jegenye 2001 Bt, Jul 22, 2003
    #2
    1. Advertising

  3. Ted Weatherly

    Trent Mick Guest

    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
     
    Trent Mick, Jul 22, 2003
    #3
  4. Ted Weatherly

    Ravi Guest

    Re: Automatically filling in answers to interactive shell commandquestions

    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
     
    Ravi, Jul 22, 2003
    #4
  5. Ted Weatherly

    Jp Calderone Guest

    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
     
    Jp Calderone, Jul 22, 2003
    #5
  6. Re: Automatically filling in answers to interactive shell commandquestions

    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)
     
    Erik Max Francis, Jul 22, 2003
    #6
  7. Re: Automatically filling in answers to interactive shell commandquestions

    > >>> 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'))
     
    Antti Kaihola, Jul 22, 2003
    #7
  8. Re: Automatically filling in answers to interactive shell command questions

    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])


    "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)
     
    Ted Weatherly, Jul 22, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    5,888
    Jonathan Bromley
    Feb 18, 2005
  2. Toni Pohl
    Replies:
    1
    Views:
    980
    Toni Pohl
    Aug 10, 2008
  3. sagar varule
    Replies:
    6
    Views:
    169
    Joshua Landau
    Aug 11, 2013
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    146
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    133
Loading...

Share This Page