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
Example #2
0
class TF2Interface:
    def __init__(self, ip, port, rcon):
        self.ip_port = (ip, port)
        self.rcon = rcon
        self.client = RCON(self.ip_port, rcon)

    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 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 rcon_command(self, command):
        response = self.client.execute(command)
        return response

    def auth(self):
        self.client.connect()
        self.client.authenticate()

    def close(self):
        self.client.close()