pythonremote-serverutility

Writing a utility to perform a simple action on a remote server


I need your advice on how to go about the following:

  1. I am running a Counter Strike Game server on an Azure VM
  2. The server is nothing but a Desktop utility that runs a service on certain port to which my friends connect and play the game
  3. At times, I have to change the "map" on the game which means I have to go/login on the Azure VM and manually perform some actions (very basic like increase round-time for a match) or kick a user
  4. Every time, I have to login on server and do this manually. If I want someone else to do it, then I will have to give them the VM access which is not what I want
  5. I know moderate Python, so I can write a script that would actually perform this action (using Python OS library)
  6. But I want to be able to run this script remotely from a web app or any other trigger

So the use case would look like this:


Solution

  • Here is a simple flask api to kick players by steamid using rcon.

    You can use curl to post data to it like this:

    curl -H "Content-Type: application/json" \
      --d '{"steamid":"STEAM_1:0:1234567"}' \
      https://myGameserver.com:8989/kickplayer
    

    The api:

    from flask import Flask, request
    import valve.rcon
    
    app = Flask(__name__)
    
    HOST = "0.0.0.0"
    PORT = 8989
    DEBUG = True
    
    # replace with your VM's local ip if you are running this web app on the same VM as the csgo server
    # or with the azure public ip if not. (You will need to open up port 27015 TCP for rcon)
    CSGO_SERVER_IP = "10.0.100.100"
    RCON_PORT = 27015
    RCON_PASS = "supersekret123456"
    
    @app.route('/kickplayer', methods=['POST'])
    def kick_player():
        data = request.get_json()
        steamid = data["steamid"] # STEAM_1:0:XXXXXXXXX
    
        rcon_command = "sm_kick " + steamid
    
        with valve.rcon.RCON((CSGO_SERVER_IP, RCON_PORT), RCON_PASS) as rcon:
            response = rcon.execute(rcon_command)
        
        return f"Kicked player {steamid}"
    
    if __name__ == '__main__':
        app.run(debug=DEBUG, host=HOST, port=PORT)