John's Musings

3/5/2012

MineCraft RCON via PHP

Filed under: Minecraft,Tips / Tricks — John @ 11:58 am

Today I went looking for examples of how to talk to a minecraft server via RCON. There is not a lot out there, and that surprised me.  I did eventually find a PHP script that worked, but it required the modification of an existing class.  I don’t like modifying someone else’s source class files to make a program work, that is why PHP has the ability to extend the class.   What follows is a simple program, that extendis the verion 1.0  rcon.class.php from http://fremnet.net/article/199/source-rcon-class and connects to a minecraft 1.2.3 server and executes the console command “say”, the result is returned in a string.   I hope this example helps others out. See the example after the break.


<?php

include_once("rcon.class.php");

// Extend the rcon class to tweak it for minecraft.
class minecraftRcon extends rcon {

function mcSendCommand($Command) {
$this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
}

function mcRconCommand($Command) {
$this->mcSendcommand($Command);

$ret = $this->Read();

return $ret[$this->_Id]['S1'];
}
}

// Server connection varialbes
$server = "minecraft.example.com";
$rconPort = 123456;
$rconPass = "PASSWORD";

// Connect to the server
$r = new minecraftRcon($server, $rconPort, $rconPass);

// Authenticate, and if so, execute command(s)
if ( $r->Auth() ) {

echo "Authenticated\n";

// Send a command
var_dump($r->mcRconCommand('say Hello World!'));
}
?>

6 Comments »

  1. Awesome tutorial! I just caught you right ontime!

    Thanks,
    Max

    Comment by Max - Max's MC Servers — 3/5/2012 @ 7:41 pm

  2. I doesn’t seem to authenticate for me. The connection works and I see it’s trying to access it from the Minecraft console.

    Comment by gbrewster — 3/28/2012 @ 8:09 am

  3. Proper client with bukkit color support: http://forums.bukkit.org/threads/admin-rcon-client-for-minecraft-servers-new-version-available.70910/

    Comment by StoleMahBukkit — 4/22/2012 @ 1:36 am

  4. Just popped over to say, it’s truly awesome to see someone using something I’ve written in such a public way.

    Comment by Shannon — 5/30/2012 @ 6:00 pm

  5. I have a same problem!
    Commands aren’t run when I run the php code on page but I’ve “Rcon connection from: IP” in console!

    Comment by juniorjpdj — 6/7/2012 @ 12:43 am

  6. for me, the Auth() function needed adjustment so under ‘class minecraftRcon extends rcon {‘ just paste:

    function Auth () {
    $PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);$ret = $this->_PacketRead();
    if ($ret[0]['ID'] == -1) {die(“Authentication Failure\n”);}
    return true;
    }

    Comment by simon816 — 10/23/2012 @ 8:49 am

RSS feed for comments on this post.

Leave a comment

Login Method

OpenID

Anonymous

Powered by WordPress