Jump to content

Bash commands in php?


doodmon

Recommended Posts

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?

Link to comment
Share on other sites

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);

    }

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.