PHP Exec命令-如何将输入传递给一系列问题 [英] PHP Exec command - How to pass input to a series of questions

查看:144
本文介绍了PHP Exec命令-如何将输入传递给一系列问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的linux服务器上有一个程序,每次执行时都会询问一系列相同的问题,然后提供几行输出.我的目标是使用php脚本自动化输入和输出.

该程序并非旨在接受命令行上的输入.而是,程序询问问题1并等待键盘的答案,然后程序询问问题2并等待键盘的答案,依此类推.

我知道如何通过编写以下内容来捕获数组中的输出: $ out = array(); exec("my/path/program",$ out);

但是我该如何处理输入? 假设程序问了3个问题,有效的答案是:左120 n 使用php将输入传递给程序的最简单方法是什么? 我可以在exec行上以某种方式执行此操作吗?

我不是php noob,但以前从未需要这样做. las,我的谷歌搜索盘旋了.

解决方案

首先,让您知道您正在尝试重新发明轮子.您真正要寻找的是 expect(1),它是一个命令- line实用程序旨在在不涉及PHP的情况下完全满足您的要求.

但是,如果您确实想编写自己的PHP代码,则需要使用 proc_open .这是一些有关使用proc_open从STDOUT读取并写入子进程的STDIN的良好代码示例:

最后,还有一个为PHP期望PECL模块. /p>

希望这会有所帮助.

I have a program on my linux server that asks the same series of questions each time it executes and then provides several lines of output. My goal is to automate the input and output with a php script.

The program is not designed to accept input on the command line. Instead, the program asks question 1 and waits for an answer from the keyboard, then the program asks question 2 and waits for an answer from the keyboard, etc.

I know how to capture the output in an array by writing: $out = array(); exec("my/path/program",$out);

But how do I handle the input? Assume the program asks 3 questions and valid answers are: left 120 n What is the easiest way using php to pass that input to the program? Can I do it somehow on the exec line?

I’m not a php noob but simply have never needed to do this before. Alas, my googling is going in circles.

解决方案

First up, just to let you know that you're trying to reinvent the wheel. What you're really looking for is expect(1), which is a command-line utility intended to do exactly what you want without involving PHP.

However, if you really want to write your own PHP code you need to use proc_open. Here are some good code examples on reading from STDOUT and writing to STDIN of the child process using proc_open:

Finally, there is also an Expect PECL module for PHP.

Hope this helps.

这篇关于PHP Exec命令-如何将输入传递给一系列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