def test_execute_no_block(self, request, rcon_server): e1_request = rcon_server.expect( 0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"echo hello") e1_request.respond( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"hello") e1_request.respond_terminate_multi_part(0) rcon_server.expect( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"") e2_request = rcon_server.expect( 0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"echo hello") e2_request.respond( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"hello") e2_request.respond_terminate_multi_part(0) rcon_server.expect( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"") rcon = valve.rcon.RCON(rcon_server.server_address, b"") rcon.connect() rcon._authenticated = True request.addfinalizer(rcon.close) response_1 = rcon.execute("echo hello", block=False) response_2 = rcon.execute("echo hello", block=True) assert response_1 is None assert response_2.id == 0 assert response_2.type is response_2.Type.RESPONSE_VALUE assert response_2.body == b"hello" assert isinstance(response_2.body, six.binary_type)
def test_execute_timeout(self, request, rcon_server): rcon_server.expect(0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"") rcon_server.expect(0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"") rcon = valve.rcon.RCON(rcon_server.server_address, b"", 1.5) rcon.connect() rcon._authenticated = True request.addfinalizer(rcon.close) with pytest.raises(valve.rcon.RCONTimeoutError): rcon.execute("")
def test_execute_timeout(self, request, rcon_server): rcon_server.expect( 0, valve.rcon.RCONMessage.Type.EXECCOMMAND, b"") rcon_server.expect( 0, valve.rcon.RCONMessage.Type.RESPONSE_VALUE, b"") rcon = valve.rcon.RCON(rcon_server.server_address, b"", 1.5) rcon.connect() rcon._authenticated = True request.addfinalizer(rcon.close) with pytest.raises(valve.rcon.RCONTimeoutError): rcon.execute("")
async def handle_rcon_command(self, message): words = message.content.split(' ') if len(words) < 4: reply = "Error: invalid parameters\n" reply += " Usage:\n @{} rcon <server-name> <rcon-command>\n".format( self.user.name) reply += " E.g:\n @{} rcon ark-server-name listplayers\n".format( self.user.name) await message.channel.send(reply) return command = words[1] server_name = words[2] rcon_command = " ".join(words[3:]) if not server_name in self.__servers__: reply = "Error: server name not found in list of servers" await message.channel.send(reply) return #print( " RCON {} {}".format( server_name, rcon_command ) ) ip = self.__servers__[server_name]["ip_address"] port = self.__servers__[server_name]["port"] password = self.__servers__[server_name]["password"] server_tuple = (ip, port) try: rcon = valve.rcon.RCON(server_tuple, password, multi_part=False) rcon.connect() rcon.authenticate() response = rcon.execute(rcon_command, timeout=10) except (valve.rcon.RCONError, ConnectionRefusedError) as e: await message.channel.send( "Failed to connect to server '{}'".format(server_name)) rcon.close() return except valve.rcon.RCONAuthenticationError as e: await message.channel.send( "Failed to authenticate to server '{}'".format(server_name)) rcon.close() return except valve.rcon.RCONTimeoutError as e: await message.channel.send( "Timeout waiting for response from server '{}'".format( server_name)) rcon.close() return except valve.rcon.RCONCommunicationError as e: await message.channel.send( "Error communicating with server '{}'".format(server_name)) await message.channel.send("Server response:\n{}".format(response.text) ) rcon.close()
def send_command(cmd, server_details): logging.info(server_details) server_status = server_details.get("network", {}).get("status", "OFFLINE") server_url = server_details.get("server", {}).get("server_ip", "127.0.0.1:1234") ip, port_str = server_url.split(":") address = (ip, int(port_str)) offline_value = "OFFLINE" result = { "info": {}, "players": {}, "text": "Failed" } password = server_details.get("server", {}).get("rcon_password", None) if server_status == "ONLINE": try: with valve.rcon.RCON(address, password) as rcon: try: response = rcon.execute(cmd) result["text"] = response.text except: result["text"] = "lost connection with server. Map may have changed!" except valve.source.NoResponseError: print("Could not fetch RCON values for server '%s:%s'" % (address)) return result
def console_command(self, guild_id, command): server_address, password = self.get_server_details(guild_id) if server_address: try: with valve.rcon.RCON(server_address, password) as rcon: a = rcon.execute(command) print(a) return 1, None except Exception as e: return 0, e else: return -1, None
async def _rcon_command(self, server, command): try: server_address = (server['server_address'], int(server['server_port'])) password = server['password'] with valve.rcon.RCON(server_address, password, timeout=10, multi_part=False) as rcon: print(rcon.execute(command).text) rcon.close() return True except: False
async def cmd(self, ctx, *, cmd_): global servers #servers = await load_json('./json/servers.json') for server in servers: channel_id = servers[server]["discord_channel_id"] if ctx.channel.id == channel_id: addr = server.split(":") addr = (addr[0], int(addr[1])) rcon_password = servers[server]["rcon_password"] try: """print("Here") rcon = valve.rcon.RCON(addr, rcon_password, timeout=None) print("Here2") rcon.connect() print("Her3") rcon.authenticate() print("Her4") command = rcon.__call__(cmd_) print("Her5")""" rcon = valve.rcon.RCON(addr, rcon_password, timeout=None) rcon.connect() rcon.authenticate() response = rcon.execute(cmd_) response_text = response.body.decode("utf-8") log1, log2 = "", "" if cmd_ == 'status': response_text = response_text.split("#") for item in range(2, len(response_text)): try: if item < len(response_text) // 2: add = format_status_log( response_text[item]) log1 = log1 + add + "\n" else: add = format_status_log( response_text[item]) log2 = log2 + add + "\n" except: await ctx.send(response_text[item]) log1 = log1 + f"{chr(701)}" await ctx.send(log1) await ctx.send(log2) else: await ctx.send(f"***__{response_text}__***") except Exception as e: print(f"Command Error: {e}") break
def test_execute_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.execute("foo")
def test_execute_not_connected(self): rcon = valve.rcon.RCON(None, b"") with pytest.raises(valve.rcon.RCONError): rcon.execute("foo")
def execute(self, commands): raw_commands = " ".join(commands) with valve.rcon.RCON((self.address, self.port), self.password) as rcon: rcon.execute("/{}".format(raw_commands), block=False, timeout=10)
def _execute(self, cmd): with valve.rcon.RCON((self._host, self._port), self._pwd) as rcon: response = rcon.execute(cmd) print(response.text)
def broadcast(RCONIP, RCONPORT, RCONPASS, MESSAGE): address = (RCONIP, int(RCONPORT)) password = RCONPASS with valve.rcon.RCON(address, password) as rcon: rcon.execute(("say " + MESSAGE), block=False, timeout=5)
def rconengine(): if request.method == 'POST': res = make_response("") if captcha.validate(): # print(request.form) if request.form.get('rconpassword') or request.form.get( 'rconport'): res.set_cookie("rconpwd", request.form.get('rconpassword'), 60 * 60) res.set_cookie("rconport", request.form.get('rconport'), 60 * 60) flash(u'Updated rcon password.', 'success') if request.form.get('mapname'): try: address = (request.form.get('ip'), int(request.cookies.get('rconport'))) password = request.cookies.get('rconpwd') with valve.rcon.RCON(address, password) as rcon: rcon.execute("say Switching scenario to " + request.form.get('mapname'), block=False, timeout=5) time.sleep(5) rcon.execute("travelscenario " + request.form.get('mapname'), block=False, timeout=5) flash( u'Switching map to: ' + request.form.get('mapname'), 'success') except: # noqa e = "Wrong password" return render_template('error.html', error=e) if request.form.get('adminchat'): try: address = (request.form.get('ip'), int(request.cookies.get('rconport'))) password = request.cookies.get('rconpwd') with valve.rcon.RCON(address, password) as rcon: rcon.execute("say " + request.form.get('adminchat'), block=False, timeout=5) flash( u'Message send: ' + request.form.get('adminchat'), 'success') except: # noqa e = "Wrong password" return render_template('error.html', error=e) if request.form.get('gridRadios'): try: address = (request.form.get('ip'), int(request.cookies.get('rconport'))) password = request.cookies.get('rconpwd') with valve.rcon.RCON(address, password) as rcon: if "kick" in request.form.get('gridRadios'): rcon.execute("kick " + request.form.get('username') + " " + request.form.get('bkreason'), block=False, timeout=5) flash(u'kicked ' + request.form.get('username'), 'success') if "ban" in request.form.get('gridRadios'): duration = request.form.get('gridRadios').split( 'ban') rcon.execute("ban " + request.form.get('username') + " " + duration[1] + " " + request.form.get('bkreason'), block=False, timeout=5) flash( u'Banned ' + request.form.get('username') + " for " + duration[1] + " minutes.", 'success') if "PERMBAN" in request.form.get('gridRadios'): rcon.execute("permban " + request.form.get('username') + " " + request.form.get('bkreason'), block=False, timeout=5) flash( u'Permanently banned ' + request.form.get('username'), 'success') # rcon.execute("say user " + request.form.get('username') + " will be " + request.form.get('gridRadios'), block=False, timeout=5) except: # noqa e = "Wrong password" return render_template('error.html', error=e) res.headers['location'] = url_for('rcon', ip=request.form['ip']) return res, 302 else: flash(u'Wrong Captcha code', 'danger') res.headers['location'] = url_for('rcon', ip=request.form['ip']) return res, 302 return render_template('rcon.html')
def rcon_send_msg(msg): with valve.rcon.RCON(address, password) as rcon: cmd = u"server %s" % msg response = rcon.execute(cmd) print('To game: server %s' % msg)