async def c(ctx, *args): if ctx.message.channel.id == 597108276945092672: bc = "" for warg in args: bc += warg bc += " " print(bc) with valve.rcon.RCON(SERVER_ADDRESS, RCON_PASSWORD) as rcon: await ctx.send("Executing command: ***" + bc + "***") await ctx.send(rcon(bc)) print(rcon(bc))
def test_call_text_bad(self, request, rcon_server): e_request = rcon_server.expect( 0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"") e_request.respond( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"\xFF") e_request.respond_terminate_multi_part(0) rcon = valve.rcon.RCON(rcon_server.server_address, b"") rcon.connect() rcon._authenticated = True request.addfinalizer(rcon.close) with pytest.raises(valve.rcon.RCONMessageError): rcon("")
def test_call_no_multi_timeout(self, request, rcon_server): e_request = rcon_server.expect( 0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"echo hello") e_request = rcon_server.expect( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"") e_request.respond( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"hello") rcon = valve.rcon.RCON( rcon_server.server_address, b"", timeout=3) rcon.connect() rcon._authenticated = True request.addfinalizer(rcon.close) with pytest.raises(valve.rcon.RCONTimeoutError): rcon("echo hello")
def send_rcon(info, cmds, retry=3, nest=0): if 'rcon' not in info or not cmds: return if isinstance(cmds, list): for cmd in cmds: send_rcon(info, cmd, retry, nest) return address = info['ext_ip'] if 'ext_ip' in info else info['host'] port = info['ext_port'] if 'ext_port' in info else 27015 padding = '\t' * nest while retry > 0: try: with valve.rcon.RCON((address, port), info['rcon']) as rcon: print('%sRCON:\t%s' % (padding, cmds)) print('%s\t%s' % (padding, rcon(cmds).replace('\n','\n' + '\t'*(nest+1)))) retry = 0 except valve.rcon.RCONAuthenticationError as e: print('%sRcon failed: %s'% (padding, e)) retry = 0 except valve.rcon.RCONError as e: print('%sRcon failed: %s'% (padding, e)) print('%sRetrying rcon' % padding) retry = retry -1 except TimeoutError as e: print('%sRcon timeout: %s'% (padding, e)) retry = 0 except ConnectionError as e: print('%sConnection failed: %s'% (padding, e)) retry = 0
def test_call(self, request, rcon_server): e_request = rcon_server.expect( 0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"echo hello") e_request.respond( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"hello") e_request.respond_terminate_multi_part(0) rcon = valve.rcon.RCON(rcon_server.server_address, b"") rcon.connect() rcon._authenticated = True request.addfinalizer(rcon.close) response = rcon("echo hello") assert response == "hello" assert isinstance(response, six.text_type)
def test_call_no_multi(self, request, rcon_server): e_request = rcon_server.expect( 0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"echo hello") e_request.respond( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"hello") rcon = valve.rcon.RCON( rcon_server.server_address, b"", multi_part=False) rcon.connect() rcon._authenticated = True request.addfinalizer(rcon.close) response = rcon("echo hello") assert response == "hello" assert isinstance(response, six.text_type)
def issue_command(self, command): connection_address = (self.address, int(self.port)) try: with valve.rcon.RCON(connection_address, self.password, timeout=5) as rcon: response = rcon(command) print("Rcon response:") print(response) if response: return True, response except valve.rcon.RCONCommunicationError as socket_err: print( f"Hit a communication error when trying to issue: '{command}' to '{self.address}:{self.port}'" ) print(socket_err) return False, socket_err except valve.rcon.RCONTimeoutError as timeout_err: print( f"Hit a timeout error when trying to issue: '{command}' to '{self.address}:{self.port}'" ) print(timeout_err) return False, timeout_err except Exception as generic_err: print( f"Hit an unknown error when trying to issue: '{command}' to '{self.address}:{self.port}'" ) return False, generic_err print("No conditions met, assuming failure?") return False
def test_call_not_authenticated(self, request, rcon_server): rcon = valve.rcon.RCON(rcon_server.server_address, b"") rcon.connect() request.addfinalizer(rcon.close) with pytest.raises(valve.rcon.RCONError): rcon("foo")
def test_call_not_connected(self): rcon = valve.rcon.RCON(None, b"") with pytest.raises(valve.rcon.RCONError): rcon("foo")
def run(servers, force_reupload=False, rcon_cmds='', delete_files=None): logger = logging.getLogger(valve.rcon.__name__) logger.setLevel(logging.ERROR) if (not os.path.isdir('upload')): os.mkdir('upload') upload_files = [] for i, (path, dirs, files) in enumerate(os.walk('upload')): for f in files: upload_files.append(('/' + path[7:].replace('\\', '/'), f)) print('Discovered %d files in uploads folder' % len(upload_files)) for i, file in enumerate(upload_files): print('\t%d: %s' % (i + 1, file)) print('Logging into %d servers...' % len(servers)) for i, (server, info) in enumerate(servers.items()): host = info['host'] port = info['ext_port'] if 'ext_port' in info else 27015 print('\t%d. %-20s [%s:%d]' % (i + 1, server, host, port)) success = False if upload_files or delete_files: try: if 'protocol' in info: success, plugins_load, plugins_reload = upload_sftp( (server, info), upload_files, delete_files, force_reupload) else: success, plugins_load, plugins_reload = upload_ftp( (server, info), upload_files, delete_files, force_reupload) except Exception as e: print('\t\tError while uploading: %s' % e) else: success, plugins_load, plugins_reload = True, [], [] if success and 'rcon' in info and (rcon_cmds or plugins_load or plugins_reload): address = info['ext_ip'] if 'ext_ip' in info else host retry = 3 while retry > 0: try: with valve.rcon.RCON((address, port), info['rcon']) as rcon: for plugin in [ p for p in plugins_load if p not in plugins_reload ]: print('\t\tRCON: %s' % rcon('sm plugins load ' + plugin)) for plugin in plugins_reload: print('\t\tRCON: %s' % rcon('sm plugins reload ' + plugin)) if rcon_cmds: print('\t\tRCON:\t%s' % rcon(rcon_cmds).replace('\n', '\n\t\t\t')) retry = 0 except valve.rcon.RCONAuthenticationError as e: print('\t\tRcon failed: %s' % e) retry = 0 except valve.rcon.RCONError as e: print('\t\tRcon failed: %s' % e) print('\t\tRetrying rcon') retry = retry - 1 except TimeoutError as e: print('\t\tRcon timeout: %s' % e) retry = 0 except ConnectionError as e: print('\t\tConnection failed: %s' % e) retry = 0
import valve.rcon server_address = ("192.168.1.104", 25577) password = "******" with valve.rcon.RCON(server_address, password) as rcon: print(rcon("echo Hello, world!"))