Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: PHP+shell execute Help!!!

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP+shell execute Help!!!

    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:

    Code:
    cd test/
    ls -la
    it does not show anything though I have files in the test directory. Any help here would be appreciated. Thanks!!!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Just implode(';', $commands); and execute
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Code:
    function saveEnv() {
      shell_exec('export > envp.txt');
    }
    
    function restoreEnv() {
      shell_exec('. envp.txt');
    }
    That will work with bash, but cause problems with other shells.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    wait a minute. If it destroys its execution environment that won't work will it?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    ah, I see. I thought it was destroyed after each call to shell_exec()

  8. #8
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey: How do I use it then?? Something like this??

    Code:
    saveEnv();
    shell_exec($_POST['command']);
    restoreEnv();
    ??

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey: How do I *use* it???

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •