<?php
$serverAddress = 'ip'; // Replace with the IP address of your remote server
$sshUsername = 'root'; // Replace with your SSH username
$sshPassword = 'pass'; // Replace with your SSH password
$minecraftDir = 'path';
// Connect to the remote server via SSH
$connection = ssh2_connect($serverAddress, 22);
if (!$connection) {
die("SSH connection failed.");
}
// Authenticate with the SSH credentials
if (!ssh2_auth_password($connection, $sshUsername, $sshPassword)) {
die("SSH authentication failed.");
}
// Open a shell
$shell = ssh2_shell($connection, 'xterm');
if (!$shell) {
die("Failed to open shell.");
}
// Check if the Minecraft server is online
fwrite($shell, "pgrep -a java | grep serwer.jar\n");
sleep(1);
$output = stream_get_contents($shell);
if (strpos($output, "serwer.jar") !== false) {
echo "Minecraft server is online." . PHP_EOL;
} else {
echo "Minecraft server is offline. Starting it..." . PHP_EOL;
// Start the Minecraft server
fwrite($shell, "cd $minecraftDir && screen java -Xmx1G -Xms1G -jar serwer.jar nogui");
sleep(5); // Wait for the server to start
// Check again if the server is online
fwrite($shell, "pgrep -a java | grep serwer.jar\n");
sleep(1);
$output = stream_get_contents($shell);
if (strpos($output, "serwer.jar") !== false) {
echo "Minecraft server has been started." . PHP_EOL;
} else {
echo "Failed to start Minecraft server." . PHP_EOL;
}
}
// Close the SSH connection and shell
fclose($shell);
fclose($connection);
?>
root@ip13:~# screen -r There is no screen to be resumed. root@ip13:~#
I have problem with ssh2 because it says that the server is online but neither my terminal or Minecraft server is not online and I dont know how to fix it.
I need that code to be able to start a server and be visible in terminal so i can execute server commands from terminal,