Skip to content

Instantly share code, notes, and snippets.

@JMatthewman
Last active February 10, 2023 01:36
Show Gist options
  • Save JMatthewman/4019383 to your computer and use it in GitHub Desktop.
Save JMatthewman/4019383 to your computer and use it in GitHub Desktop.
SRV-Management.sh
#!/bin/bash
recognise() {
prefix="${srv%%-*}"
prefix="${prefix^^}"
case $prefix in
"CSS") { # CounterStrike: Source
game="CounterStrike: Source"
CSS-start() {
if [ $startupvars = $srv ]
then
screen -dmS $srv $srv/css/srcds_run -console -game cstrike +ip $ip -port $port +map $map +maxplayers $maxplayers -autoupdate +rcon_password $rconpass +fps_max $fpsmax
else
printf "${RED}Server startup.vars wrong, or not loaded."
fi
}
CSS-stop() {
printf "Asking server to stop${EOL}"
screen -x $srv -X stuff "quit$(echo -ne '\r')" # Enter 'quit' into screen and press enter. Stops server.
sleep 5
printf "Quiting screen session.${EOL}"
screen -x $srv -X quit # Quit screen session.
}
CSS-update() { # update procedures
printf "Updating server${EOL}"
./steam -command update -game "counter-strike source" -dir "$SRV"
}
CSS-install() { # install procedures... download files etc
printf "Installing server${EOL}"
mkdir $srv
./steam -command update -game "counter-strike source" -dir "$SRV"
# installey things
}
CSS-config() { # List details of config files for the server
if [ -f "$srv/startup.vars" ] # if startup.vars file is found in server dir, don't create, otherwise create
then
printf "startup.vars already exists. If other config files need creating, try running the server once.${EOL}"
else
printf "startupvars=$srv\\nip=192.168.0.150\\nport=27015\\nmap=de_dust2\\nmaxplayers=15\\nrconpass=RandomPass\\nfpsmax=550" > $srv/startup.vars
fi
printf "Configs for '$game' are stored in the following locations:\\n\\to $srv/startup.vars\\n\\to $srv/css/cstrike/cfg/server.cfg\\n\\to Other configs may also exist for SourceMod and SourceMod plugins" #'\\n\\to ' = new line, tab indent, 'o' (improv bullet point)
}
}
;;
"TS3") { # Teamspeak 3
game="Teamspeak 3" # name of game
TS3-start() { # startup procedures
printf "Starting $srv${EOL}"
if [ "$dbplugin" = "ts3db_mysql" ]
then
$srv/ts3server_startscript.sh start dbplugin=$dbplugin host=$host port=$port username=$username password=$password database=$database
else
$srv/ts3server_startscript.sh start dbplugin=$dbplugin
fi
}
TS3-stop() { # stop procedures
printf "Stopping $srv${EOL}"
$srv/ts3server_startscript.sh stop
}
TS3-install() { # install procedures... download files etc
printf "Installing $srv${EOL}"
mkdir $srv
cd $srv
wget http://teamspeak.gameserver.gamed.de/ts3/releases/3.0.6.1/teamspeak3-server_linux-x86-3.0.6.1.tar.gz
tar -zxvf teamspeak3-server_linux-x86-3.0.6.1.tar.gz
rm teamspeak3-server_linux-x86-3.0.6.1.tar.gz
mv -f ./teamspeak3-server_linux-x86/* ./
rm -r teamspeak3-server_linux-x86/
cd ~
# installey things
}
TS3-update() { # update procedures
printf "Updating $srv${EOL}"
TS3-stop
TS3-install
TS3-start
#only way to update TS3 is overwrite files with new.
}
TS3-config() { # List details of config files for the server
if [ -f "${srv}/startup.vars" ] # if startup.vars file is found in server dir, don't create, otherwise create
then
printf "startup.vars already exists. If other config files need creating, try running the server once.${EOL}"
else
printf "Creating startup.vars${EOL}"
printf "startupvars=${srv}\\ndbplugin=ts3db_sqlite3\\n\\n#dbplugin=ts3db_mysql\\nhost=localhost\\nport=3306\\nusername=<mySQL username>\\npassword=<mySQL password>\\ndatabase=<mySQL database>\\nsocket=/var/run/mysqld/mysqld.sock" > $srv/startup.vars # Create startup.vars file
fi # list locations for config files
printf "Configs for '$game' are stored in the following locations:\\n\\to $srv/startup.vars\\n\\to All other server configs are stored in the database"
}
}
;;
"CSGO") { # CounterStrike: Global Offensive
game="CounterStrike: Global Offensive" # name of game
CSGO-start() {
if [ $startupvars = $srv ]
then
screen -dmS $srv $srv/srcds_run -console -usercon -game csgo +ip $ip -port $port +map $map +mapgroup $mapgroup -autoupdate -tickrate $tickrate +game_type $gametype +game_mode $gamemode +sv_pure $purity +rcon_password $rconpass
else
printf "${RED}Server startup.vars wrong, or not loaded."
fi
}
CSGO-stop() {
printf "Asking server to stop${EOL}"
screen -x $srv -X stuff "quit$(echo -ne '\r')" # Enter 'quit' into screen and press enter. Stops server.
sleep 5
printf "Quiting screen session.${EOL}"
screen -x $srv -X quit # Quit screen session.
}
CSGO-update() { # update procedures
printf "Updating server${EOL}"
STEAMEXE=steamcmd ./steam.sh +login $steamuser $steampass +force_install_dir ./$srv +app_update 740 validate +quit
}
CSGO-install() { # install procedures... download files etc
printf "Installing server${EOL}"
mkdir $srv
STEAMEXE=steamcmd ./steam.sh +login $steamuser $steampass +force_install_dir ./$srv +app_update 740 validate +quit
# installey things
}
CSGO-config() { # List details of config files for the server
if [ -f "$srv/startup.vars" ] # if startup.vars file is found in server dir, don't create, otherwise create
then
printf "startup.vars already exists. If other config files need creating, try running the server once.${EOL}"
else
printf "startupvars=$srv\\nip=192.168.0.150\\nport=27015\\nmap=de_dust2\\nmapgroup=mg_deathmatch\\nrconpass=RandomPass\\ntickrate=128\\ngametype=0\\ngamemode=0\\npurity=0" > $srv/startup.vars
fi
printf "Configs for '$game' are stored in the following locations:\\n\\to $srv/startup.vars\\n\\to $srv/csgo/cfg/settings.cfg\\n\\to Other configs may also exist for SourceMod and SourceMod plugins" #'\\n\\to ' = new line, tab indent, 'o' (improv bullet point)
}
}
;;
"FAKE") { #Fake entry for testing <Game Name>
game="Fake entry for testing" # name of game
FAKE-start() { # startup procedures
echo $srv $prefix
screen -dmS $srv nano fakefile
printf "Yeah I totally just started a game${EOL}"
}
FAKE-stop() { # stop procedures
printf "Stopping server${EOL}"
#stoppy type things
}
FAKE-update() { # update procedures
printf "Updating server${EOL}"
#updatey type things
}
FAKE-install() { # install procedures... download files etc
printf "Installing server${EOL}"
mkdir $srv
# installey things
}
FAKE-config() { # List details of config files for the server
if [ -f "${srv}/startup.vars" ] # if startup.vars file is found in server dir, don't create, otherwise create
then
printf "startup.vars already exists. If other config files need creating, try running the server once.${EOL}"
else
printf "startupvars=${srv}\\nip=192.168.0.150\\nport=27015\\nmap=de_dust2\\nmaxplayers=15\\nrconpass=RandomPass\\nfpsmax=550" > $srv/startup.vars
fi
printf "Configs for '$game' are stored in the following locations:\\n\\to $srv/startup.vars\\n\\to $srv/css/cstrike/cfg/server.cfg\\n\\to Other configs may also exist for SourceMod and SourceMod plugins" #'\\n\\to ' = new line, tab indent, 'o' (improv bullet point)
}
}
;;
*) { # Default entry. Game not recognised. Ask to run update, or contact for support.
printf "\\a${RED}Game with prefix '$prefix' not recognised.${EOL}"
printf "Try running $0 update or contact [email protected] about adding support for new games.${EOL}"
return 1;
}
;;
esac
}
#!/bin/bash
{ # Initial Setup
task=$1
task="${task,,}"
servers=("${*:2}")
steamuser="anonymous"
steampass=""
cd ~ # for the sake of making life easier and avoiding issues, always cd to home dir
if [ -f ".games" ]
then
source ./.games # All game definitions and relevant functions are stored within .games; This file can be updated by running <this script> update.
else
task="update"
servers=""
fi
}
{ # Text color variables
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BOLD=$(tput bold)
UNDERLINE=$(tput smul)
NORMAL=$(tput sgr0)
EOL=${NORMAL}\\n
}
{ # Functions
handler() { # loop through requests for multiple servers
for srv in $servers
do
srv="${srv%%/*}"
process #hands over to main server management function
printf $EOL
done
}
usage() {
printf "Usage:${EOL}Call this script with $0 <command> or $0 <command> <server/servers>${EOL}"
printf "${GREEN}General Commands available:${EOL}"
printf "\\to HLDS\\t\\t\\t\\t--\\tDownload and install HLDS Update tool${EOL}"
printf "\\to SteamCMD\\t\\t\\t--\\tDownload and install SteamCMD server tool${EOL}"
printf "\\to About\\t\\t\\t\\t--\\tDisplay info about this script${EOL}"
printf "\\to Help\\t\\t\\t\\t--\\tDisplay this help information${EOL}"
printf "\\to Update\\t\\t\\t--\\tUpdate game specific commands${EOL}"
printf "${GREEN}Server Commands available:${EOL}"
printf "\\to start <server / servers>\\t--\\tStart the specified server / list of servers${EOL}"
printf "\\to stop <server / servers>\\t--\\tStop the specified server / list of servers${EOL}"
printf "\\to restart <server / servers>\\t--\\tRestart the specified server / list of servers${EOL}"
printf "\\to status <server / servers>\\t--\\tCheck the status of the specified server / list of servers${EOL}"
printf "\\to install <server / servers>\\t--\\tInstall the specified server / list of servers${EOL}"
printf "\\to update <server / servers>\\t--\\tUpdate the specified server / list of servers${EOL}"
printf "\\to config <server / servers>\\t--\\tList the config files etc for the specified server / list of servers${EOL}"
}
exists() { # Check folder for server exists, continue / error & exit
if [ -d $srv ] # does the server direcory exist?
then # If the folder does exist, do this \/
if [ $task = "install" ] # is the task 'install'?
then # if the task is install, we don't want to install over an existing folder. return 1 for error
printf "\\a${RED}Folder for server '$srv' already exists. Cancelling installation.${EOL}"
return 1
else
return 0 # if not installing, then the folder is good, return 0 for OK
fi
else # if the folder doesn't exist
if [ $task = "install" ] # is the task 'install'?
then # if the task is install, the folder shouldn't exist, return 0 for OK
return 0
else # if the task is anything other than install, the server should exist. return 1 for error, and print message
printf "\\a${RED}$srv doesn't exist!${EOL}Please check the name and case-sensitivity, or run $0 install $srv${EOL}"
return 1
fi
fi
}
status() {
if [ -n $srv ]
then
if [ "$(screen -ls | grep 'No Sockets found in /var/run/screen/')" ] # screen -ls if output includes 'no sockets found'
then
printf "\\a${RED}There are no servers running!${EOL}" # if found warn no servers running
return $1 # return 1 for error / stopped
else
if [ "$(screen -ls | grep $srv)" ] # else search for specific server
then
printf "${GREEN}$srv is running (supposedly)${EOL}" # if found, server is running
return $0 # return 0 okay / running
else
printf "${RED}$srv is stopped.${EOL}" # if not, server is stopped
return $1 # return 1 error / stopped
fi
fi
else
printf "No server specified. Running screen -ls${EOL}"
screen -ls
fi
}
process() { # Server Management recognition/processing
if exists && recognise # check that the server folder exists (unless task is install) and the prefix for the server is recognised
then
case $task in
"status")
status
;;
"start")
if status
then
if [ -f "${srv}/startup.vars" ] # if startup.vars file is found in server dir, source it to load variables
then
source $srv/startup.vars
$prefix-start
fi
else
printf "Server is already running. Try running 'screen -x ${srv}' to verify the status, or '$0 restart $srv'${EOL}"
fi
;;
"stop")
$prefix-stop
;;
"restart")
$prefix-stop # Stop the server
sleep 5 # Bit obvious? (change time delay?)
$prefix-start # Start server
;;
"update")
$prefix-stop # Stop the server
$prefix-update # Update the server
$prefix-start # Start server
;;
"install")
# do instally type things
# check for $prefix-TEMPLATE, if exists, copy, else install
# need to make other commands refuse to start stop or restart $prefix-TEMPLATE
if [ -d "${prefix}-TEMPLATE" ] # check if folder with same prefix -TEMPLATE exists
then
printf "${prefix}-TEMPLATE found. Duplicating${EOL}"
cp -r "${prefix}-TEMPLATE" $srv # if it does, copy it as the new server
printf "Updating ${srv}.${EOL}"
$prefix-update # run update on duplicated template
else
$prefix-install # if template doesn't exist, run install script
printf "This server was installed from scratch. Consider making a template for this server to save time and bandwidth.${EOL}"
fi
printf "Game installed. Please remember to configure the server before starting.${EOL}"
$prefix-config
;;
"config")
$prefix-config
;;
esac
fi
}
}
{ # Initial task recognition (this actually runs naturally - Everything else is stored in functions and called)
case $task in
"steamcmd")
# Download and install SteamCMD
printf "Using Steam account:\\t${steamuser}${EOL}"
printf "Password:\\t\\t${steampass}${EOL}"
wget http://blog.counter-strike.net/wp-content/uploads//2012/04/steamcmd.tar.gz
tar xvfz steamcmd.tar.gz
rm steamcmd.tar.gz
chmod +x steam.sh
STEAMEXE=steamcmd ./steam.sh +quit
;;
"hlds")
# Download and install HLDS
wget http://storefront.steampowered.com/download/hldsupdatetool.bin
chmod +x hldsupdatetool.bin
./hldsupdatetool.bin
rm hldsupdatetool.bin
rm readme.txt
printf "${GREEN}HLDS Update Tool downloaded and run. Steam server tool installed.${EOL}"
;;
"about")
printf "${CYAN}Games Server Management script by ${BOLD}MoleMan (Joel Matthewman)${NORMAL}${CYAN} -- 20/11/2012\\[email protected]${EOL}"
;;
"display") # sample to test processing of multiple inputs
i=0
for srv in $servers
do
srv="${srv%%/*}"
((i++))
printf "$i = $srv\\n"
recognise
printf "${prefix}${EOL}"
printf $EOL
done
;;
"start" | "stop" | "restart" | "install" | "config" )
if [ -n "$servers" ]
then
handler
else
printf "\\a${RED}You forgot to specify a server!${EOL}"
usage
fi
;;
"status")
if [ -n "$servers" ]
then
handler # if not-null pass to handler to process server as normal
else
status # if no server specified, provide generic status
fi
;;
"update")
if [ -n "$servers" ]
then
handler # if not-null pass to handler to process server as normal
else
printf "\\a${RED}Downloading games definition file. Please re-run your command${EOL}${EOL}"
wget -O .games http://tmfga.me/games-definitions # if no server specified, update games definitions file
chmod +x .games
fi
;;
"help")
usage
;;
*)
printf "\\a${RED}Command '${task}' not recognised!${EOL}"
printf "${EOL}"
usage
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment