RCON VIA PHP

Discussion in 'Bukkit Help' started by NathanDTaylor, Oct 11, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    NathanDTaylor

    Hi, I have RCON set up on my server... the following lines are in my server.properties.
    Code:
    enable-rcon=true
    rcon.port=25575
    rcon.password=PASSWORD
    I can connect with an RCON Client just fine and send commands through to the server which means the ports must be open. (I use a host). But whenever I try to use a PHP script that sends RCON to minecraft I get the following error
    Code:
    Unable to open socket: Connection timed out (110)
    Not sure if it is an issue with the script or what, because the connection is obviously there.
     
  2. Offline

    iScottien

    Paste the script you are using please.

    Scott
     
  3. Offline

    NathanDTaylor

    It is a script I found online. The frist file is for CS:S but in the second file, it is supposed to extend it for minecraft. If you know any other script, feel free to share.

    RCON.CLASS.PHP
    Code:
    <?php
    /*
        Basic CS:S Rcon class by Freman.  (V1.00)
        ----------------------------------------------
        Ok, it's a completely working class now with with multi-packet responses
     
        Contact: printf("%s%s%s%s%s%s%s%s%s%d%s%s%s","rc","on",chr(46),"cl","ass",chr(64),"pri","ya",chr(46),2,"y",chr(46),"net")
     
        Behaviour I've noticed:
            rcon is not returning the packet id.
    */
     
    define("SERVERDATA_EXECCOMMAND",2);
    define("SERVERDATA_AUTH",3);
     
    class RCon {
        var $Password;
        var $Host;
        var $Port = 25575;
        var $_Sock = null;
        var $_Id = 0;
     
        function RCon ($Host,$Port,$Password) {
            $this->Password = $Password;
            $this->Host = $Host;
            $this->Port = $Port;
            $this->_Sock = @fsockopen($this->Host,$this->Port, $errno, $errstr, 30) or
                    die("Unable to open socket: $errstr ($errno)\n");
            $this->_Set_Timeout($this->_Sock,2,500);
            }
     
        function Auth () {
            $PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);
     
            // Real response (id: -1 = failure)
            $ret = $this->_PacketRead();
            if ($ret[1]['id'] == -1) {
                die("Authentication Failure\n");
            }
        }
     
        function _Set_Timeout(&$res,$s,$m=0) {
            if (version_compare(phpversion(),'4.3.0','<')) {
                return socket_set_timeout($res,$s,$m);
            }
            return stream_set_timeout($res,$s,$m);
        }
     
        function _Write($cmd, $s1='', $s2='') {
            // Get and increment the packet id
            $id = ++$this->_Id;
     
            // Put our packet together
            $data = pack("VV",$id,$cmd).$s1.chr(0).$s2.chr(0);
     
            // Prefix the packet size
            $data = pack("V",strlen($data)).$data;
     
            // Send packet
            fwrite($this->_Sock,$data,strlen($data));
     
            // In case we want it later we'll return the packet id
            return $id;
        }
     
        function _PacketRead() {
            //Declare the return array
            $retarray = array();
            //Fetch the packet size
            while ($size = @fread($this->_Sock,4)) {
                $size = unpack('V1Size',$size);
                //Work around valve breaking the protocol
                if ($size["Size"] > 4096) {
                    //pad with 8 nulls
                    $packet = "\x00\x00\x00\x00\x00\x00\x00\x00".fread($this->_Sock,4096);
                } else {
                    //Read the packet back
                    $packet = fread($this->_Sock,$size["Size"]);
                }
                array_push($retarray,unpack("V1ID/V1Response/a*S1/a*S2",$packet));
            }
            return $retarray;
        }
     
        function Read() {
            $Packets = $this->_PacketRead();
     
            foreach($Packets as $pack) {
                if (isset($ret[$pack['ID']])) {
                    $ret[$pack['ID']]['S1'] .= $pack['S1'];
                    $ret[$pack['ID']]['S2'] .= $pack['S1'];
                } else {
                    $ret[$pack['ID']] = array(
                        'Response' => $pack['Response'],
                        'S1' => $pack['S1'],
                        'S2' =>    $pack['S2'],
                    );
                }
            }
            return $ret;
        }
     
        function sendCommand($Command) {
            $Command = '"'.trim(str_replace(' ','" "', $Command)).'"';
            $this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
        }
     
        function rconCommand($Command) {
            $this->sendcommand($Command);
     
            $ret = $this->Read();
     
            //ATM: Source servers don't return the request id, but if they fix this the code below should read as
            // return $ret[$this->_Id]['S1'];
            return $ret[0]['S1'];
        }
    }
    ?>
    TEST.PHP
    Code:
    <?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 = "#MY IP";
    $rconPort = 25575;
    $rconPass = "$MY 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!'));
    }
    ?>
    You can test it out my going here... http://www.taylorwebs.com/minecraft/rcon2/test.php
    The link takes a while to load, because it is timing out the connection.

    Anyone have RCON via PHP working on their webiste? If so, can you share your script. I mean, I concider myself a pretty good PHP developer and once I get RCON working I want to make a control panel for my server via PHP. And also a system to allow people to purchase ranks through paypal and automatically set them up with that rank. Thanks. I would like to start on this ASAP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    NathanDTaylor

    No one has RCON via PHP working?
     
  5. Offline

    cnaude

    Is your web server allowed to connect to your minecraft server on that port? Did you test the connection from the command line of your web server? You can use "telnet servername 25575" to verify that the connection works. If that does not work you might have to choose a different port.
     
  6. Offline

    NathanDTaylor

    Thanks, checking with my upper host, will update momentarily.
     
  7. Offline

    NathanDTaylor

    I got my webhost to open that port, but now my command doesn't get authenticated. I simply changed the script which said to send the script if it is authenticated, to say "not authenticated" if it wasn't, which is what is happening. Any ideas? I am using the right port/password. I am using the same ones for my RCON client on my computer and I can log in. Maybe faulty script?
     
Thread Status:
Not open for further replies.

Share This Page