Python:HL2Rcon
From Devicenull's Code
This is a very basic implementation of the HL2 RCon protocol. There are better ways of doing this.
import string, re, time, socket, xdrlib def PackInt(Num): packer = xdrlib.Packer() packer.pack_int(Num) return packer.get_buffer()[::-1] def UnPackInt(Num): packer = xdrlib.Unpacker(Num[::-1]) return packer.unpack_int() class CHL2Rcon: """This will connect to a Half Life 2 Server and send/recieve RCON commands""" def __init__(self,server,port,password): self.srv_addr = server self.srv_port = port self.srv_pass = password self.RCON_AUTH = 3 self.RCON_EXEC = 2 self.RCON_AUTH_RESP = 2 self.RCON_EXEC_RESP = 0 self.req_id = 1 self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._s.connect((server,port)) self.Auth() def Auth(self): self.SendPacket(self.RCON_AUTH,self.srv_pass) # First resposne is garbage self.ReadPacket() # Second packet contains the result self.ReadPacket() def Execute(self,Command): self.SendPacket(self.RCON_EXEC,Command) pck = self.ReadPacket() buf = pck[2][0] try: self._s.settimeout(0.5) str = self._s.recv(1024) while len(str) > 0: buf = buf + str str = self._s.recv(1024) except: pass return buf def SendPacket(self, Command, String1, String2=''): self.req_id += 1 packer = xdrlib.Packer() buf = String1 + '\x00' + String2 + '\x00' buf = PackInt(Command) + buf buf = PackInt(self.req_id) + buf buf = PackInt(len(buf)) + buf self._s.send(buf) def ReadPacket(self): # retrieve the packet length packetLen = UnPackInt(self._s.recv(4)) # retrieve the request ID req_id = UnPackInt(self._s.recv(4)) # retrieve the command response cmd = UnPackInt(self._s.recv(4)) # retrieve the response buf = self._s.recv(packetLen) return (cmd,req_id,buf.split("\x00"))