phpexecopenwrtluciuhttpd

PHP shell_exec uci not working but without errors


Hey all I am new at doing anything regaurding PHP Shell_exec commands.

I have some UCI code that allows me to add a rule to my fireall. This works just fine in SSH but when I try running the same thing in the PHP page it does not seem to do anything and without any error lettings me know whats going on....

My code:

<?php 
try {
    $cmd = "uci add firewall rule ".
    "uci set firewall.@rule[-1].name='my iphone' ".
    "uci set firewall.@rule[-1].src='lan' ".
    "uci set firewall.@rule[-1].dest='wan' ".
    "uci set firewall.@rule[-1].src_mac='XX:XX:XX:XX:XX:XX' ".
    "uci set firewall.@rule[-1].proto='all' ".
    "uci set firewall.@rule[-1].target='REJECT' ".
    "uci set firewall.@rule[-1].enabled='1' ".
    "commit firewall ".
    "service firewall restart ";
    
    echo 'command: ',  $cmd;

    $output = shell_exec($cmd);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

echo "<pre>output: $output</pre>";
?>

I've even tried adding 2>&1 at the end:

<?php 
try {
    $cmd = "uci add firewall rule 2>&1".
    "uci set firewall.@rule[-1].name='my iphone' 2>&1".
    "uci set firewall.@rule[-1].src='lan' 2>&1".
    [more code here]

And Ive tried using only 1 uvi for the whole command:

<?php 
try {
    $cmd = "uci add firewall rule 2>&1".
    "set firewall.@rule[-1].name='my iphone' 2>&1".
    "set firewall.@rule[-1].src='lan' 2>&1".
    [more code here]

And I've tried using the same as above but without the 2>&1

<?php 
try {
    $cmd = "uci add firewall rule ".
    "set firewall.@rule[-1].name='my iphone' ".
    "set firewall.@rule[-1].src='lan' ".
    [more code here]

And lastly I've tried it with the 2>&1 just at the end of the command and with only 1 uci:

<?php 
try {
    $cmd = "uci add firewall rule ".
    "set firewall.@rule[-1].name='my iphone' ".
    "set firewall.@rule[-1].src='lan' ".
    [more code here]
    "service firewall restart 2>&1";
    [more code here]

and with each having uci:

<?php 
try {
    $cmd = "uci add firewall rule ".
    "uci set firewall.@rule[-1].name='my iphone' ".
    "uci set firewall.@rule[-1].src='lan' ".
    [more code here]
    "service firewall restart 2>&1";
    [more code here]

It first code block above outputs this on the page: enter image description here

But like I said, it doesn't give any errors.

I tested a simple ls command with it and it works as expected:

enter image description here

So, what am I missing?

UPDATE 1

I tried this as requested"

<?php 
try {
    echo("starting..");
    shell_exec("uci add firewall rule");
    echo("1</br>");
    shell_exec("uci set firewall.@rule[-1].name='my iphone'");
    echo("2</br>");
    shell_exec("uci set firewall.@rule[-1].src='lan'");
    echo("3</br>");
    shell_exec("uci set firewall.@rule[-1].dest='wan'");
    echo("4</br>");
    shell_exec("uci set firewall.@rule[-1].src_mac='XX:XX:XX:XX:XX:XX'");
    echo("5</br>");
    shell_exec("uci set firewall.@rule[-1].proto='all'");
    echo("6</br>");
    shell_exec("uci set firewall.@rule[-1].target='REJECT'");
    echo("7</br>");
    shell_exec("uci set firewall.@rule[-1].enabled='1'");
    echo("8</br>");
    shell_exec("commit firewall");
    echo("9</br>");
    shell_exec("service firewall restart");
    echo("10</br>");
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

echo "done.";
?>

Which gave me this output on the page:

enter image description here

UPDATE 2

enter image description here


Solution

  • It seems to me that each uci command is an individual line. You can not execute multiple commands with a shell_exec.

    The whole command set must be one process, so separate shell_execs will just restart the process, and the previous command will be ingnored.

    Create a script containing the commands and execute that script in shell_exec.

    Or you can use the batch command (not tested)

    $cmd1= "uci batch << EOI ".
           "add firewall rule ".
           "set firewall.@rule[-1].name='my iphone' ".
           "set firewall.@rule[-1].src='lan' ".
           "set firewall.@rule[-1].dest='wan' ".
           "set firewall.@rule[-1].src_mac='XX:XX:XX:XX:XX:XX' ".
           "set firewall.@rule[-1].proto='all' ".
           "set firewall.@rule[-1].target='REJECT' ".
           "set firewall.@rule[-1].enabled='1' ".
           "commit firewall ".
           "EOI";
    $cmd2= "service firewall restart ";
    

    And shell_exec both of them. It may require nl chars to separate the lines.