8

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.

Gmap4 guy
  • 321
  • 1
  • 2
  • 11
  • 2
    Can you make it so your command line program takes the inputs through the command? Just take the PHP values then pass them as command line arguments. – Darren Dec 29 '10 at 01:11
  • It's a C++ program and I'm more of a php/javascript kinda guy. While I do have the source code, I would prefer not to try and fiddle with it if possible. – Gmap4 guy Dec 29 '10 at 02:42
  • @user but why not do this the normal PHP way - have a form, submit it to a PHP script, exec() and output the results? – Pekka Dec 29 '10 at 08:49

3 Answers3

5

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.

Community
  • 1
  • 1
dansimau
  • 1,155
  • 1
  • 8
  • 11
  • Many thanks for the tip about expect(1). I was unaware of that tool. Bad news - I'm using a shared host (Blue host) and their php was apparently not compiled with expect(1) support. I'll explore proc_open. Thanks again. – Gmap4 guy Dec 29 '10 at 13:27
0

Just add the arguments to the exec line.

exec("/path/to/programname $arg1 $arg2 $arg3");

... but don't forget to apply escapeshellarg() on every argument! Otherwise, you're vulnerable to injected malicious code.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Pekka - thanks for the suggestion. I love simple, but it's not working for me. To clarify: The program (which I did not write) asks several separate questions. It expects a keyboard reply to each one. Is this technique supposed to take the place of keyboard entry? – Gmap4 guy Dec 29 '10 at 02:30
  • @user556597 - What your ask for is different from what you stated in the above comment, try update your question with relevant details – ajreal Dec 29 '10 at 03:04
0
$out = array();
//add elements/parameters/input to array
string $execpath = "my/path/program ";
foreach($out as $parameter) {
  $execpath += $parameter;
  //$execpath += "-"+$execpath; use this if you need to add a '-' in front of your parameters.
}
exec($execpath);
tHeSiD
  • 4,587
  • 4
  • 29
  • 49