|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# LinuxGSM rcon.py module |
| 4 | +# Author: MicLieg |
| 5 | +# Contributors: http://linuxgsm.com/contrib |
| 6 | +# Website: https://linuxgsm.com |
| 7 | +# Description: Allows sending RCON commands to different gameservers. |
| 8 | + |
| 9 | +import argparse |
| 10 | +import socket |
| 11 | +import struct |
| 12 | +import sys |
| 13 | + |
| 14 | + |
| 15 | +class PacketTypes: |
| 16 | + LOGIN = 3 |
| 17 | + COMMAND = 2 |
| 18 | + |
| 19 | + |
| 20 | +class Rcon: |
| 21 | + |
| 22 | + def __init__(self, arguments): |
| 23 | + self.arguments = arguments |
| 24 | + self.connection = None |
| 25 | + |
| 26 | + def __enter__(self): |
| 27 | + self.connect_to_server() |
| 28 | + return self |
| 29 | + |
| 30 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 31 | + if self.connection: |
| 32 | + self.connection.close() |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def fatal_error(error_message, error_code): |
| 36 | + sys.stderr.write(f'ERROR: {error_code} {error_message}\n') |
| 37 | + sys.exit(error_code) |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def exit_success(success_message=''): |
| 41 | + sys.stdout.write(f'OK: {success_message}\n') |
| 42 | + sys.exit(0) |
| 43 | + |
| 44 | + def connect_to_server(self): |
| 45 | + self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 46 | + self.connection.settimeout(self.arguments.timeout) |
| 47 | + |
| 48 | + try: |
| 49 | + self.connection.connect((self.arguments.address, self.arguments.port)) |
| 50 | + except socket.timeout: |
| 51 | + self.fatal_error('Request timed out', 1) |
| 52 | + except Exception as e: |
| 53 | + self.fatal_error(f'Unable to connect: {e}', 1) |
| 54 | + |
| 55 | + def send_packet(self, request_id, packet_type, data): |
| 56 | + # Packet structure follows the Source RCON Protocol: size, request ID, type, data, two null bytes |
| 57 | + packet = ( |
| 58 | + struct.pack('<l', request_id) |
| 59 | + + struct.pack('<l', packet_type) |
| 60 | + + data.encode('utf8') + b'\x00\x00' |
| 61 | + ) |
| 62 | + try: |
| 63 | + self.connection.send(struct.pack('<l', len(packet)) + packet) |
| 64 | + except socket.error as e: |
| 65 | + self.fatal_error(f'Failed to send packet: {e}', 2) |
| 66 | + |
| 67 | + def receive_packet(self): |
| 68 | + try: |
| 69 | + response = self.connection.recv(self.arguments.buffer) |
| 70 | + return response |
| 71 | + except socket.error as e: |
| 72 | + self.fatal_error(f'Failed to receive response: {e}', 3) |
| 73 | + |
| 74 | + def login(self): |
| 75 | + self.send_packet(1, PacketTypes.LOGIN, self.arguments.password) |
| 76 | + response = self.receive_packet() |
| 77 | + if response: |
| 78 | + size, id_response, type_response = struct.unpack('<l', response[:4]), struct.unpack('<l', response[ |
| 79 | + 4:8]), struct.unpack( |
| 80 | + '<l', response[8:12]) |
| 81 | + |
| 82 | + if id_response[0] == -1: |
| 83 | + self.fatal_error('Login to RCON failed', 4) |
| 84 | + else: |
| 85 | + self.fatal_error('No response received for login', 4) |
| 86 | + |
| 87 | + def send_command(self): |
| 88 | + self.send_packet(2, PacketTypes.COMMAND, self.arguments.command) |
| 89 | + response = self.receive_packet() |
| 90 | + if response: |
| 91 | + response_message = response[12:-2].decode('utf-8') # Stripping trailing null bytes |
| 92 | + self.exit_success(str(response_message)) |
| 93 | + else: |
| 94 | + self.fatal_error('No response received for command', 5) |
| 95 | + |
| 96 | + |
| 97 | +def parse_args(): |
| 98 | + parser = argparse.ArgumentParser(description='Sends RCON commands to Minecraft servers.') |
| 99 | + parser.add_argument('-a', '--address', type=str, required=True, help='The server IP address.') |
| 100 | + parser.add_argument('-p', '--port', type=int, required=True, help='The server port.') |
| 101 | + parser.add_argument('-P', '--password', type=str, required=True, help='The RCON password.') |
| 102 | + parser.add_argument('-c', '--command', type=str, required=True, help='The RCON command to send.') |
| 103 | + parser.add_argument('-t', '--timeout', type=int, default=5, help='The timeout for server response.') |
| 104 | + parser.add_argument('-b', '--buffer', type=int, default=4096, help='The buffer length for server response.') |
| 105 | + return parser.parse_args() |
| 106 | + |
| 107 | + |
| 108 | +def main(): |
| 109 | + arguments = parse_args() |
| 110 | + with Rcon(arguments) as rcon: |
| 111 | + rcon.login() |
| 112 | + rcon.send_command() |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + main() |
0 commit comments