Linux Executing bash scripts from a webpage

Discussion in 'Software' started by steveo_mcg, 17 Sep 2008.

  1. steveo_mcg

    steveo_mcg What's a Dremel?

    Joined:
    26 May 2005
    Posts:
    5,841
    Likes Received:
    80
    I've seen on a couple of utility distros (ipcop, geexbox) on their webgui they have a button to shutdown the system or perform specific operations ie in geex play a file in mplayer. Basicaly i think they are executing bash scripts but i might be wrong. Could some one help me understand how this is done it could be handy for my juke box and if it just a bash script how would one call it from a web page.
     
  2. deeem119

    deeem119 What's a Dremel?

    Joined:
    5 Jul 2008
    Posts:
    14
    Likes Received:
    0
    You'll likely find they're using customised versions of Apache's httpd, or perhaps Apache Tomcat. However, you can do something similar if you have apache and php installed on your machine. You can write a php script to execute commands on the server (which would be your PC) and optionally output the result to the page. The command is either "shell_exec()" or enclosing the command in backticks. Check the PHP page for more info: http://uk2.php.net/manual/en/function.shell-exec.php

    As a for-instance:

    <?php
    // execute 'ls -l', save the results in $comout, echo it out to the page
    $comout = shell_exec('ls -l');
    echo "<pre>$comout </pre>";
    ?>

    This will execute a directory list, and output that list to your web page. The link I gave also has links to similar commands that might be useful, depending on what you wanted to do.
     
  3. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    yup.
    shellexec() is the right way to do shell commands on a 'nix box through PHP.
    HOWEVER, there's a HUGE security risk involved with the whole thing (potentially). But i assume you want to use this for your table-based PC mod?
     
  4. steveo_mcg

    steveo_mcg What's a Dremel?

    Joined:
    26 May 2005
    Posts:
    5,841
    Likes Received:
    80
    Its for a different project but yeah its on my internal network no where near the internet.
     
  5. koola

    koola Minimodder

    Joined:
    11 Jul 2004
    Posts:
    2,401
    Likes Received:
    10
    While it's not using bash, it's sometimes more powerful to use PHP itself. Here's the recursive function I use on my intranet.

    PHP:
    function scanFilesystem($dir) {
      
    $itemArray scandir($dir);

      for(
    $i=2$i<count($itemArray); $i++) {
        
    $item $itemArray[$i];
        
    $newDir $dir "/" $item;

        if(
    is_dir($newDir)) {
          
    $tempArray[$item] = scanFilesystem($newDir);
          
    asort($tempArray);
        }
        else {
          
    $tempArray[] = $item;
        }

      }

      return 
    $tempArray;
    }
    Assuming the following directory structure:

    Code:
    root/html
    root/html/index.php
    root/html/about.php
    root/html/archives.php
    root/html/images/logo.png
    root/html/images/photo.png
    root/html/images/icons/arrow.png
    root/html/images/icons/pencil.png
    root/html/images/icons/paper.png
    root/html/style/style.css
    
    Then calling scanFilesystem() as follows:

    PHP:
    $dir "root/html";
    $dirStructure scanFilesystem($dir);
    print_r($dirStructure);
    Will give the output:

    PHP:
    Array
    (
        [
    0] => about.php
        
    [1] => archives.php
        
    [2] => index.php
        
    [images] => Array
            (
                [
    0] => logo.png
                
    [1] => photo.png
                
    [icons] => Array
                    (
                        [
    0] => arrow.png
                        
    [1] => pencil.png
                        
    [2] => paper.png
                    
    )
            )

        [
    style] => Array
            (
                [
    0] => style.css
            
    )
    )
    Now just print the array using a foreach/for loop et volia.

    Good for displaying mp3 etc..
     
  6. Jasio

    Jasio Made in Canada

    Joined:
    27 Jun 2008
    Posts:
    810
    Likes Received:
    13
    Perl/CGI is able to execute command-line/bash commands to a certain degree. For example:

    Code:
    open(TAR, "/bin/tar -zxvf some-file.tar.gz |") || die "Could not extract files ($!)\n";
    
    [b]or call system commands like so:[/b]
    
    system("/bin/rm -rf some/path/somefolder/");
    
     
    Last edited: 20 Sep 2008
  7. Fophillips

    Fophillips What's a Dremel?

    Joined:
    9 Oct 2006
    Posts:
    948
    Likes Received:
    1
    Why are you recursively removing a single file? -r is for deleting directories.
     
  8. Jasio

    Jasio Made in Canada

    Joined:
    27 Jun 2008
    Posts:
    810
    Likes Received:
    13
    My bad, didn't check what I wrote. Shouldn't be .tar.gz -- should just be a folder.
     
Tags:

Share This Page