0

I am trying to execute ssh2 commands via a ajax post method. It always returns Code 500. I tested the ajax call without ssh2 and in different directories and it worked. I also tried with using $_POST['port'] in the startBot function. What am I doing wrong? (edit.php is the current file)

My PHP

require_once("../tools/sql.php");

if (!isset($_GET['uid'])) {
    header("Location: overview");
}

$botID = $_GET['uid'];

$botResult = $pdo->query("SELECT * FROM bot WHERE id=" . $botID);
$botrow = $botResult->fetch();
$qport = $botrow['queryport'];

function executeSSH($commandLines) {
    global $sshsshhost, $sshsshuser, $sshsshpass;

    $connection = ssh2_connect($sshsshhost, 22);
    ssh2_auth_password($connection, $sshsshuser, $sshsshpass);

    for($i = 0; $i < count($commandLines); $i++) {
        ssh2_exec($connection,  $commandLines[$i]);
    }

    ssh2_disconnect($connection);
}

if (isset($_POST['startBot'])) {
    // I also tried using $qport = $_POST['port'] here
    executeSSH(['cd /home/botsv2/bot' . $qport . '/ && screen -d -m -S bot' . $qport . ' mono /home/botsv2/bot' . $qport . '/TS3AudioBot.exe']);
}

AJAX

    $('#startBot').click(function (e) {
        e.preventDefault();
        $("#startBot,#stopBot,#restartBot,#deleteBot,#saveChanges").prop('disabled', true);

        $.ajax({
            type: "POST",
            url: 'edit.php?uid=' <?php echo $qport; ?>,
            data: {
                startBot: "true",
                port: <?php echo $qport; ?>
            },
            success: function () {
                $("#stopBot,#restartBot,#deleteBot,#saveChanges").prop('disabled', false);
            },
            error: function(xhr, status, error) {
                alert(xhr.responseText);
            }
        });
    });

0 Answers0