Counter Strike Server Admins

Scripts, Tips etc. usw. for Counter Strike Server Admins

Psychostats 2.x to HLSW

Create a HLSW Global ID Database Import file from a Psychostats 2.x Database

hlswdb-config.php

Configuration settings

<?php
    // config.php
    error_reporting(E_ALL);
 
    define('MYSQL_HOST',     'localhost');
    define('MYSQL_USER',     'dbuser');
    define('MYSQL_PASS',     'dbpassword');
    define('MYSQL_DATABASE', 'dbname');
    define('MYSQL_TABLE_PREFIX', 'pstats_');
    define('FRIENDS_COUNT', 300);
    #currently unused:
    define('FRIENDS_TYPE', 'Friend'); // Friend, Blue, Brown, Normal, Lamer, Auto Added
    define('OTHER_TYPE', 'Auto Added');
?>

hlswdb.php

Meant to be called from the command line (see below).

<?php
 
include 'hlswdb-config.php';
 
function check_args($argv, $argc) {
    $table_prefix = "";
 
    if(@php_sapi_name() != 'cli' && @php_sapi_name() != 'cgi') {
        die("This script will only work in the shell.");
    }
 
    if ($argc <= 1) {
        if (MYSQL_TABLE_PREFIX == "") {
            die("Missing table prefix\n");
        } else {
            $table_prefix = MYSQL_TABLE_PREFIX;
        }
    } else {
        $table_prefix = MYSQL_TABLE_PREFIX . $argv[1];
    }
    return $table_prefix;
}
 
function connect() {
    @mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS)
        || die("Es konnte keine DB Verbindung aufgebaut werden: " . mysql_error() . "\n");
 
    mysql_select_db(MYSQL_DATABASE)
        || die("Konnte Datenbank nicht benutzen, Fehlermeldung: " . mysql_error() . "\n");
}
 
function query($table_prefix) {
    $sql = "SELECT DISTINCT worldid, name FROM ${table_prefix}plrids"
            . " WHERE worldid LIKE 'STEAM%'"
            . " AND worldid <> 'STEAM_ID_PENDING'"
    //        . " OR worldid = 'HLTV'"
            . " ORDER BY worldid ASC, totaluses DESC";
    $result = mysql_query($sql) OR die(mysql_error() . "\n");
    return $result;
}
 
function print_entries($result) {
    $lastid = "";
    $names = array();
    $rowcount = 0;
 
    print_header();
 
    while($row = mysql_fetch_assoc($result)) {
        // print_r($row);
        if($row['worldid'] != $lastid && $rowcount > 0) {
            print_entry($lastid, $names, $rowcount);
            $names = array($row['name']);
        } else {
            $names[] = $row['name'];
        }
        $lastid = $row['worldid'];
        $rowcount++;
    }
 
    print_entry($lastid, $names, $rowcount);
}
 
function print_header(){
    echo("// HLSW WONID database text filer\n");
    echo("// Date: " . date("m/d/y") . "\r\n\r\n");
    echo("Version: 1\r\n\r\n");
}
 
function print_entry($id, $nicks, $index) {
    //$type = $index < FRIENDS_COUNT ? FRIENDS_TYPE : OTHER_TYPE;
    $type = OTHER_TYPE;
    echo("STEAM:    $id\r\n");
    echo("Type:     \"$type\"\r\n");
    echo("Banned:  0\r\n");
    echo("Names:   ");
 
    $i = 0;
    foreach($nicks as $nick) {
        if ($i++ > 0) {
            echo(" ");
        }
        // escape backslashes
        $nick = ereg_replace("\\\\", "\\\\", $nick);
        echo('"' . $nick . '"');
    }
    echo("\r\n\r\n");
}
 
// ### main ###################################################################
 
$table_prefix = check_args($argv, $argc);
connect();
print_entries(query($table_prefix));
 
?>

hlswdb.sh

Mail the current hlswdb.

#!/bin/sh
 
hlswdbphp=/path/to/hlswdb.php
tempdir=/var/tmp
zipfilename=hlswdb.db5.zip
recipients="joe-dalton@fahr-zur-hoelle.org someone@example.com"
 
mkdir -p $tempdir
 
echo "Generating HLSW DB..."
php $hlswdbphp/hlswdb.php 16_ > $tempdir/hlswdb.db5.txt
 
echo "Zipping..."
cd $tempdir
zip -9 $zipfilename *.db5.txt
 
echo "Sending mail..."
mpack -s "HLSW DB vom `date +%d.%m.%Y`" $tempdir/$zipfilename $recipients
 
