How Do I Simulate a Shell Terminal via PHP?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How Do I Simulate a Shell Terminal via PHP?
# 1  
Old 01-19-2012
Question 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 Smilie 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.

Last edited by blogmaster; 01-20-2012 at 02:02 AM..
# 2  
Old 01-19-2012
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"
This User Gave Thanks to teresaejunior For This Post:
# 3  
Old 01-19-2012
@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 Smilie

Last edited by blogmaster; 01-20-2012 at 02:05 AM..
# 4  
Old 01-19-2012
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!

http://www.webmin.com/cgi-bin/search...earch=ajaxterm
# 5  
Old 01-19-2012
Both of these scripts sound great but it looks like xwiterm requires root access and ajaxterm requires either root or SSH access
Quote:
If run as root ajaxterm will run /bin/login, otherwise it will run ssh
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
Quote:
Authentication failure Smilie
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.
# 6  
Old 01-19-2012
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.
# 7  
Old 01-19-2012
Quote:
Originally Posted by teresaejunior
You are going to need root access to install Webmin on the host for ajaxterm...
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Kill specific terminal using shell/bash

Hello elite shell/bash specialists, I have done plenty of STFW and some RTFM, but I cannot find a clear solution to my challenge Goal: My goal is to have a script(of any language, preferably shell/bash/anything that can run things on unix), which will kill specific unix terminal windows for... (0 Replies)
Discussion started by: kamil-mech
0 Replies

2. Shell Programming and Scripting

How to save and execute terminal commands in shell?

I frequently use some commands, which I want to save in some file say myregularshell.shthese are the commands I use, I tried saving and executing, but couldn't get the preview of execution, and result is also not coming if I copy same commands and paste it on terminal result is coming cd go... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

3. Shell Programming and Scripting

Running shell script in Cygwin terminal

I am new to shell scripting. I tried to run a simple shell script using Cygwin terminal in Win XP env. The script I have written is as follows - #!/bin/bash a=5 ] && echo "true" || echo "false" But when I execute the script, getting some confusing error. The error I am getting are - ... (3 Replies)
Discussion started by: linux_learner
3 Replies

4. Shell Programming and Scripting

executing a shell script everytime the terminal is opened

the problem statement is Write a shell script, which gets executed the moment the user opens a terminal. It should display the message "Hello your_name, welcome to 172.16.4.120 server” How to get around this? (2 Replies)
Discussion started by: arindamlive
2 Replies

5. Programming

Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX

Writing a Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX I have over the years come across the same issue a couple of times, and it normally is that the read speed on SAN is absolutely atrocious when doing non-sequential I/O to the disks. Problem being of... (7 Replies)
Discussion started by: vrghost
7 Replies

6. Shell Programming and Scripting

Freeing the terminal from busy shell script?

Hi guys, I wrote a basic inotifywait shell script on my CentOS 5.6 x86_64 test server that syncs any deleted files in a directory. /usr/bin/script #!/bin/sh inotifywait -m -e delete /home/user/test | while read file; do # log event here done The script alone works fine. However, the... (4 Replies)
Discussion started by: TECK
4 Replies

7. Shell Programming and Scripting

simulate linux shell using script

hi guys I was requested to create a script to simulate a linux shell so when the user opens linux he will get my linux menu with some options and one of those is a simulated linux shell this is my code #!/bin/ksh while do read whichcmd?"Enter Command: " $whichcmd ... (5 Replies)
Discussion started by: kopper
5 Replies

8. Shell Programming and Scripting

Set terminal width inside a shell script

Hi all, I have a shell script which uses "mailx -H" to get the subject of a email in a Linux system. However, the subject is truncated, and I think it has something to do with the terminal width because it only returns the first 80 characters of each line. I have tried "stty columns"... (7 Replies)
Discussion started by: mezzo
7 Replies

9. Shell Programming and Scripting

Problem - gnome terminal shell scripting

I've a python script named rwe.py. I'm running the program in three separate terminals. If one of the executing program stops . I want to leave the terminal as it is so that i can see the error. i wrote a the below script and used cron to run it every one hour to check if the three programs are... (0 Replies)
Discussion started by: msteve2002
0 Replies

10. Shell Programming and Scripting

Answer Terminal Questions With Shell Script?

First off, I am using Mac OS X, with Apple Remote Desktop. I have to install several app's on teachers' laptops which are on several cd's that I have made disk images of. (DMG's) To do rollouts quicker, I have written a script to mount the disk images and running the installers inside each of... (4 Replies)
Discussion started by: The Reepr
4 Replies
Login or Register to Ask a Question