分享几个关于战术小队日志分析实战的脚本

双方换边脚本:

#!/bin/bash
/usr/bin/rcon -Ppswd -p21114 AdminBroadcast 服务器双方换边脚本启动
sleep 3
/usr/bin/rcon -Ppswd -p21114  ListPlayers |grep -Ev "Active Players"\|Disconnect > /home/steam/shell/put/shuf1.txt
mkdir -p /home/steam/shell/put

WC=`cat /home/steam/shell/put/shuf1.txt|wc -l`

PL=(`cat /home/steam/shell/put/shuf1.txt|awk '{print $2}'`)
COUNTER=0
PLSUM=${#PL[@]}

while true
do
  if [ $PLSUM -eq 0 ];then
   break
  fi
  if [ $COUNTER -eq $PLSUM ];then
   break
  fi
/usr/bin/rcon -Ppswd -p21114 AdminForceTeamChangeById ${PL[$COUNTER]} &
 COUNTER=$((COUNTER+1))
done


击杀检测自动踢出脚本:

#!/bin/bash
#[2023.04.20-11.12.35:776][ 87]LogSquad: Player:=TSAR= Teddyou ActualDamage=62.000004 from Light莱特上校 caused by BP_M4A1_M68_Foregrip_Tracer_C_2147479517
#[2023.04.20-11.12.35:776][ 87]LogSquad: Player:=TSAR= Teddyou ActualDamage=62.000004 from Light莱特上校 caused by BP_M4A1_M68_Foregrip_Tracer_C_2147479517
while true
do
KILL=`cat /home/steam/squad_server/SquadGame/Saved/Logs/SquadGame.log|tail -100 |grep -v null |grep -vE UH60\|SA330\|MI8\|MI17\|CH146\|MRH90\|Z8G\|UH1Y | grep -E ActualDamage | tail -1`
#KILL=`cat /home/steam/squad_server/nohup.out |grep -v null |grep -E ActualDamage |tail -1`
R2=`echo ${KILL##*from }`
U2=`echo ${R2%%' caused'*}`
R1=`echo ${KILL##*Player:}`
U1=`echo ${R1%%' ActualDamage'*}`
  if [ "$U1" != "$U2" ];then
   /usr/local/bin/mcrcon -P 21114 -p ouyangwenguang "AdminKick ${U2} 训练场不准对玩家造成伤害" ok
  fi
sleep 3
done


击杀日志记录脚本:

#!/bin/bash
while true
do
cat /home/steam/squad_server/SquadGame/Saved/Logs/SquadGame.log |grep -E ActualDamage\|"has revived"\|StartLoadingDestination |tail -500|tac|sed 's/^/<br><br>/g'|sed 's/StartLoadingDestination to: /地图更换为:/g' | sed 's/ActualDamage/输出伤害/g' | sed 's/has revived/拯救了/g' > /var/www/html/kill.html
sleep 30
done


当前用户统计脚本(crontab):

#!/bin/bash
/usr/local/bin/mcrcon -P 21114 -p pswd "ListPlayers" ok |grep -v Disconnect |awk -F \| '{$2="" ;print }'  >  /var/www/html/userinfo.html
sed -i 's/^/<br>/g' /var/www/html/userinfo.html


CGI随机打乱阵营脚本(需要上方脚本支撑):

#!/bin/bash
echo "Content-type:text/html;charset=UTF8"
echo ""
echo "指令已向服务器下发!<br>"
echo "<br>"
WC=`cat /var/www/html/userinfo.html|wc -l`
PT=`expr $WC / 2`
PL=(`cat /var/www/html/userinfo.html|awk '{print $2}'|shuf -n $PT`)
COUNTER=0
PLSUM=${#PL[@]}
echo "打乱人数为${PT}人。"
echo "<br>"
echo "<br>"
while true
do
  if [ $PT -eq 0 ];then
   break
  fi
  if [ $COUNTER -eq $PLSUM ];then
   break
  fi 
/usr/local/bin/mcrcon -P 21114 -p pwsd "AdminForceTeamChangeById ${PL[$COUNTER]}" ok
 COUNTER=$((COUNTER+1))
done

前台代码:

<li><a href="http://127.0.0.1:8088/cgi-bin/shuf.sh" target="_blank">随机打乱(即时)</a></li>


CGI前台交互设置服务器密码脚本:

#!/bin/bash
echo "Content-type:text/html;charset=UTF8"
echo ""
echo "指令已向服务器下发!<br>"

PASS=`echo "$QUERY_STRING"|sed 's/pass=//'`

/usr/local/bin/mcrcon -P 21114 -p pswd  "AdminSetServerPassword \"$PASS\" " ok

前台代码:

<div style="color:#696969"><a name="top" id="top_anchor"> <p>设置服务器密码<ul>
<form role="search" method="get" action="http://127.0.0.1:8088/cgi-bin/pswd.sh">
<font style="color:#756fe9">设置密码:</font></label>
<input name="pass" placeholder="留空为取消密码" >
<input type="submit" value="提交"></ul>
</svg></button></div></form>


CGI前台交互设置地图脚本:

#!/bin/bash
echo "Content-type:text/html;charset=UTF8"
echo ""
echo "指令已向服务器下发!<br>"

MAP=`echo "$QUERY_STRING"|sed 's/map=//'`
/usr/local/bin/mcrcon -P 21114 -p pswd -w 1 "AdminChangeLayer $MAP" ok

前台代码:

<li><form role="search" method="get" action="http://127.0.0.1:8088/cgi-bin/AdminChangeLayer.sh">
<font style="color:#756fe9">自定义地图:<a href="http://squad.teddyou.com/squad-map.html" target="_blank">[查询地图名]</a>
</font></label>
<input type="search" name="map" value="" value="" placeholder="请输入完整地图名称" required="">
<input type="submit" value="提交"></ul></li>
</svg></button></div></form>


服务器自动重启脚本升级版,实现以下能力:

1、此版本能检测服务器是否假在线并且重启。

2、通过日志大小(大于50M)判断服务器是否应该重启并在无人的时候执行。

3、判断进程是否掉线,并自动重启。

4、自动保存上次重启时的游戏日志。

#!/bin/bash
#20230516.Teddyou.服务器日志大小检测,崩溃自动重启脚本
#服务器序号
SERV=1
#网卡IP
SERIP=10.0.0.1
#服务器抓包端口号
SERP='26301'
#rcon密码
RCP=123456

#载入用户变量
source /home/steam/.bash_profile
DATE=`date +"%Y%m%d%H%M%S"`

#抓包判断服务器在线是否在线
sudo timeout 10 tcpdump -i enp3s0 -n udp port ${SERP} -c 50 | grep $SERP > /home/steam/restart/fl$SERV
IPS=(`cat /home/steam/restart/fl$SERV | awk '{print $5}' | awk -F: '{print $1}'`)
COUNTER=0
IPSP=0
IPSUM=${#IPS[@]}
while true
do
  if [ $IPSUM -eq 0 ];then
   break
  fi
  if [ ${IPS[$COUNTER]} != "${SERIP}.${SERP}" ];then
   IPSP=$((IPSP+1))
  fi
  COUNTER=$((COUNTER+1))
  if [ $COUNTER -eq $IPSUM ];then
   break
  fi 
done

if [ $IPSP -eq 0 ];then
 kill `ps -ef|grep server${SERV}\/SquadGame |grep -v grep |awk '{print $2}'`
fi 

#日志大小判断
DAT=`ls -l /home/steam/squad_server${SERV}/SquadGame/Saved/Logs/SquadGame.log | awk '{print $5}'`
USER=`/usr/bin/rcon -P$RCP -p2${SERV}114 ListPlayers | grep Name | wc -l`
  if [ $DAT -gt 52582400 ];then
    if [ $USER -eq 0 ];then
     kill `ps -ef|grep server${SERV}\/SquadGame |grep -v grep |awk '{print $2}'`
    fi
  fi

#判断日志是否异常,程序是否在线执行重启
 SD=`tail -10 /home/steam/squad_server${SERV}/nohup.out|grep Shutdown|wc -l`
 if [ $SD -eq 1 ]; then
  kill `ps -ef|grep server${SERV}\/SquadGame |grep -v grep |awk '{print $2}'`
 fi

SQ=`ps -ef | grep server${SERV}\/SquadGame | grep -v grep | grep steam | wc -l`
if [ $SQ -ne 2 ]; then
  kill `ps -ef|grep server${SERV}\/SquadGame |grep -v grep |awk '{print $2}'`
  cd /home/steam/squad_server${SERV}
  cp -rp /home/steam/squad_server${SERV}/nohup.out /home/steam/squad_server${SERV}/logs/squad.log.${DATE}
  nohup bash ./start.sh > /home/steam/squad_server${SERV}/nohup.out &
fi

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。