I wrote something for you.
Code: Select all
;
; RCon
;
; idea from a C sourcecode at http://developer.valvesoftware.com/wiki/Source_RCON_Protocol
;
; V1.00 first try
; V1.01 second try
; V1.02 now RConSend() is working
; V1.03 fixed a typo in RConReceive(), added mor verbose output (I can not test this)
; V1.04 now the received data can come in pieces, better hex output (-v -v)
; also multiple answers are accepted now. (hope so)
; New parameter -i to set the ID
; V1.05 It should work now! Several changes. Tested with an emulator.
; V1.06 Now also a multipacket receive is handled correct
; V1.07 Long answer strings should be handled correct now
; V1.08 changed all string stuff to UTF8
EnableExplicit
#SERVERDATA_AUTH = 3
#SERVERDATA_AUTH_RESPONSE = 2
#SERVERDATA_EXECCOMMAND = 2
#SERVERDATA_RESPONSE_VALUE = 0
Structure RCONPacketHeaderStructure
Size.l
ID.l
Type.l
EndStructure
Global Verbose.i
Procedure HexListing(*Buffer, Length)
Protected i.i, Hex$, Byte.a, Ascii$
For i = 0 To Length - 1
Byte = PeekA(*Buffer + i)
Hex$ + RSet(Hex(Byte), 2, "0") + " "
If Byte > 31
Ascii$ + Chr(Byte)
Else
Ascii$ + "."
EndIf
If (i + 1) % 16 = 0
PrintN(Hex$ + " " + Ascii$)
Hex$ = ""
Ascii$ = ""
ElseIf (i + 1) % 8 = 0
Hex$ + "- "
EndIf
Next i
If Length % 16 <> 0
PrintN(LSet(Hex$, 50) + " " + Ascii$)
EndIf
EndProcedure
Procedure.i RConSend(Con, ID.l, Type.l, Str1$, Str2$="")
Protected Result.i, PacketSize.l, *Buffer.RCONPacketHeaderStructure
If Verbose > 1
PrintN("RConSend")
EndIf
PacketSize = SizeOf(RCONPacketHeaderStructure) + StringByteLength(Str1$, #PB_Ascii) + 1 + StringByteLength(Str2$, #PB_Ascii) + 1 ; + 1s for the terminating 0s
*Buffer = AllocateMemory(PacketSize)
If *Buffer
*Buffer\Size = PacketSize - SizeOf(Long)
*Buffer\ID = ID
*Buffer\Type = Type
PokeS(*Buffer + SizeOf(RCONPacketHeaderStructure), Str1$, -1, #PB_Ascii)
PokeS(*Buffer + SizeOf(RCONPacketHeaderStructure) + StringByteLength(Str1$, #PB_Ascii) + 1, Str2$, -1, #PB_Ascii) ; + 1 for the terminating 0 of Str1$
If Verbose > 1
PrintN("Sending:")
HexListing(*Buffer, PacketSize)
PrintN("")
EndIf
Result = SendNetworkData(Con, *Buffer, PacketSize)
FreeMemory(*Buffer)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.i RConReceive(Con, *ID, *CommandResponse, *Str1, *Str2, MaxWait)
Protected.i Result, *Buffer.RCONPacketHeaderStructure, Answer.i, BuffPtr.i, Received.i, Length.i
If Verbose > 1
PrintN("RConReceive")
EndIf
*Buffer = AllocateMemory(8192)
If *Buffer
Answer = #False
Repeat
Select NetworkClientEvent(Con)
Case #PB_NetworkEvent_None
Delay(10)
MaxWait - 10
Case #PB_NetworkEvent_Data
Length = ReceiveNetworkData(Con, *Buffer + BuffPtr, 8192 - BuffPtr)
If Length > 0
Received + Length
If Verbose > 1
PrintN("Received:")
HexListing(*Buffer + BuffPtr, Length)
PrintN("")
EndIf
If Not Result And Length > 3
Result = *Buffer\Size
EndIf
If Received >= Result + 4
PokeL(*ID, *Buffer\ID)
PokeL(*CommandResponse, *Buffer\Type)
PokeS(*Str1, PeekS(*Buffer + SizeOf(RCONPacketHeaderStructure), -1, #PB_Ascii), -1, #PB_Ascii)
PokeS(*Str2, PeekS(*Buffer + SizeOf(RCONPacketHeaderStructure) + StringByteLength(PeekS(*Buffer + 12, -1, #PB_Ascii), #PB_Ascii) + 1), -1, #PB_Ascii)
Answer = #True
EndIf
BuffPtr + Length
EndIf
Case #PB_NetworkEvent_Disconnect
If Verbose
PrintN("Receive disconnect!")
EndIf
Break
EndSelect
Until MaxWait <= 0 Or Answer
FreeMemory(*Buffer)
If Verbose
If MaxWait < 0
PrintN("Receive timeout!")
EndIf
EndIf
If Not Answer
Result = 0
EndIf
EndIf
ProcedureReturn Result
EndProcedure
Procedure RConProcessResponse(Con, OurID, CommandExpectedAnswer)
Protected Result.i, ID.l, CommandResponse.l, *Str1, *Str2
Result = -1
*Str1 = AllocateMemory(4096)
If *Str1
*Str2 = AllocateMemory(4096)
If *Str2
If Verbose > 1
PrintN("RConProcessResponse")
EndIf
While RConReceive(Con, @ID, @CommandResponse, *Str1, *Str2, 1000) > 0
If Verbose
PrintN("Received something")
EndIf
If CommandResponse = CommandExpectedAnswer
Select CommandResponse
Case #SERVERDATA_AUTH_RESPONSE
If ID = OurID
Result = 1
ElseIf ID = -1
PrintN("Password refused")
Else
PrintN("Bad Auth Response ID: " + Str(ID))
EndIf
Case #SERVERDATA_RESPONSE_VALUE
PrintN("RCon answer:")
PrintN(PeekS(*Str1, -1, #PB_Ascii))
Result = 2
Default
PrintN("Unexpected command response: " + Str(CommandResponse))
EndSelect
Break
EndIf
Wend
FreeMemory(*Str2)
EndIf
FreeMemory(*Str1)
EndIf
ProcedureReturn Result
EndProcedure
Procedure Usage()
PrintN("")
PrintN("RCon V1.08 (c) Infratec")
PrintN("")
PrintN("usage: RCon -s server -p password command [-i ID][-P port][-h][-v]")
PrintN("")
PrintN(" -s server : the name or the IP address of the server")
PrintN(" -p password : the password for authentication")
PrintN(" command : what you want to send")
PrintN(" -i ID : the ID you want to use (default is 0)")
PrintN(" -P port : the port you want to use (default is 27015)")
PrintN(" -h : shows this help")
PrintN(" -v : increase verbosity")
PrintN("")
End
EndProcedure
Define Port.i, Passwd$, Server$, Command$, OurID.i, i.i,Parameter$, Con.i
Port = 27015
Server$ = "127.0.0.1"
If OpenConsole()
If InitNetwork()
For i = 0 To CountProgramParameters() - 1
Parameter$ = ProgramParameter(i)
If FindString(Parameter$, "-h", 1)
Usage()
ElseIf FindString(Parameter$, "-v", 1)
Verbose + 1
ElseIf FindString(Parameter$, "-s", 1)
i + 1
Server$ = ProgramParameter(i)
ElseIf FindString(Parameter$, "-p", 1)
i + 1
Passwd$ = ProgramParameter(i)
ElseIf FindString(Parameter$, "-i", 1)
i + 1
OurID = Val(ProgramParameter(i))
ElseIf FindString(Parameter$, "-P", 1)
i + 1
Port = Val(ProgramParameter(i))
Else
Command$ = Parameter$
EndIf
Next i
If Verbose > 0
PrintN("")
PrintN("Server : " + Server$)
PrintN("Port : " + Str(Port))
PrintN("Password: " + Passwd$)
PrintN("Command : " + Command$)
PrintN("ID : " + Str(OurID))
PrintN("Verbose : " + Str(Verbose))
PrintN("")
EndIf
Con = OpenNetworkConnection(Server$, Port, #PB_Network_TCP)
If Con
If RconSend(Con, OurID, #SERVERDATA_AUTH, Passwd$) > 0
If RConProcessResponse(Con, OurID, #SERVERDATA_AUTH_RESPONSE) = 1
PrintN("Password accepted")
If RConSend(Con, OurID, #SERVERDATA_EXECCOMMAND, Command$) > 0
If RConProcessResponse(Con, OurID, #SERVERDATA_RESPONSE_VALUE) <> 2
PrintN("No answer received for: " + Command$)
EndIf
Else
PrintN("Was not possible to send: " + Command$)
EndIf
Else
PrintN("Authentication failed")
EndIf
Else
PrintN("Send failed")
EndIf
CloseNetworkConnection(Con)
Else
PrintN("Was not able to establish a connection to " + Server$ + " at port " + Str(Port))
EndIf
EndIf
CloseConsole()
EndIf
put the working result here in the forum.