echo "Cleaning up..."
rm -v $tempdir/*.db5.*
 
echo "Done"

crontab

00 08 1 * * nice /path/to/hlswdb.sh > /path/to/hlswdb.log

Post Yesterday's Bans

Extract the ban entries from local logfiles and post them to a phpBB forum.

print.sh (CS 1.6)

Takes one date argument. see the description of the –date parameter in man date for further details.

#!/bin/bash
 
logdir=/path/to/logs
amxdir=/path/to/logs/amx
 
echo "[b]Rcon bans:[/b]"
cd $logdir
grep "^L `date --date="$1" +%m/%d/%Y`.*banid [0-9]* STEAM_[0-9:]*.*" L*.log | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*banid \([0-9]*\) \(STEAM_[0-9:]*\).*" from "\(.*\)"/[b]\1[\/b] [color=red]\3[\/color] für [color=orange]\2[\/color] min, von [color=green]\4[\/color]/g'
foundrcon=$?
 
echo
echo "[b]AMX bans:[/b]"
cd $amxdir
grep "^L `date --date="$1" +%m/%d/%Y`.*Ban:.*" admin*.log | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*Ban: "\(.*\)".*ban and kick "\(.*\)".*minutes "\([0-9]*\)")/[b]\1[\/b] [color=red]\3[\/color] für [color=orange]\4[\/color] min, von [color=green]\2[\/color]/g'
foundamx=$?
 
echo
echo "[b]Steambans:[/b]"
cd $logdir
grep "^L `date --date="$1" +%m/%d/%Y`.*\[SBSRV\] Active removal" L*.log | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*\[SBSRV\] Active removal for: \(.*\)/[b]\1[\/b] [color=red]\2[\/color]/g'
#grep "^L `date --date="$1" +%m/%d/%Y`.*\[SBSRV\] Player with id STEAM_.* is banned\." L*.log | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*\[SBSRV\] Player with id \(STEAM_.*\) is banned\./[b]\1[\/b] [color=red]\2[\/color]/g'
foundsb=$?
 
echo
echo "[b]Entbannt:[/b]"
cd $logdir
grep "^L `date --date="$1" +%m/%d/%Y`.*removeid STEAM_[0-9:]*.*" L*.log | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*removeid \(STEAM_[0-9:]*\).*" from "\(.*\)"/[b]\1[\/b] [color=orange]\2[\/color] von [color=green]\3[\/color]/g'
unbans=$?
 
let foundbans=$foundrcon+$foundamx+$foundsb+$unbans
exit $foundbans

print.sh (CS:S)

#!/bin/bash
 
logdir=/path/to/logs
cd $logdir
 
tempfile=`mktemp`
echo "[b]Rcon bans:[/b]"
grep "^L `date --date="$1" +%m/%d/%Y`.*banid [0-9]* *STEAM_[0-9]*.*" L*.log > $tempfile
foundrcon=$?
iconv -c -f "UTF-8" -t "8859_1" $tempfile | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*rcon from "\([^:"]*\).*banid \([0-9]*\) *\(STEAM_[0-9:]*\).*"/[b]\1[\/b] [color=red]\4[\/color] für [color=orange]\3[\/color] min, von [color=green]\2[\/color]/g'
rm $tempfile
 
tempfile=`mktemp`
echo
echo "[b]Mani  bans:[/b]"
grep -h "^L `date --date="$1" +%m/%d/%Y`.*\[MANI_ADMIN_PLUGIN\].*banid" L*.log > $tempfile
foundmani=$?
iconv -c -f "UTF-8" -t "8859_1" $tempfile | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*\[MANI_ADMIN_PLUGIN\] Admin \(.*\) Executed :  Banned (By Admin) \(.*\) banid \([0-9]*\).*/[b]\1[\/b] [color=red]\3[\/color] für [color=orange]\4[\/color] min, von [color=green]\2[\/color]/g'
rm $tempfile
 
