Jump to content

PHP shell_exec uci not working but without errors


stealthrt
 Share

Recommended Posts

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

I have some UCI code that allows me to add a rule to my firewall. 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 I've tried using only 1 uci 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: 

image.png.7ce05eb878b165875251947241c12193.png

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

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

image.png.ed0df3368fe853d3c1dee6c990f2fc5e.png

So, what am I missing?

Link to comment
Share on other sites

You don't have any new lines in your command string, so when shell_exec pass it to the shell it'll be as if you'd typed it all out on a single line in your shell, e.g.:

blah@example:$: uci add firewall rule uci set firewall.@rule[-1].name='my iphone' ... service firewall restart

Presumably that'd just result in some error as it's not really valid.

Add some new lines or semi-colons to your command string to separate the commands and try again.  If it still doesn't work, make an array of individual commands and run them one by one in a loop.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.