source: scripts/usr/local/lib/7dtd/monitor-log.sh@ 497

Last change on this file since 497 was 484, checked in by alloc, 12 months ago

Scripts: Command execution monitor fixes

  • Property svn:executable set to *
File size: 9.5 KB
RevLine 
[17]1#!/bin/bash
2
[258]3# Copyright 2016 Christian 'Alloc' Illy
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
[17]18. /usr/local/lib/7dtd/common.sh
[67]19. /usr/local/lib/7dtd/playerlog.sh
[17]20
21if [ $(isValidInstance $1) -eq 0 ]; then
22 echo "No instance given or not a valid instance!"
23 return
24fi
25
26INSTANCE=$1
[240]27LOGTIMESTAMP=$2
28LOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_output_log.txt
29CHATLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_chat.log
30COMMANDLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_commandExecution.log
[17]31
32timestamp() {
33 date '+%Y.%m.%d %H:%M:%S'
34}
35
36handleConnect() {
[234]37 local entityId="$1"
38 local name="$2"
[482]39 local platformId="$3"
40 local crossId="$4"
41 local ip="$5"
42 local ownerId="$6"
[17]43
[482]44 logPlayerConnect $INSTANCE "$entityId" "$name" "$platformId" "$crossId" "$ip" "$ownerId"
[17]45
[259]46 for H in $(getHooksFor playerConnect $INSTANCE); do
[482]47 $H $INSTANCE "$entityId" "$name" "$platformId" "$ip" "$ownerId" "$crossId"
[17]48 done
49}
50
51handleDisconnect() {
[482]52 local entityId="$1"
[17]53
[87]54 logPlayerDisconnect $INSTANCE "$entityId"
55
[259]56 for H in $(getHooksFor playerDisconnect $INSTANCE); do
[482]57 $H $INSTANCE "$entityId" "$NICKNAME" "$PLATFORMID" "$IP" "$OWNERID" "$CROSSID"
[17]58 done
59}
60
[308]61handlePlayerSpawnedInWorld() {
62 local entityId="$1"
[482]63 local platformId="$2"
64 local crossId="$3"
65 local ownerId="$4"
66 local playerName="$5"
67 local reason="$6"
68 local position="$7"
[308]69
70 for H in $(getHooksFor playerSpawned $INSTANCE); do
[482]71 $H $INSTANCE "$entityId" "$platformId" "$ownerId" "$playerName" "$reason" "$position" "$crossId"
[308]72 done
73}
74
[62]75handleChat() {
[344]76 echo "$(timestamp): $1: $2 (SteamID $3, EntityID $4, Target $5)" >> $CHATLOG
[62]77
[259]78 for H in $(getHooksFor chat $INSTANCE); do
[344]79 $H $INSTANCE "$1" "$2" "$3" "$4" "$5"
[275]80 done
81}
82
83handleGmsg() {
84 echo "$(timestamp): GMSG: $1" >> $CHATLOG
85
86 for H in $(getHooksFor gmsg $INSTANCE); do
[63]87 $H $INSTANCE "$1"
[62]88 done
89}
90
[87]91handleRemoteCommand() {
92 local cmd="$1"
93 local name="$2"
[484]94 local clientInfo="$3"
[87]95
[484]96 echo "$(timestamp): Player \"$name\" executed \"$cmd\" (client $clientInfo)" >> $COMMANDLOG
[87]97
[259]98 for H in $(getHooksFor remoteCommand $INSTANCE); do
[87]99 $H $INSTANCE "$cmd" "$name"
100 done
101}
102
[484]103handleRemoteCommandDenied() {
104 local cmd="$1"
105 local name="$2"
106 local clientInfo="$3"
107
108 echo "$(timestamp): Denied \"$name\" to exec command \"$cmd\" (client $clientInfo)" >> $COMMANDLOG
109
110 for H in $(getHooksFor remoteCommandDenied $INSTANCE); do
111 $H $INSTANCE "$cmd" "$name"
112 done
113}
114
[87]115handleTelnetCommand() {
116 local cmd="$1"
117 local ip="$2"
118
119 echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG
120
[259]121 for H in $(getHooksFor telnetCommand $INSTANCE); do
[87]122 $H $INSTANCE "$cmd" "$ip"
123 done
124}
125
126
[67]127if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
128 mkdir "$(getInstancePath $INSTANCE)/logs"
129fi
130
131setAllPlayersOffline
132
[219]133rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
[87]134rm $(getInstancePath $INSTANCE)/logs/current_chat.log
135rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
[219]136ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
[87]137ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
138ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
139
[18]140sleep 5
141
[17]142NOBUF="stdbuf -e0 -o0"
143
[220]144$NOBUF tail -n 5000 -F $LOG |
[17]145$NOBUF tr '\\' '/' |
146$NOBUF tr -d '\r' |
147$NOBUF grep -v "^(Filename: " |
[219]148$NOBUF sed -r 's/^[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+ [0-9]+[.,][0-9]+ [A-Z]+ (.*)$/\1/' |
[17]149while read line ; do
150 if [ -n "$line" ]; then
[482]151 #Player connected, entityid=1278, name=termo2, pltfmid=Steam_76561197997439820, crossid=EOS_eab...421, steamOwner=Steam_76561197997439820, ip=178.203.27.140
[298]152 #Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$
[87]153 if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
[482]154 entityId=$(expr "$line" : 'Player connected, entityid=\([0-9]*\), name=.*, pltfmid=.*, crossid=.*, steamOwner=.*, ip=[a-fA-F:0-9.]*$')
155 playerName=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=\(.*\), pltfmid=.*, crossid=.*, steamOwner=.*, ip=[a-fA-F:0-9.]*$')
156 platformId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, pltfmid=\(.*\), crossid=.*, steamOwner=.*, ip=[a-fA-F:0-9.]*$')
157 crossId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, pltfmid=.*, crossid=\(.*\), steamOwner=.*, ip=[a-fA-F:0-9.]*$')
158 steamOwner=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, pltfmid=.*, crossid=.*, steamOwner=\(.*\), ip=[a-fA-F:0-9.]*$')
159 ip=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, pltfmid=.*, crossid=.*, steamOwner=.*, ip=\([a-fA-F:0-9.]*\)$')
[17]160 sleep 1
[482]161 handleConnect "$entityId" "$playerName" "$platformId" "$crossId" "$ip" "$steamOwner"
162 unset entityId playerName platformId crossId steamOwner ip
[234]163 #Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$
[308]164 elif [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then
[482]165 entityId=$(expr "$line" : "Player disconnected: EntityID=\(.*\), PltfmId='.*$")
166 handleDisconnect "$entityId"
167 unset entityId
168 #PlayerSpawnedInWorld (reason: .*, position: .*, .*, .*): EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='.*', ClientNumber='.*'
[308]169 elif [ -n "$(echo "$line" | grep '^PlayerSpawnedInWorld ')" ]; then
[482]170 reason=$(expr "$line" : "PlayerSpawnedInWorld (reason: \(.*\), position: .*, .*, .*): EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='.*', ClientNumber='.*'$")
171 position=$(expr "$line" : "PlayerSpawnedInWorld (reason: .*, position: \(.*, .*, .*\)): EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='.*', ClientNumber='.*'$")
172 entityId=$(expr "$line" : "PlayerSpawnedInWorld (reason: .*, position: .*, .*, .*): EntityID=\(.*\), PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='.*', ClientNumber='.*'$")
173 platformId=$(expr "$line" : "PlayerSpawnedInWorld (reason: .*, position: .*, .*, .*): EntityID=.*, PltfmId='\(.*\)', CrossId='.*', OwnerID='.*', PlayerName='.*', ClientNumber='.*'$")
174 crossId=$(expr "$line" : "PlayerSpawnedInWorld (reason: .*, position: .*, .*, .*): EntityID=.*, PltfmId='.*', CrossId='\(.*\)', OwnerID='.*', PlayerName='.*', ClientNumber='.*'$")
175 ownerId=$(expr "$line" : "PlayerSpawnedInWorld (reason: .*, position: .*, .*, .*): EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='\(.*\)', PlayerName='.*', ClientNumber='.*'$")
176 playerName=$(expr "$line" : "PlayerSpawnedInWorld (reason: .*, position: .*, .*, .*): EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='\(.*\)', ClientNumber='.*'$")
177 handlePlayerSpawnedInWorld "$entityId" "$platformId" "$crossId" "$ownerId" "$playerName" "$reason" "$position"
178 unset reason position entityId platformId crossId ownerId playerName
[87]179 #GMSG: .*$
[308]180 elif [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
[62]181 msg=$(expr "$line" : 'GMSG: \(.*\)$')
[275]182 handleGmsg "$msg"
[62]183 unset msg
[344]184 #Chat (from '<steamid>', entity id '<entityid>', to '<targettype>'): '<senderchatname>': <msg>
185 elif [ -n "$(echo "$line" | grep -E '^Chat .+')" ]; then
[482]186 steamId=$(expr "$line" : "Chat (from '\(.*\)', entity id '.*', to '.*'): '.*': .*$")
187 entityId=$(expr "$line" : "Chat (from '.*', entity id '\(.*\)', to '.*'): '.*': .*$")
188 targetType=$(expr "$line" : "Chat (from '.*', entity id '.*', to '\(.*\)'): '.*': .*$")
189 name=$(expr "$line" : "Chat (from '.*', entity id '.*', to '.*'): '\(.*\)': .*$")
190 msg=$(expr "$line" : "Chat (from '.*', entity id '.*', to '.*'): '.*': \(.*\)$")
[344]191 handleChat "$name" "$msg" "$steamId" "$entityId" "$targetType"
192 unset name msg steamId entityId targetType
[234]193 #Executing command ".*" from client ".*"$
[308]194 elif [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then
[484]195 cmd=$(expr "$line" : "Executing command '\(.*\)' from client EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='.*', ClientNumber='.*'$")
196 clientInfo=$(expr "$line" : "Executing command '.*' from client \(.*\)$")
197 playerName=$(expr "$line" : "Executing command '.*' from client EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='\(.*\)', ClientNumber='.*'$")
198 handleRemoteCommand "$cmd" "$playerName" "$clientInfo"
199 unset cmd playerName clientInfo
200 #Denying command ".*" from client ".*"$
201 elif [ -n "$(echo "$line" | grep '^Denying command '.*' from client')" ]; then
202 cmd=$(expr "$line" : "Denying command '\(.*\)' from client EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='.*', ClientNumber='.*'$")
203 clientInfo=$(expr "$line" : "Denying command '.*' from client \(.*\)$")
204 playerName=$(expr "$line" : "Denying command '.*' from client EntityID=.*, PltfmId='.*', CrossId='.*', OwnerID='.*', PlayerName='\(.*\)', ClientNumber='.*'$")
205 handleRemoteCommandDenied "$cmd" "$playerName" "$clientInfo"
206 unset cmd playerName clientInfo
[234]207 #Executing command ".*" by Telnet from .*$
[308]208 elif [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then
[234]209 cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$")
210 ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$")
[87]211 handleTelnetCommand "$cmd" "$ip"
212 unset cmd ip
[17]213 fi
214 fi
215done
Note: See TracBrowser for help on using the repository browser.