Αναζήτηση αυτού του ιστολογίου

Javascript & PHP Run Shell Commands

Execute Shell commands by browser.
http://superuser.com/questions/292818/run-a-shell-script-with-an-html-button
Javascript in browsers seems that is not allowing the executing of local scripts/commands in the client side due to obvious safety issues.
PS: Linux includes JavaScript as a stand alone application, to be run locally. In this case seems it is possible to execute shell commands.

On your browser, in server side, you can execute shell commands by JS but using PHP commands. This means that your machine / server should be set up and running PHP.

PS: There are some workarounds to run php locally without installing a full and complete web server but you have at least to install PHP (some how as a stand alone application).

In this server side case, you can call php commands by JS (call - run - exit php on the fly) like this within a JScript:

More PHP Scripts to run shell commands on your Apache server from your browser, using shell_exec or exec commands:

<?php
if (isset($_POST['shellexec']))
{
 $dirlist = shell_exec($_POST['command1']);
    echo '<pre>';
    #print_r($dirlist);
    echo htmlentities($dirlist); #echo works here well. Htmlentities work fine either you use ls -all, or cat file.html
    echo '</pre>';
    UNSET($_POST);
}

if (isset($_POST['exec2']))
{
 exec($_POST['command2'],$dirlist2,$retval);
    echo '<pre>';
 #print_r($dirlist2); #this works fine, but if you try to "cat" an html/php file , this is executedd on your browser...
 #moreover htmlentities doesn't work with print_r method (due to array).
    $result=print_r($dirlist2,true);
    #echo $dirlist2 -> echo doesn't work with arrays!
    echo htmlentities($result); #this works ok since you load print_r in $result variable.
    print_r($retval);
    echo '</pre>';
    unset ($_POST);
}

?>
<form action="" method="post">
    Command: <input type="text" name="command1" value="">
    <button type="submit" name="shellexec"> ShellExec </button>
   
</form>

<form action="" method="post">
    Command: <input type="text" name="command2" value="">
    <button type="submit" name="exec2">exec</button>
   
</form>

Tips:
You can run almost any freebsd command , depending on the permission rights.
for example :
ls -all , pwd, host , date, ifconfig, etc.
You can use cat filename to quickly display the contents of any server file in your browser
You can use cp or rm (copy or delete files) if you have the permission to do these actions.
You can combine commands in one line like "cd ..; pwd; ls -all".
Notice that when you reload the page, you are driven back to the directory that above script exists.

PS: Be carefull not to destroy your server...