PHP Exec team - how to pass input to a series of questions

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

The program is not intended to enter 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 output in an array by writing: $ out = array (); Exec ("my / path / program", $ from);

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

I am not php noob, but just never done this before. Alas, my googling goes in circles.

+4
source share
3 answers

At first, just so you know you're trying to invent a wheel. What you are really looking for is expect (1) , which is a command-line utility designed 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 when reading from STDOUT and writing to the STDIN of a child process using proc_open :

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

Hope this helps.

+4
source

Just add the arguments to the exec line.

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

... but don't forget to apply escapeshellarg() to each argument! Otherwise, you are vulnerable to malicious code.

0
source
 $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); 
0
source

Source: https://habr.com/ru/post/1333621/


All Articles