By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

How to automate user input at the command prompt?

P: n/a

I'm working on a scirpt to be used on a windows machine and I need to
automate a user's input on the command prompt. For example I'm using
os.system('mycommand') to excute the commands I want. However some of
these command will prompt for a confirmation of yes or no from user.
Is there anything in python that will allow my script to automate these
input
Another example is let say if i type "format c:\" on the command
prompt it will warn me and ask me to type 'y' to continue. I can use
the os.system to automate the "format c:\" but is there a way to
automate the 'y' input part. I try searching for help on the Interent
but cant seem find any info on how to do this with Python. Can someone
give me some confirmation if what I want to do is possible or not.
Thanks in advance for any help.

Jul 21 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi!

Same problem. I had search, long time, with popenX, with subprocess. I don't
have, actually, any solution...

Suggestion: the first which finds prevents the others...

@+

Michel Claveau

Jul 21 '06 #2

P: n/a

You may want to look at pexpect:

http://pexpect.sourceforge.net/

But I am not sure about its support on windows.

ge*****@yahoo.com wrote:
I'm working on a scirpt to be used on a windows machine and I need to
automate a user's input on the command prompt. For example I'm using
os.system('mycommand') to excute the commands I want. However some of
these command will prompt for a confirmation of yes or no from user.
Is there anything in python that will allow my script to automate these
input
Another example is let say if i type "format c:\" on the command
prompt it will warn me and ask me to type 'y' to continue. I can use
the os.system to automate the "format c:\" but is there a way to
automate the 'y' input part. I try searching for help on the Interent
but cant seem find any info on how to do this with Python. Can someone
give me some confirmation if what I want to do is possible or not.
Thanks in advance for any help.
Jul 21 '06 #3

P: n/a

dr*******@gmail.com wrote:
You may want to look at pexpect:

http://pexpect.sourceforge.net/

But I am not sure about its support on windows.
To the best of my recent investigation, and an email exchange with the
author of pexpect, it is NOT supported under Windows.

Jul 21 '06 #4

P: n/a

In that case, the OP can probably use cygwin's version of python.
pexpect definitely works there.

Mike Kent wrote:
dr*******@gmail.com wrote:
You may want to look at pexpect:

http://pexpect.sourceforge.net/

But I am not sure about its support on windows.

To the best of my recent investigation, and an email exchange with the
author of pexpect, it is NOT supported under Windows.
Jul 21 '06 #5

P: n/a
In article <11**********************@75g2000cwc.googlegroups. com>,
dr*******@gmail.com <dr*******@gmail.comwrote:
>
In that case, the OP can probably use cygwin's version of python.
pexpect definitely works there.
Jul 21 '06 #6

P: n/a

Cameron Laird wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
dr*******@gmail.com <dr*******@gmail.comwrote:

In that case, the OP can probably use cygwin's version of python.
pexpect definitely works there.
.
.
.
I suspect there are easier approaches--but probably won't have time
before Monday to explain. For now, I counsel the original poster
not to be discouraged.
Although I have not find the solution I need yet, thanks to the
suggestions so far I'm actually a bit more optimistic than before that
there might actually be a way to do what I want.

Jul 21 '06 #7

P: n/a
On 2006-07-21 19:39:52, ge*****@yahoo.com wrote:
Cameron Laird wrote:
>I suspect there are easier approaches--but probably won't have time
before Monday to explain. For now, I counsel the original poster
not to be discouraged.

Although I have not find the solution I need yet, thanks to the
suggestions so far I'm actually a bit more optimistic than before that
there might actually be a way to do what I want.
Until Cameron explains... maybe you can work out something with pipes (on
the program's stdin, stdout and stderr). Not sure how that would look like
in Python, though, and it almost for sure won't work with all command line
programs. (There are different ways how a program can read user input.)

Gerhard

Jul 21 '06 #8

P: n/a
If you have the Pywin32 extensions installed, you can use the
win32console module to send keystrokes directly to a command
prompt via WriteConsoleInput.

Roger

<ge*****@yahoo.comwrote in message news:11**********************@75g2000cwc.googlegro ups.com...
>
I'm working on a scirpt to be used on a windows machine and I need to
automate a user's input on the command prompt. For example I'm using
os.system('mycommand') to excute the commands I want. However some of
these command will prompt for a confirmation of yes or no from user.
Is there anything in python that will allow my script to automate these
input
Another example is let say if i type "format c:\" on the command
prompt it will warn me and ask me to type 'y' to continue. I can use
the os.system to automate the "format c:\" but is there a way to
automate the 'y' input part. I try searching for help on the Interent
but cant seem find any info on how to do this with Python. Can someone
give me some confirmation if what I want to do is possible or not.
Thanks in advance for any help.

Jul 21 '06 #9

P: n/a
Hi!

I had try with pipes & subprocess. Unfortunaly, when dos-commandline show a
text who question for Yes/No, this text is not available in subprocess/pipe
; =and block!
And then, I can't send "Y" to the stdin...

I test with:
MD TOTO
RD TOTO/S

(I know, RD TOTO/S/Q run Ok, but I search another solution).

*sorry for my bad english*

Michel Claveau


Jul 21 '06 #10

P: n/a
Hi!

>>There are typically only two output streams from a command line program.
stdout and stderr... Some of the OS triggered error messages
(abort, retry, fail) might be going to stderr -- so if you were looking at
stdout, you won't see them.

- when you use RD TOTO/S , win show the text "Are-you sure (Y/N)?" on
stdout, and no stderr
- when the text is send, by win, on stdout, there are no RC ; consequently,
subprocess (or popen) don't see anything
- it's possible, with popen4, with subprocess, to merge stdout & stderr in
the same pipe
The problem remain entire.
@-salutations
--
Michel Claveau

Jul 22 '06 #11

P: n/a

Cameron Laird wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
dr*******@gmail.com <dr*******@gmail.comwrote:

In that case, the OP can probably use cygwin's version of python.
pexpect definitely works there.
.
.
.
I suspect there are easier approaches--but probably won't have time
before Monday to explain. For now, I counsel the original poster
not to be discouraged.
If you happen to have time to explain the possible approaches, it would
help me tremendously. Thanks.

Jul 25 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.