Re: Open a stream to the shell..
| From: | M. Sokolewicz | Date: | Sat, 28 Aug 2004 12:00:48 +0000 |
| Subject: | Re: Open a stream to the shell.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-195000@lists.php.net to get a copy of this message | ||
As of PHP 4.3.0 you can use the proc_* functions (proc_open & proc_close in PHP 4.3.0+ and proc_get_status, proc_nice and proc_terminate as of PHP 5).
If you don't have access to that, you can use the following:
use shell_exec to create a process, and make it send all output to some temporary file. Then, instead of getting it directly, you just read the contents of that temporary file :)
eg:
$stream = fopen('cvs server .... > /tmp/cvsOutput', 'w+');
just read it from /tmp/cvsOutput, and write to the $stream :)
hope it helps,
- Tul
Joel Kitching wrote:
Hello, is it possible to open a stream in which I can write commands and read output from them to a shell like bash? I want to do this because this program can not be completed with just abacktickor shell_exec() command, as it's somewhat user-interactive. As an example, let's say I want to get cdecl to explain to me what "char *horse" means. I would type: # <i>cdecl</i> Typehelp' or?' for help cdecl> <i>explain char *horse</i> declare horse as pointer to char cdecl> <i>exit</i> ...where input is surrounded by <i>these</i>. This would be difficult or impossible to do with shell_exec, especially if halfway-through you have to do some calculating before you can know what to ask cdecl next. So I want to know whether you can somehow open a stream to a shell, and execute commands there. Like... $fp = fopen('/bin/bash', 'rw'); $output = fgets($fp); fputs($fp, 'cdecl'); $output = fgets($fp); $output = fgets($fp); fputs($fp, 'explain char *horse'); $expl = fgets($fp); /* Right now you could do some calculations and figure out what to do next. */ $output = fgets($fp); fputs($fp, 'exit'); $output = fgets($fp); fclose($fp); Now this obviously wouldn't work, as it would overwrite the bash executable if you had proper access, but it's just to get the idea across. So, does anyone know how I can do this?