Executing a bash script using php exec and javascript

I am trying to run this javascript below but cannot see the php shell_exec command output.

Running the test -a bash script will print a series of ID 34535, 25643, 23262, etc. When I run it in my php file with a simple one, it works fine.

print shell_exec('/opt/bin/echkchunk -a'); 

But when I try to run it below, and I select test1, nothing is displayed. Looking through the chromes developer tools, I see the code when test1 is selected as

<!--?php shell_exec("/opt/bin/echkchunk -a"); ?-->

as if he was commenting out.

So my question is: is it possible to run a bash script using php and JavaScript? Or is there another way to get this information displayed on a web page without JavaScript?

 <script type="text/javascript"> var tester = '<?php shell_exec("/opt/bin/test -a"); ?>'; $(document).ready(function() { $("#selector").on("change", function() { if ($("#selector").val() == "test1") { $("#rightselection").css("background-color", "red"); $("#rightselection").html(tester); } if ($("#selector").val() == "test2") { $("#rightselection").css("background-color", "blue"); $("#rightselection").html("test2"); } if ($("#selector").val() == "test3"){ $("#rightselection").css("background-color", "yellow"); $("#rightselection").html("test3"); } if ($("#selector").val() == ""){ $("#rightselection").css("background-color", "green"); $("#rightselection").html("This is a Test"); } }); }); </script> 
+4
source share
2 answers

Try running the command by copying the output to standard output:

 <?php shell_exec("/opt/bin/echkchunk -a 2>&1"); ?> 

shell_exec returns null if an error occurs when running the command using 2>&1 , you get all the output, even if the command does not work.

See the sh command: exec 2> & 1

+1
source

If your result is multi-line, you need to replace the newline marker with an escape sequence or HTML. Try replacing

 var tester = '<?php shell_exec("/opt/bin/test -a"); ?>'; 

with

 var tester = '<?php echo str_replace(PHP_EOL, '<br/>', shell_exec("/opt/bin/test -a")); ?>'; 
+1
source

Source: https://habr.com/ru/post/1439985/


All Articles