def rcon(command): connection = AdminClient() connection.settimeout(0.4) connection.configure(password=RCON_PASSWORD, host=SERVER_IP, port=ADMIN_PORT) connection.connect() failed = False try: protocol_response = connection.recv_packet() welcome_response = connection.recv_packet() except socket.error: failed = True if protocol_response is None or welcome_response is None: failed = True result = [] if failed: pass else: connection.send_packet(AdminRcon, command=command) cont = True while cont: packets = connection.poll() if packets is None or packets is False: break cont = len(packets) > 0 for packet, data in packets: if packet == ServerRcon: result += [data['result']] elif packet == ServerRconEnd: cont = False break else: pass connection.disconnect() return result
if failed: print("Unable to connect to %(host)s:%(port)s" % {'host': options.host, 'port': options.port}) else: for command in args: if not connection.is_connected: break if options.verbose: print("Sending Rcon command: '%s'" % command) connection.send_packet(AdminRcon, command = command) cont = True while cont: packets = connection.poll() if packets is None or packets is False: print("Connection lost!") break cont = len(packets) > 0 for packet, data in packets: if packet == ServerRcon: print(">>> %s" % data['result']) elif packet == ServerRconEnd: print("<<< END OF COMMAND >>>") cont = False break else: pass if options.verbose: print("Disconnecting") connection.disconnect()
'host': options.host, 'port': options.port }) else: for command in args: if not connection.is_connected: break if options.verbose: print("Sending Rcon command: '%s'" % command) connection.send_packet(AdminRcon, command=command) cont = True while cont: packets = connection.poll() if packets is None or packets is False: print("Connection lost!") break cont = len(packets) > 0 for packet, data in packets: if packet == ServerRcon: print(">>> %s" % data['result']) elif packet == ServerRconEnd: print("<<< END OF COMMAND >>>") cont = False break else: pass if options.verbose: print("Disconnecting") connection.disconnect()