• Es freut uns dass du in unser Minecraft Forum gefunden hast. Hier kannst du mit über 130.000 Minecraft Fans über Minecraft diskutieren, Fragen stellen und anderen helfen. In diesem Minecraft Forum kannst du auch nach Teammitgliedern, Administratoren, Moderatoren , Supporter oder Sponsoren suchen. Gerne kannst du im Offtopic Bereich unseres Minecraft Forums auch über nicht Minecraft spezifische Themen reden. Wir hoffen dir gefällt es in unserem Minecraft Forum!

Minecraft + RCon + PHP = Hilfe?!

mmaarrkkuuss

Minecrafter
Registriert
18 Dezember 2013
Beiträge
22
Diamanten
0
Minecraft
WolverinGER
Hallo,
Ich habe versucht mit PHP und R-Con eine Verbindung zu meinem Server aufzubauen.
Dis hat leider nicht gelappt da PHP den Code nicht versteht.
(Server: Bukkit 1.7.4)
PHP:
<?php
include_once("rcon.class.php");
$host = "XXX.XXX.XXX.XXX"; // Server IP
$port = "25555";                      // R-Con Port
$password = "XXXXXXXXX"; // R-Con Passwort
$timeout = "3";                      // Timeout
$rcon = new Rcon($host, $port, $password);
echo "hi1";
if ($rcon->connect())
{
  echo "hi";
  $rcon->send_command("say Hello World!");
}
Wenn ich "$rcon = new Rcon($host, $port, $password);" ausnehme lädt zwar die Seite aber ich habe keine R-Con Verbindung. Wo ist das Problem?!
 
Zuletzt bearbeitet:

Pappi

Schafhirte
Gesperrt
Registriert
28 Oktober 2012
Beiträge
141
Alter
40
Diamanten
0
Minecraft
Pappi / PapaHarni
Hallo,
Ich habe versucht mit PHP und R-Con eine Verbindung zu meinem Server aufzubauen.
Dis hat leider nicht gelappt da PHP den Code nicht versteht.
PHP:
<?php
$host = "XXX.XXX.XXX.XXX"; // Server IP
$port = "25555";                      // R-Con Port
$password = "XXXXXXXXX"; // R-Con Passwort
$timeout = "3";                      // Timeout
$rcon = new Rcon($host, $port, $password);
echo "hi1";
if ($rcon->connect())
{
  echo "hi";
  $rcon->send_command("say Hello World!");
}
Wenn ich "$rcon = new Rcon($host, $port, $password);" ausnehme lädt zwar die Seite aber ich habe keine R-Con Verbindung. Wo ist das Problem?!

Ist es ein fertiges rcon Script? Wenn ja ist es das richtige? Musste selbst mehrere probieren bis eines ging.

Hast du Rcon Aktiviert auf deinem Server in der server.properties? Und das Rcon Passwort angelegt dort drin?
 

Pappi

Schafhirte
Gesperrt
Registriert
28 Oktober 2012
Beiträge
141
Alter
40
Diamanten
0
Minecraft
Pappi / PapaHarni
Edit : Genau die aus deinem Edit.

PHP:
<?php
include_once("(pfad zu deiner rcon class datei).php");

$host = "XXX.XXX.XXX.XXX"; // Server IP
$port = "25555";                      // R-Con Port
$password = "XXXXXXXXX"; // R-Con Passwort
$timeout = "3";                      // Timeout
$rcon = new Rcon($host, $port, $password);
echo "hi1";
if ($rcon->connect())
{
  echo "hi";
  $rcon->send_command("say Hello World!");
}

Irgendwoher muss er ja die Class kennen erst einmal.
 

mmaarrkkuuss

Minecrafter
Registriert
18 Dezember 2013
Beiträge
22
Diamanten
0
Minecraft
WolverinGER
geht immer noch nicht (Code von der Class)
PHP:
<?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 = 25555;
    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'];
    }
}
?>
 

Pappi

Schafhirte
Gesperrt
Registriert
28 Oktober 2012
Beiträge
141
Alter
40
Diamanten
0
Minecraft
Pappi / PapaHarni
geht immer noch nicht (Code von der Class)
PHP:
<?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 = 25555;
    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'];
    }
}
?>

