def command(self, command_string: str, do_print: bool = True, *args, **kwargs): """ runs console command using RCON """ if self.is_running(): if self.is_rcon_enabled(): output = None rcon = RCON(**self._get_rcon_args()) try: rcon.connect() except ConnectionRefusedError: if do_print: self.logger.warning("could not connect to RCON") return STATUS_FAILED else: rcon.authenticate() output = rcon.execute(command_string).text rcon.close() if do_print and output is not None: self.logger.info(output) return STATUS_SUCCESS if do_print: self.logger.warning( f"{self.server_name} does not have RCON enabled") return STATUS_PARTIAL_FAIL self.logger.warning(f"{self.server_name} is not running") return STATUS_PARTIAL_FAIL
def send_rcon_command(host, port, rcon_password, command, raise_errors=False, num_retries=3, timeout=3.0): from valve.rcon import (RCON, RCONCommunicationError, RCONAuthenticationError) attempts = 0 while attempts < num_retries: attempts += 1 try: with RCON((host, port), rcon_password, timeout=timeout) as rcon: response = rcon(command) return strip_rcon_logline(response) except ConnectionRefusedError: return None except KeyError: raise RconError('Incorrect rcon password') except (RCONCommunicationError, RCONAuthenticationError) as e: if attempts >= num_retries: if raise_errors: raise RconError(str(e)) else: return None
def _get_players(self, server_id: str) -> List[Tuple[str, str]]: address = (self.settings[server_id]["server_address"], 27015) password = self.settings[server_id]["rcon_password"] with RCON(address, password) as rcon: return [ tuple(x.split(";")) for x in rcon.execute( "sm_player_teams").text.strip().split("\n")[:-1] ]
def is_accessible(self): is_accessible = super().is_accessible() if is_accessible and self.is_rcon_enabled(): rcon = RCON(**self._get_rcon_args()) try: rcon.connect() except ConnectionRefusedError: self.logger.debug("RCON connect failed") is_accessible = False return is_accessible
def connect_server(sv): addr = get_addr(sv) try: connections[addr] = RCON((sv['ip'], sv['port']), password=sv['rcon'], timeout=5) connections[addr].connect() connections[addr].authenticate() except Exception as e: print(e) print('Error while authenticating server {0}'.format(addr)) sys.stdout.flush()
def await_connect_to_server(self): retry_count = 0 while retry_count < 10: try: self.client = RCON(self.ip_port, self.rcon) self.auth() return True except Exception as e: print(e) print("Failed connecting to tf2, retrying ", retry_count) retry_count += 1 self.close() time.sleep(20) return False
def execute_safe(self, command): if not self.enabled: return None if self.server.running_time() <= timedelta(minutes=1): return None rcon_enabled, rcon_host, rcon_port, rcon_password = self.get_rcon_auth() if not rcon_enabled: return None command = command.encode('ascii', 'replace') self.logger.debug('Executing on %s:%s %s' % (rcon_host, rcon_port, command)) try: with RCON((rcon_host, int(rcon_port)), rcon_password) as rcon: return rcon.execute(command) except RCONError: self.logger.error('Error sending command ' + command) except Exception: self.logger.exception('Error when exceuting RCON command') return None
def send_rcon_command(host, port, rcon_password, command, raise_errors=False, num_retries=3, timeout=3.0): from valve.rcon import (RCON, RCONMessageError, RCONAuthenticationError, RCONTimeoutError) try: port = int(port) except ValueError: return None attempts = 0 while attempts < num_retries: attempts += 1 try: with RCON((host, port), rcon_password, timeout=timeout) as rcon: response = rcon(command) return strip_rcon_logline(response) except (socket.error, socket.timeout, RCONMessageError, RCONAuthenticationError, RCONTimeoutError) as e: if attempts >= num_retries: if raise_errors: raise RconError(str(e)) else: return None
def _do_rcon(self, server_id: str, command: str, block: bool=True): address = (self.settings[server_id]["ftp_server"], 27015) password = self.settings[server_id]["rcon_password"] with RCON(address, password) as rcon: rcon.execute(command, block=block)
session = ftplib.FTP(ftp_host, ftp_login, ftp_password) session.cwd(Upload_To_Folder) session.retrlines('LIST') if plugin_name_x in session.nlst(): print( 'Plugin already exist in python plugins, queuing future reload of plugin' ) else: print("Plugin directory not found, plugin will be started ") session.mkd(plugin_name_x) NotFound = 1 file = open(Path_To_Mod, 'rb') print('STOR ' + Upload_To_Folder + plugin_name_x + os.sep + plugin_name_x + '.py') session.storbinary( 'STOR ' + Upload_To_Folder + plugin_name_x + '/' + plugin_name_x + '.py', file) file.close() session.quit() if AutoRestart > 0: print("Sending RCON reload command.") with RCON(SERVER_ADDRESS, PASSWORD) as rcon: if NotFound == 1: rcon("sp plugin load " + plugin_name_x) else: rcon("sp plugin reload " + plugin_name_x) print("Plugin successfully loaded to plugin and restarted.")
def run_rcon_command(self, args: str) -> str: with RCON(('localhost', self.rcon_port), self.rcon_pass) as rcon: return rcon(args)
def reload_admins(): servers = Server.objects.all() for server in servers: with RCON((server.host, server.port), settings.RCON_PASSWORD) as rcon: print(rcon("sm_reloadadmins"))
def update_rcon(self, new_rcon): self.rcon_command("rcon_password {}".format(new_rcon)) self.rcon = new_rcon self.client = RCON(self.ip_port, self.rcon) self.auth()
def __init__(self, ip, port, rcon): self.ip_port = (ip, port) self.rcon = rcon self.client = RCON(self.ip_port, rcon)