View Full Version : PHP+shell execute Help!!!
shachi
09-04-2006, 04:18 PM
Hello all, I am trying to build a virtual shell(with PHP+AJ(AX)) for myself(personal use) and I needed to execute the commands but the problem I am having with the shell_exec() command is that every time I pass a command it executes in a completely new shell. For e.g if I execute:
cd test/
ls -la
it does not show anything though I have files in the test directory. Any help here would be appreciated. Thanks!!!
Just implode(';', $commands); and execute :)
shachi
09-04-2006, 05:36 PM
Twey, I know that *but* I need the response as soon as I hit enter and if I implode it and execute it then it should wait for two commands to execute.:(
Ah, I see. The problem is that the script terminates after every execution, destroying its execution environment. The solution is to save the environment after executing the command(s), then restore it before the next call.
function saveEnv() {
shell_exec('export > envp.txt');
}
function restoreEnv() {
shell_exec('. envp.txt');
}That will work with bash, but cause problems with other shells.
blm126
09-04-2006, 11:23 PM
wait a minute. If it destroys its execution environment that won't work will it?
Yes. saveEnv() is called before it's destroyed the environment, saving the environment to a file on the disk. Then restoreEnv() is called to restore the environment at the beginning of the next time the script is called. The environment is only destroyed when the script exits, not at the end of each shell_exec() call.
blm126
09-05-2006, 02:13 AM
ah, I see. I thought it was destroyed after each call to shell_exec()
shachi
09-05-2006, 05:07 AM
Twey: How do I use it then?? Something like this??
saveEnv();
shell_exec($_POST['command']);
restoreEnv();
??
shachi
09-05-2006, 05:23 PM
Twey: How do I *use* it???
It appears I was wrong: each shell_exec() call is run in a new instance of the shell. I'm sure it's possible to do this, but it's not simple. :)
shachi
09-06-2006, 05:10 AM
Twey: That's what the problem is.:)
I think we should first append all the commands given by the user in a text file, implode it in an array and execute it everytime.
shachi
09-08-2006, 06:26 PM
Hello??? Any help here??
blm126
09-08-2006, 07:53 PM
Twey: That's what the problem is.:)
I think we should first append all the commands given by the user in a text file, implode it in an array and execute it everytime.
You seemed to have this well in hand. I don't enough about shell stuff to help
shachi
09-09-2006, 11:35 AM
No one??
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.