Das ist doch für CounterStrike Source wenn ich das richtig lese oben oO

Probier das mal :

PHP:
<?php

class MinecraftRconException extends Exception
{
	// Exception thrown by MinecraftRcon class
}

class MinecraftRcon
{
	/*
	 * Class written by xPaw
	 *
	 * Website: http://xpaw.ru
	 * GitHub: https://github.com/xPaw/PHP-Minecraft-Query
	 *
	 * Protocol: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
	 */
	
	// Sending
	const SERVERDATA_EXECCOMMAND    = 2;
	const SERVERDATA_AUTH           = 3;
	
	// Receiving
	const SERVERDATA_RESPONSE_VALUE = 0;
	const SERVERDATA_AUTH_RESPONSE  = 2;
	
	private $Socket;
	private $RequestId;
	
	public function __destruct( )
	{
		$this->Disconnect( );
	}
	
	public function Connect( $Ip, $Port = 25575, $Password, $Timeout = 3 )
	{
		$this->RequestId = 0;
		
		if( $this->Socket = FSockOpen( $Ip, (int)$Port ) )
		{
			Socket_Set_TimeOut( $this->Socket, $Timeout );
			
			if( !$this->Auth( $Password ) )
			{
				$this->Disconnect( );
				
				throw new MinecraftRconException( "Authorization failed." );
			}
		}
		else
		{
			throw new MinecraftRconException( "Can't open socket." );
		}
	}

	public function isConnected( )
	{
		if( $this->Socket )
		{
			return true;
		}
		return false;
	}
	
	public function Disconnect( )
	{
		if( $this->Socket )
		{
			FClose( $this->Socket );
			
			$this->Socket = null;
		}
	}
	
	public function Command( $String )
	{
		if( !$this->WriteData( self :: SERVERDATA_EXECCOMMAND, $String ) )
		{
			return false;
		}
		
		$Data = $this->ReadData( );
		
		if( $Data[ 'RequestId' ] < 1 || $Data[ 'Response' ] != self :: SERVERDATA_RESPONSE_VALUE )
		{
			return false;
		}
		
		return $Data[ 'String' ];
	}
	
	private function Auth( $Password )
	{
		if( !$this->WriteData( self :: SERVERDATA_AUTH, $Password ) )
		{
			return false;
		}
		
		$Data = $this->ReadData( );
		
		return $Data[ 'RequestId' ] > -1 && $Data[ 'Response' ] == self :: SERVERDATA_AUTH_RESPONSE;
	}
	
	private function ReadData( )
	{
		$Packet = Array( );
		
		$Size = FRead( $this->Socket, 4 );
		$Size = UnPack( 'V1Size', $Size );
		$Size = $Size[ 'Size' ];
		
		// TODO: Add multiple packets (Source)
		
		$Packet = FRead( $this->Socket, $Size );
		$Packet = UnPack( 'V1RequestId/V1Response/a*String/a*String2', $Packet );
		
		return $Packet;
	}
	
	private function WriteData( $Command, $String = "" )
	{
		// Pack the packet together
		$Data = Pack( 'VV', $this->RequestId++, $Command ) . $String . "\x00\x00\x00"; 
		
		// Prepend packet length
		$Data = Pack( 'V', StrLen( $Data ) ) . $Data;
		
		$Length = StrLen( $Data );
		
		return $Length === FWrite( $this->Socket, $Data, $Length );
	}
}

?>

PHP:
<?php
$host = "XXX.XXX.XXX.XXX"; // Server IP 
$port = "25555";                      // R-Con Port 
$password = "XXXXXXXXX"; // R-Con Passwort 
$timeout = "3"; 

$query = new MinecraftRcon();
$query = $query->Connect( $host, $port, $password, $timeout);
if($query->isConnected()) {
  $query->Command("say Hallo Welt!!!");
  echo "Befehl ist raus";
} else {
  echo "Fehler , keine Verbindung zum Server vorhanden.";
}
?>
 
Oben