windows 自动响应批处理文件中的命令行程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7990041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:23:21  来源:igfitidea点击:

Automating responses to command line program in batchfile

pythonwindowsbatch-filecmd

提问by dave35

I am trying to run a Windows (XP) command line program which prompts for 'Enter' or 'y' to continue. Currently, I can respond to the 'y's by running the program as: echo y | name_of_binaryin a batch file.

我正在尝试运行一个 Windows (XP) 命令行程序,该程序提示输入“Enter”或“y”以继续。目前,我可以通过将程序运行为:echo y | name_of_binary在批处理文件中来响应“y” 。

I can't figure out how to tell the shell to respond with 'Enter' or 'y' when this is required. If it gets to a 'Press Enter to continue' sort of prompt (of which there are not very many, but enough to cause problems) and echoes 'y', it get stuck in a weird loop and won't accept any input (it spews thousands of 'press enter to continue's). If I could echo first an Enter and then a 'y' in sequence, that might work, but none of the methods I tried for echoing an 'Enter' keypress worked.

我不知道如何告诉外壳在需要时用“Enter”或“y”进行响应。如果它进入“按 Enter 继续”之类的提示(其中数量不多,但足以引起问题)并回显“y”,则它会陷入一个奇怪的循环中并且不会接受任何输入(它喷出数以千计的'按回车继续')。如果我可以先回显 Enter 然后按顺序回显 'y',那可能会奏效,但是我尝试回显 'Enter' 按键的方法都没有奏效。

I am ultimately calling this batch file through os.system()in Python. If there is a way to get Python to run the binary (through os.system(name_of_binary)?) AND respond to the prompts, that would be ideal. I have already tried os.system(echo y | name_of_binary) which behaved the same way as the batch file (as it should). Should I be using a different approach, or can I solve this by modifying the 'echo ...' command I'm currently using?

我最终是os.system()在 Python 中调用这个批处理文件。如果有办法让 Python 运行二进制文件(通过 os.system(name_of_binary)?)并响应提示,那将是理想的。我已经尝试过os.system(echo y | name_of_binary) 的行为方式与批处理文件相同(应该如此)。我应该使用不同的方法,还是可以通过修改我当前使用的“echo ...”命令来解决这个问题?

回答by Aacini

The standard way to output an Enter in Batch is echo/in a line with no additional spaces. To output an Enter followed by 'Y' you may try this: (echo/&echo Y) | name_of_binary(don't insert spaces in echo/&).

批量输出 Enter 的标准方法是echo/在一行中,没有额外的空格。要输出后跟“Y”的 Enter,您可以尝试以下操作:((echo/&echo Y) | name_of_binary不要在 中插入空格echo/&)。

If that not works would be because the LineFeed characters that are inserted both after the first Enter and after the Y. The exact sequence of bytes generated by (echo/&echo Y) is: CrLfYCrLfwhere Cr is a Carriage Return and Lf is a Line Feed.

如果这不起作用,可能是因为在第一个 Enter 之后和 Y 之后都插入了 LineFeed 字符。(echo/&echo Y)生成的确切字节序列是:CrLfYCrLf其中 Cr 是回车符,Lf 是换行符。

If previous method have problems, you may create a file with just CrYbytes with this Batch file:

如果以前的方法有问题,您可以CrY使用此批处理文件创建一个仅包含字节的文件:

@echo off
echo/> EnterY.txt
(
echo e101
echo 59
echo w
echo q
) | debug EnterY.txt > nul

and then execute your command this way: name_of_binary < EnterY.txt.

然后执行你的命令是这样的:name_of_binary < EnterY.txt

PS - Please note that echo y | name_of_binarygenerate y CrLf, that is, an space after the y; that should be echo y| name_of_binary.

PS - 请注意echo y | name_of_binarygenerate y CrLf,即y;后面有一个空格 那应该是echo y| name_of_binary

回答by icarus74

Check out Expect for Windows.

查看Expect for Windows

That is all that you need. There are several books on Expect, and many tutorials (including samples). Your favourite search engine is your best friend, but the Activestate site URL given in above, has everything you'd need. While what you may find in terms of tutorials and books may be Unix centric, which is where expect originated, the Windows expect isn't too different.

这就是你所需要的。有几本关于 Expect 的书籍,以及许多教程(包括示例)。您最喜欢的搜索引擎是您最好的朋友,但上面给出的 Activestate 站点 URL 拥有您需要的一切。虽然您在教程和书籍方面可能会发现以 Unix 为中心,这也是 expect 的起源,但 Windows expect 并没有太大的不同。

Expect will allow you to automate the command-line interaction within batch scripts.

Expect 将允许您在批处理脚本中自动执行命令行交互。