<style type="text/css" scoped>
li { margin: 5px; }
.server_output_text {
background-color:black; color:white;
max-width:800px; margin:0 auto;
max-height:500px;
font-family: monospace; white-space:pre;
padding:5px;
overflow-x: scroll; overflow-y: scroll;
}
.server_output_head { font-weight:bold; color:yellow; }
</style>
<script type="text/javascript">
function checkInput() {
sCmd = document.getElementById('RemCommand').value;
if (sCmd == '') {
sErrText = "<div class='bg-danger' style='padding:3px'><strong>FEHLER:</strong> Es wurde kein Kommando eingegeben. </div>";
document.getElementById('fehlerText').innerHTML = sErrText;
return(false);
}
return(true);
}
</script>
<?php
define( "MAXTIME", 20); //mehr als 20 sekunden soll das Skript nicht brauchen
$isWindows = strstr(strtoupper(PHP_OS), strtoupper("Win"));
//Wir wollen dieses Skript rekursiv aufrufen und die Zeit beschränken
if (!( $sScriptName = substr(strrchr($_SERVER['SCRIPT_NAME'], SLASH),1))) $sScriptName = $_SERVER['SCRIPT_NAME'];
set_time_limit(MAXTIME);
$CmdPrefix = StripSlashes(HTML_get_request_field('CmdPrefix'));
$RemCommand = StripSlashes(HTML_get_request_field('RemCommand'));
$cbxPrefix = (HTML_get_request_field('cbxPrefix')) ? "checked='checked'" : '';
$cbxVardump = (HTML_get_request_field('cbxVardump')) ? "checked='checked'" : '';
$phpCommand = HTML_get_request_field('phpCommand');
if ($phpCommand == 'shell_exec') {
$radioShellExec = "checked='checked'";
$radioExec = "";
} else {
$phpCommand = 'exec'; //default
$radioShellExec = "";
$radioExec = "checked='checked'";
}
$FirstCall = (HTML_get_request_field('FirstCall')) ? false : true;
if ($FirstCall) $CmdPrefix = ($isWindows) ? "cmd /c" : LEER;
echo "<div class='jumbotron'><h1>" . menuGetPageTitle(false) . "</h1></div>";
if ( $FirstCall ) { /******* Sonst nur einen einfachen Text ausgeben *******/ ?>
<div class="row">
<div class="col-sm-1 col-sm-offset-1">
<img class="img-responsive" src="http://NetAktiv.de/images/stop_klein.gif" alt="Stopp">
</div>
<div class="col-sm-8">
<p>Bei dem Ausführen der Kommandos sind verschiedene Dinge zu beachten:</p>
<ul>
<li>Die Ausführung erfolgt im Kontext des Web-Servers, fehlende Privilegien könnendie Durchführung verhindern.</li>
<li>Man darf keine Kommandos eingeben, die eine Eingabe des Benutzers erwarten, dann hängt der Vorgang.</li>
<li>
Bei manchen Betriebssystemen wie Windows muss zumindest vor internen Kommandos
(wie Dir) der Kommandoointerpreter aufgerufen werden, der nach der Ausführung aber die
Kontrolle wieder an den rufenden Prozess gibt (unter Windows NT/2000 lautet das Kommando
dann zum Beispiel <code>cmd /c dir</code> (statt nur dir)
</li>
<li>
Einige wenige Kommandos, die doch nur eine einfache Eingabe erwarten, kann man Aufrufen
indem man die Eingabe mittels Pipe übergibt, Beispiel (Windows NT)
<code>cmd /c echo. | date</code>
</li>
<li>Will man mehrere Kommandos ausführen, so muss man ein Skript hochladen (eventuell spätere Version).</li>
<li>Die Form erlaubt, vor jedem Kommando ein festes Prefix einzufüge (beispielsweise <code>cmd /c</code>).</li>
<li>Unter Linux/Unix findet man durch Eingabe von <code>df -kl</code> alle Festplatten.</li>
<li>Wir haben die beiden PHP shell Befehle <code>exec</code> und <code>shell_exec</code> implementiert, für die Unterschiede siehe
beispielweise <a href="http://stackoverflow.com/questions/7093860/php-shell-exec-vs-exec">php-shell-exec-vs-exec</a>
bei stackoverflow. Beim Befehl <code>exec</code> erfolgt die Ausgabe in ein Array, dessen Inhalt kann optional auch als var_dump ausgegeben werden.
</li>
</ul>
<hr class="hr-rr-def"/>
</div>
</div>
<?php } /******* Ende der Ausgabe der Hilfe bei FirstCall *******/ ?>
<form class="form-horizontal" method="POST" action="<?php echo $sScriptName ?>" onsubmit="return(checkInput());">
<input type="hidden" name="FirstCall" value="FALSE"/>
<input type="hidden" name="page" value="server_cmd" />
<div class="form-group">
<label class="col-sm-1 col-sm-offset-2">Exec:</label>
<div class="col-sm-2">
shell_ecec <input type="radio" name="phpCommand" value="shell_exec" <?php echo $radioShellExec; ?> />
<input type="radio" name="phpCommand" value="exec" <?php echo $radioExec; ?> /> exec
</div>
<div class="col-sm-2"><input type="checkbox" id="cbxVardump" name="cbxVardump" value="ON" <?php echo $cbxVardump; ?>/> var_dump</div>
</div>
<div class="form-group">
<label class="col-sm-1 col-sm-offset-2" for="CmdPrefix">Prefix:</label>
<div class="col-sm-2"><input class="form-control input-sm" type="text" name="CmdPrefix" <?php echo 'value="' . $CmdPrefix . '"'; ?>/></div>
<div class="col-sm-2"><input type="checkbox" id="cbxPrefix" name="cbxPrefix" value="ON" <?php echo $cbxPrefix; ?>/> verwenden</div>
</div>
<div class="form-group">
<label class="col-sm-1 col-sm-offset-2" for="RemCommand">Cmd $:</label>
<div class="col-sm-4"><input class="form-control input-sm" type="text" id="RemCommand" name="RemCommand" value="<?php echo $RemCommand; ?>"/></div>
<div class="col-sm-2"><input type="submit" value="Ausführen" name="B1" class="btn btn-default btn-sm"/></div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-3" id="fehlerText"></div>
</div>
</form>
<!-- Ausgabe des Kommandos -->
<?php
if ($RemCommand) {
if (($cbxPrefix) && ($CmdPrefix)) $RemCommand = $CmdPrefix . BLANK . $RemCommand;
/******* Ausgabe und Ausführung nur wenn ein Kommando übergeben wurde *******/
echo "<div class='server_output_text'>";
echo "<span class='server_output_head'>PHP $phpCommand Command\$: $RemCommand</span>\n\n";
if ($radioShellExec) {
echo shell_exec( $RemCommand);
} else {
$iResult = 0;
if (exec( $RemCommand, $sArrResult, $iResult)) echo implode("\n", $sArrResult);
$sError = ( $iResult != 0 ) ? "FEHLER (Code $iResult)" : "OK";
echo "\n\n<span class='server_output_head'><strong>Status: </strong> $sError</span>";
if ($cbxVardump) echo "\n\n<span class='server_output_head'>Ausgabe von var_dump:</span>\n" . print_r($sArrResult, true);
}
echo "</div>";
}
?>