How Do I Simulate a Shell Terminal via PHP?

----------- Summary -----------
I need a command/set of commands that can help me simulate a shell terminal via a PHP web page using commandline functions. How can I combine the power of nohup and a while loop to:

  1. start the shell,
  2. execute commands and print output (and errors i.e. 2>&1) to screen (STDOUT),
  3. send the shell to background when PHP disconnects to wait for more commands from the user, and
  4. killing the process after the exit command is issued or a certain period, say 5 mins has elapsed with no response?

Is there an easier or better way (SSH being out of the question)?

----------- The Full Story -----------
What I Need To Accomplish
I have a bash script (outsourced) that�s located outside of the web root. I need to access it (and the shell) interactively via HTTPS using PHP�s shell_exec() function. This is to support a big chunk of my clients who use my app on shared hosts and have no SSH access.

Not ideal, I know, but I can�t think of any other option. To mitigate the security risks I�m making this page accessible only to �Super Administrators� (who already have FTP access anyway). I�m also escaping the commands and arguments with escapeshellcmd() and escapeshellarg().

Problem Areas

  • The shell script sometimes takes input from the Super Admin (via a web page), processes it, returns results so far and asks for confirmation to complete the process.

    I thought I might be able to accomplish this by using combining nohup and a while loop but I can�t figure out the exact commands to do so. I just recently bought a couple of books on bash and shell scripting but I need an answer fast so I�m turning to you, the expert :slight_smile: Help please�
  • The shell script sometimes needs to make backups before it can finish the process. Via SSH, it�s doing this flawlessly but through shell_exec() it�s failing to create the backup directory.

    This is most likely a permissions issue. If so how do I solve it without root access since this will be running on shared hosts?

I have searched Google and this forum for two days. I have gotten some great pointers and tried a few �PHP Terminal Emulators� out there but they are not interactive. They are doing directory traversing via PHP itself and can�t handle confirmation dialogs like �Are you sure you want to continue?�, which requires y/n to be passed back.

I think only PHP would be very limited...

This one uses PHP+Ajax for the interactivity you want xwiterm - eXtreme Web Interactive Terminal - Google Project Hosting.

There are some more, I think, just google for "PHP Ajax terminal emulator"

1 Like

@teresaejunior Yes, you are right. I didn't mention this but I'm already using AJAX on the script. I skipped mentioning AJAX because the real functionality is coming from the interaction between PHP and the shell. AJAX is only enhancing a user's experience by not refreshing the page.

Thanks for the link to that script. The ones I tried before couldn't handle user interaction. This one says it has "the possibility to execute commands that needs user interaction" so let me try it out and let you know how it goes.

Thanks a lot :slight_smile:

I have Webmin installed on my machine, and it has a nice new module called "ajaxterm" and I've just tried it and yes, it supports such interactivity! And does very good! Commands like "read" and "sudo" work perfectly!

Both of these scripts sound great but it looks like xwiterm requires root access and ajaxterm requires either root or SSH access

Is that the case or I'm wrong?

xwiterm is requiring my "UNIX password". If I try putting in my cPanel password it's (obviously) giving me

It's running commands as su. I'm trying to create this script for users with no root or SSH access on a shared host otherwise I would recommend them to use SSH as it's safer and easier that way.

Have you ever tried running ajaxterm separately on a shared host? If you know how to make it run please teach me. Thanks for both of these scripts, I will go over the source code. Maybe I can learn a thing or two from there.

You are going to need root access to install Webmin on the host for ajaxterm. Later you can assign permissions to users to use modules such as ajaxterm, but as you have stated, it could drop the user to SSH. I have tried it both as root and as an Webmin user, and I couldn't notice the difference (if it is /bin/login or SSH)... but in both cases the user is going to need a UNIX password for the shell.

I don't know how safer it would be to use a PHP term instead of SSH, as you can make SSH strong and set iptables rules for dropping people who try to break through the authentication system.

---------- Post updated at 10:40 AM ---------- Previous update was at 10:32 AM ----------

Of course, each user will have its own UNIX password, so only installing and setting permissions would require a root password.

Webmin is bit similar to cPanel, so you know how it works: root has access to all modules, and the other users are limited to what modules you allow them to use.

That confirms my fears. The thing is that the application is not hosted on my server. The users download the application on my website and install it on their own hosting account to have complete control of the application. I need to support users who will be using this application on their shared hosts too.

See what I get in my desktop as an ordinary user:

teresaejunior@localhost  ~
 /bin/login 
login: Cannot possibly work without effective root

So ajaxterm, xwiterm, or any other emulator that runs /bin/login needs root access, actually, which makes it, I believe, not impossible, but much more difficult for you to have a real interactive shell...

Thanks for clarifying that. I believe it�s possible too. Actually I see two ways of achieving this.

  1. Making PHP keep the process open by using its process control functions, or
  2. Keeping the bash process itself open by toggling it between background and foreground states when PHP disconnects and connects to it respectively.

I didn�t go for option 1 because process control functions come through process control extensions which I believe are nor installed on most shared hosts plus the PHP resource limits on most shared hosts are rather low. On the other hand I believe using shell process control will make the whole script much faster and more portable because some commandline functions like shell_exec() are part of core and, I believe, are enabled on most shared hosts too.

I think nohup is key in making this work. As for keeping the process running I think a loop would be easier to implement. I also considered using suspend/wait. I believe the piece of the puzzle that�s left is the command/commands that�s why I�m here; where the experts in shell scripting gather :slight_smile: