Basically, I want to run below script locally in order start stop restart and get status of service. And it must show output of each command button. I am using php 5.6 in order to run this code.
Below is the code:
<?php
// define cmds
$commands = [
'stop_apache' => [
'description' => 'Stop Apache2',
'cmd' => 'systemctl stop apache2'
],
'restart_apache' => [
'description' => 'Restart Apache2',
'cmd' => 'systemctl restart apache2'
],
'start_apache' => [
'description' => 'Start Apache2',
'cmd' => 'systemctl start apache2'
],
'status_apache' => [
'description' => 'Status Apache2',
'cmd' => 'systemctl status apache2'
],
];
// handle post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = [];
$result = '';
// validate input
if (empty($_POST['service'])) {
$error = [
'service' => 'Service type required!'
];
} elseif (!array_key_exists($_POST['service'], $commands)) {
$error = [
'service' => 'Invalid Service!'
];
}
}
?>
<form action="" method="post">
<?php if (!empty($error)): ?>
<h3>Error</h3>
<pre><?= print_r($error, true) ?></pre>
<?php endif ?>
<?php foreach ($commands as $key => $command): ?>
<button type="submit" name="service" value="<?= $key ?>"><?=
$command['description'] ?></button>
<?php endforeach ?>
</form>
<?php if (!empty($result)): ?>
<pre><?= print_r($result, true) ?></pre>
<?php endif ?>