| « sleepy golden eyes | pass me the booze! » |
Battlefield 2 rcon utility
#!/usr/bin/perl -w
## this program implements the bf2 rcon password login
### and provides a simple commandline based interface
use strict;
use IO::Socket;
use Digest::MD5;
my $host = shift or die "no hostname";
my $port = shift or die "no rcon port";
my $pass = shift or die "no bf2 rcon password";
my $cmd = join(' ', @ARGV) || 'list';
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
while($sock) {
my $line = scalar SMALLER_SIGN $sock GREATER_SIGN;
if (!(defined($line) && $line)) {
close($sock); exit(0);
} else {
## find the Digest seed to login
if ($line =~ /\#\#\# Digest seed: (.*)/) {
my $digest = $1;
my $md5value = Digest::MD5->new();
## this is the digested password
$md5value->add($digest); $md5value->add($pass);
## login with the digested password
print $sock "login " . unpack("H*", $md5value ->digest()) . "\n";
## send the command and logout
print $sock $cmd . "\n"; print $sock "logout" . "\n"
} else {
if ( ($line =~ /^\#\#\#/) || ($line =~ /^$/) ||
($line =~ /Authentication successful, rcon ready/) ||
($line =~ /Logout successful/) ) {
next;
} else { print $line; }
}
}
}
close $sock;
This code is connecting to the rcon port of a bf2 server and expects a command.
It then returns the output of this command.
Bear in mind that punkbuster console commands like "pb_sv_plist" are only returned to the server console and will not be returned to the rcon output.
Generally, it's called this way:
#!/bin/bash
grep -v "^##" "$(dirname ${0})/bf2rcon.cfg" | \
while read host port pass
do
echo "updating $host:$port"
"$(dirname ${0})/bf2rcommand" $host $port $pass \
exec game.sayAll \"SYS load: $(cat /proc/loadavg)\"
done
while bf2rcon.cfg is a simple list of hosts, ports and passwords for executing rcon commands.
This sample tool is just printing the current load average of the server to the console of the game servers.
Of course you could print teamspeak users online or other nifty jokes.
-Alex
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Feedback awaiting moderation
This post has 1 feedback awaiting moderation...