Last active
February 24, 2023 09:01
-
-
Save fr34kyn01535/d242ba5eada818fe16567560d8ff01d5 to your computer and use it in GitHub Desktop.
Quick'n dirty PHP client for RocketMod RCON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RocketRCON { | |
private $socket; | |
private $connected = false; | |
public $errstr; | |
function connect($ip,$port,$password,$timeout = 5) { | |
try{ | |
$this->socket = fsockopen($ip, intval($port), $errno, $errstr, $timeout); | |
if(!$this->socket) { | |
$this->errstr = $errstr; | |
return false; | |
} | |
else{ | |
$this->connected = true; | |
$this->login($password); | |
return true; | |
} | |
}catch(Exception $e){ | |
$this->errstr = $e->getMessage(); | |
return false; | |
} | |
} | |
private function login($password){ | |
if(!$this->connected) return; | |
fputs($this->socket, "login " . $password."\r\n"); | |
} | |
function send($command){ | |
if(!$this->connected || !$command) return; | |
fputs($this->socket, $command."\r\n"); | |
} | |
public function receive(){ | |
$buffer = fread($this->socket,1024); | |
return $buffer; | |
} | |
function __destruct(){ | |
$this->disconnect(); | |
} | |
function disconnect(){ | |
if(!$this->connected) return; | |
fputs($this->socket, "quit\r\n"); | |
$this->receive(); | |
fclose($this->socket); | |
$this->connected = false; | |
} | |
} | |
$r = new RocketRCON(); | |
$r->connect($argv[1],$argv[2],$argv[3]) or die($r->errstr); | |
while(true){ | |
usleep(1000); | |
echo "< ".trim($r->receive())."\r\n"; | |
echo "> "; | |
$cmd = trim(fgets(STDIN)); | |
if($cmd == "exit") break; | |
$r->send($cmd); | |
} | |
$r->disconnect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment