doodmon Posted December 19, 2007 Share Posted December 19, 2007 Hi, I'm probably in over my head but it seems like there should be a solution to my problem. I know you can create socket connections to remote locations. In these socket connections you can input data and the process will execute it without suspending the rest of your php script. I need to perform something similar, only locally. I'm looking for a method of emulating a terminal which my php code will enter commands into while analyzing the output and, depending on what is received, will input other commands. More specifically... I am using Expect Dislocate to sustain a telnet connection to a remote location which I will constantly be feeding commands. I need to first call 'dislocate telnet' which opens a telnet session with the ability for my php server to loose connectivity and regain it later (as long as I obtain the PID which is simple enough). When I use shell_exec('dislocate telnet') or simply exec('dislocate telnet') these hang because the telnet prompt is called. If I attempt to run it in the background : exec('dislocate telnet &') I don't have a way of inputting to the telnet session I just started! I've looked at phpTelnet sockets which work great, but my need is to be able to sustain a connection between different php pages (my understanding is that a socket is closed when the php script which opened the socket has finished executing). Is there a way to separate an xterm-like console from my php script while inputting and reading from it freely? Quote Link to comment https://forums.phpfreaks.com/topic/82281-bash-commands-in-php/ Share on other sites More sharing options...
trq Posted December 19, 2007 Share Posted December 19, 2007 You might be able to do this using screen but really, its probably gonna get pretty messy. Quote Link to comment https://forums.phpfreaks.com/topic/82281-bash-commands-in-php/#findComment-418393 Share on other sites More sharing options...
doodmon Posted December 20, 2007 Author Share Posted December 20, 2007 Screens seems to be doing the trick! I am giving each screen a unique name so I don't need to recall the PID. I can terminate and access the screen via that name. I call the screen in a pseudo terminal environment (nice little code I found online). $descriptorspec = array( 0 => array("pty", "w"), 1 => array("pty", "r"), 2 => array("pty", "r") ); $process = proc_open("TERM=xterm screen -S ".$screenname, $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], "commands in shell\r"); echo fread($pipes[1], 1024); fclose($pipes[0]); } my problem now is that if I attempt to fread() too much (ie, more than what is available in the terminal), The process hangs. I found this loop but it hangs as well: while (!feof($pipes[1])) { echo fgets($pipes[1], 1024); } Quote Link to comment https://forums.phpfreaks.com/topic/82281-bash-commands-in-php/#findComment-419672 Share on other sites More sharing options...
doodmon Posted December 20, 2007 Author Share Posted December 20, 2007 fgets has similar issues Quote Link to comment https://forums.phpfreaks.com/topic/82281-bash-commands-in-php/#findComment-419674 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.