tempfile=`mktemp`
echo
echo "[b]VAC bans:[/b]"
grep "^L `date --date="$1" +%m/%d/%Y`.*VAC banned" L*.log > $tempfile
foundvac=$?
iconv -c -f "UTF-8" -t "8859_1" $tempfile | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*"\(.*\)" disconnected (reason "VAC banned from secure server/[b]\1[\/b] [color=red]\2[\/color]/g'
rm $tempfile
 
tempfile=`mktemp`
echo
echo "[b]Steambans:[/b]"
grep "^L `date --date="$1" +%m/%d/%Y`.*\[SBSRC\] Active removal" L*.log > $tempfile
#grep "^L `date --date="$1" +%m/%d/%Y`.*\[SBSRC\] Player with id STEAM_.* is banned\." L*.log > $tempfile
foundsb=$?
iconv -c -f "UTF-8" -t "ISO-8859-1" $tempfile | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*\[SBSRC\] Active removal for: \(.*\)/[b]\1[\/b] [color=red]\2[\/color]/g'
#iconv -c -f "UTF-8" -t "ISO-8859-1" $tempfile | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*\[SBSRC\] Player with id \(STEAM_[0-9:]*\) is banned\./[b]\1[\/b] [color=red]\2[\/color]/g'
rm $tempfile
 
tempfile=`mktemp`
echo
echo "[b]Entbannt:[/b]"
grep -h "^L `date --date="$1" +%m/%d/%Y`.*removeid \(STEAM_[0-9:]*\).*" L*.log > $tempfile
iconv -c -f "UTF-8" -t "ISO-8859-1" $tempfile | sed 's/.*- \([0-9][0-9]:[0-9][0-9]\).*: rcon from "\([^:"]*\).*removeid \(STEAM_[0-9: ]*\)"/[b]\1[\/b] [color=orange]\3[\/color] von [color=green]\2[\/color]/g'
unbans=$?
rm $tempfile
 
let foundbans=$foundrcon+$foundmani+$foundvac+$foundsb+$unbans
exit $foundbans

post.sh

#!/bin/bash
 
cd `dirname $0`
yesterday=`date --date="1 day ago" "+%A, %d. %b. %Y"`
 
forumurl=http://incognito.iam.at/forum # url of forum, without trailing slash
username=someone # name of poster
password=mypw # password of poster
forum=1 # id of forum
thread=1234 # id of thread
 
message=`./print.sh "1 day ago"`
foundbans=$?
 
#if [ $foundbans -eq 0 ]
#then
    curl --silent --show-error --cookie-jar cookies.txt --output logged-in.html --data "username=$username&password=$password&autologin=on&redirect=&login=Login" "$forumurl/login.php"
    sid=`egrep -o "phpbb2mysql_sidw*(.*)" cookies.txt | sed  "s/phpbb2mysql_sidt*//"`
    curl --silent --show-error --cookie cookies.txt --output posted.html --data "subject=Bans vom $yesterday&message=$message&mode=reply&f=$forum&t=$thread&post=Absenden&disable_smilies=truei&sid=$sid" "$forumurl/posting.php"
#fi

crontab

01 0 * * * nice /path/to/post.sh &> /path/to/post.log

manually

$ /path/to/post.sh

Rotate ban list

After loosing the huge ban list of our public server once, I started using this config for the logrotate program.

Counter Strike 1.6

/path/to/banned.cfg {
    missingok
    daily
    copy
    rotate 30
}

Counter Strike:Source

/path/to/banned_user.cfg {
    missingok
    daily
    copy
    rotate 30
}

Hide banned players

A Script to hide banned players from the PsychoStats 2.x rankings. Set the allow_rank field to 0 for players in the ban list. Must be executed once after each psychostats run.

Found this one in the old psychostats forums.

#!/bin/sh
# hide banned players from ranking
#
 
# banned.cfg:
banned_cfg=/path/to/banned.cfg # banned_user.cfg for source servers
 
# database
database="dbname"
 
# table prefix
table_prefix="pstats_"
 
# "mysql" exe location:
mysql_shell="`which mysql`"
 
# mysql command line options:
mysql_options="-u dbuser --password=dbpassword -v"
 
# temporary SQL script file:
tmpfile=/tmp/.sql-$$
 
echo "use $database ;" > "$tmpfile"
if [ -w "$tmpfile" -a -r "$banned_cfg" ] ; then
  while read a b id ; do
    echo "update ${table_prefix}plr set allowrank = 0 where worldid = "$id" ;" >> "$tmpfile"
  done < "$banned_cfg"
  "$mysql_shell" $mysql_options -e "source $tmpfile"
fi
rm -f $tmpfile

Psychostats 1.9

mirrorlogs.sh

Unix Shell Script to fetch the game server's logfiles

#!/bin/bash
 
# config #######################################################################
rooturl= ### game server URL ###
rootdir= ### base dir for mirrored logfiles ###
logpath= ### dir for script activity logs ###
 
# script #######################################################################
 
# hlds logs
curmonth=$(/bin/date +%m)
nice wget -nv -N -o $logpath/mirrorlogs.log -P "$rootdir"/logs "$rooturl"/logs/L"$curmonth"*.log
#wget -nv -N -a $logpath/mirrorlogs.log -P "$rootdir"/logs "$rooturl"/logs/L*.log
 
# banned.cfg
mkdir 
nice wget -nv -N -a $logpath/mirrorlogs.log -P "$rootdir"/banned "$rooturl"/banned.cfg
 
# amx admin logs
mkdir -p $rootdir/amx
nice wget -nv -N -a $logpath/mirrorlogs.log -P "$rootdir"/amx "$rooturl"/addons/amx/logs/*
 
# banned_user.cfg on source servers
mkdir -p $rootdir/banned
nice wget -nv -N -a $logpath/mirrorlogs.log -P "$rootdir"/banned "$rooturl"/cfg/banned_user.cfg

rotatelogs.sh

Unix Shell Script to delete old log files This script gzip's the files first and deletes them later on.

#!/bin/bash
 
find ~/z/logs/ -name "*.log" -mtime +30 -exec gzip -fqv "{}" ";" 2>&1
find ~/z/logs/ -name "*.log.gz" -mtime +180 -exec rm -fv "{}" ";"

crontab

Cron entries to automate the above

58 * * * * /path/to/mirrorlogs.sh &> /path/to/mirrorlogs.log
00 0 * * * /path/to/rotatelogs.sh &> /path/to/rotatelogs.log
01 2,14,18,22 * * * nice /path/to/psychostats1.9.1/stats.pl &> /dev/null