It is very simple actually. Assume we have an adder program made in C++ that add the numbers passed as arguments like this:
./adder 2 3 6
{"risk_of_disease":"11"}
If we use a server side language like PHP, then we can have a simple form like this:
<form method="post">
<input type="text" name="num1" value="<?php echo $num1; ?>">
<input type="text" name="num2" value="<?php echo $num2; ?>">
<input type="text" name="num3" value="<?php echo $num3; ?>">
<button>Submit and run executable adder</button>
</form>
And the PHP code like this:
if(isset($_POST['num1']) &&
isset($_POST['num2']) &&
isset($_POST['num3']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$exec_cmd = "../bin/adder {$num1} {$num2} {$num3}";
$json_result = shell_exec($exec_cmd);
} else {
$num1 = 0;
$num2 = 0;
$num3 = 0;
}
- The location of the executable is on
'../bin/' directory.
- The executable must have same ownership as the PHP user executing the
script page (ex.:
chown web35:client3 ./adder).
- PHP should not have
shell_exec in the disable_functions for the specific pool/user, or else it won't be able the run the program.
A complete example:
<?php
if(isset($_POST['num1']) &&
isset($_POST['num2']) &&
isset($_POST['num3']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$exec_cmd = "../bin/adder {$num1} {$num2} {$num3}";
$json_result = shell_exec($exec_cmd);
} else {
$num1 = 0;
$num2 = 0;
$num3 = 0;
}
?>
<form method="post">
<input type="text" name="num1" value="<?php echo $num1; ?>" size="5">
<input type="text" name="num2" value="<?php echo $num2; ?>" size="5">
<input type="text" name="num3" value="<?php echo $num3; ?>" size="5">
<button>Submit and run adder</button>
</form>
<?php if(isset($json_result)): ?>
<h3>JSON result from executable</h3>
<pre><?php echo $json_result; ?></pre>
<?php endif; ?>
RESULT

EDIT
For a jQuery ajax call, just create a php file with the following code:
exerun.php
if(isset($_POST['num1']) &&
isset($_POST['num2']) &&
isset($_POST['num3']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$exec_cmd = "../bin/adder {$num1} {$num2} {$num3}";
die(shell_exec($exec_cmd));
} else {
die('{"risk_of_disease":"0"}');
}
And then have your simplejQuery ajax call liek this:
<form method="post" action="exerun.php">
<input type="text" name="num1" value="0" size="5">
<input type="text" name="num2" value="0" size="5">
<input type="text" name="num3" value="0" size="5">
<button type="submit">Submit and run adder</button>
</form>
<h3>JSON result from executable</h3>
<textarea id="result" cols="30"></textarea>
<script type="text/javascript">
jQuery(function($) {
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(response) {
$('#result').text(response);
}
});
});
});
</script>
UPDATE
If you want to run an AJAX call without a submit form, then you can pass the arguments directly as an object and have the url manually assigned like this:
<button id="execall">Run AXAJ remote exec</button>
<h3>JSON result from executable</h3>
<textarea id="result" cols="30"></textarea>
<script type="text/javascript">
jQuery(function($) {
$('#execall').on('click', function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: 'exerun.php',
data: {
'num1': 10,
'num2': 20,
'num3': 30
},
success: function(response) {
$('#result').text(response);
}
});
});
});
</script>