stealthrt Posted September 13, 2022 Share Posted September 13, 2022 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: But like I said, it doesn't give any errors. I tested a simple ls command with it and it works as expected: So, what am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/315317-php-shell_exec-uci-not-working-but-without-errors/ Share on other sites More sharing options...
kicken Posted September 13, 2022 Share Posted September 13, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315317-php-shell_exec-uci-not-working-but-without-errors/#findComment-1600468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.