Cookbook:Accessing CD Key Hash
Problem
You want to access the CD Key Hashes for players in a game from within a Python script.
Solution
Use the host.rcon_invoke
method to send the admin.listPlayers
command to the game engine; then use a regular expression to parse the resulting table.
import re def playerData(): '''Returns a dictionary containing player data; dictionary keys are playerIDs, values are 4-element tuples, containing player name, IP address, IP port, and cd-key hash.''' rawData = host.rcon_invoke("admin.listplayers") pattern = re.compile(r'''^Id:\ +(\d+) # PlayerID \ -\ (\S+) # Player Name \ is\ remote\ ip:\ (\d+\.\d+\.\d+\.\d+): # IP Address (\d+) # Port Number (?:.*hash:\ (\w{32}))? # CD Key Hash ''', re.DOTALL | re.MULTILINE | re.VERBOSE) result = {} for player in pattern.findall(rawData): result[int(player[0])] = player[1:] return result
Discussion
This function packages the parsed data into a dictionary keyed by Player ID for easy access.
A simple example of how you would use this function:
# print a list of player names, IP addresses, and CD key hashes: info = playerData() for playerID in info: print info[playerID][0], info[playerID][1], info[playerID][3]
In principle you could do the same sort of thing with the pb_sv_plist
command to retrieve each player's PunkBuster GUID. Unfortunately, there is a bug/"unimplemented feature" in BF2, the result of which is that Python and RCon clients never get any output from PunkBuster commands.
If you need to convert CD keys into key hashes, check out keyHash.
Submitted By
--Woody