Last active
February 27, 2021 22:50
-
-
Save waggz81/0355b4bed02dd214b16afb032acb17df to your computer and use it in GitHub Desktop.
Revisions
-
waggz81 revised this gist
Feb 17, 2021 . 1 changed file with 53 additions and 35 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,49 +8,67 @@ WEBHOOK_URL="CHANGEME" STEAMKEY="CHANGEME" ########################## ### script logic ### # array to store steam ids and map to player names declare -A SteamID # initialize global variables lastSteamID=0 havePlayerName=0 # begin watching the log file and check each new line tail -Fn0 $FILE | \ while read -r line ; do # check for the matching lines we want to look into further if grep -iq "Closing socket\|Got handshake from client\|Got character ZDOID from" <<< "$line" then # tokenize the line sp;it by colons so easier to process IFS=':' tokens=( $line ) # we're only interested in the text of the log entry entry=$(echo "${tokens[3]}" | xargs) # get rid of windows carriage return entry="$(echo $entry | sed 's/\r//g')" if grep -iq "Got handshake from client" <<< "$entry" then # grab everything in the entry past the matching text and mark that we have a recent steam id lastSteamID=$(sed -n -e 's/^.*Got handshake from client //p' <<< "$entry") haveSteamID=1 elif grep -iq "Got character ZDOID from" <<< "$entry" then # grab everything in the entry past the matching text and mark that we have a recent player name playerName=$(sed -n -e 's/^.*Got character ZDOID from //p' <<< "$entry") havePlayerName=1 # add the name and id to the array for recall later SteamID[$lastSteamID]=$playerName # if we have both a recent name and recent steam id go ahead and assume we have a new client that connected and send alert if [[ havePlayerName -eq 1 && haveSteamID -eq 1 ]] then # grab the steam id profile url and name response=$(curl -Gs -d "steamids=${lastSteamID}" -d "key=${STEAMKEY}" https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/) profile=$(jq '.response.players[0].profileurl' <<< $response | tr -d "\"") personaname=$(jq '.response.players[0].personaname' <<< $response | tr -d "\"") # output to terminal echo "Player ${playerName} (probably ${lastSteamID}) has joined the server - Steam profile ${personaname} (${profile})" # send to Discord curl -X POST --data "{\"content\": \"Player ${playerName} (probably ${lastSteamID}) has joined the server - Steam profile ${personaname} (${profile})\"}" --header "Content-Type:application/json" "$WEBHOOK_URL" # reset variables so we're not sending alerts without it being a new connection havePlayerName=0 haveSteamID=0 fi elif grep -iq "Closing socket" <<< "$entry" then # grab everything in the entry past the matching text socket=$(sed -n -e 's/^.*Closing socket //p' <<< "$entry") # check that it's an actual socket closing if [ "$socket" != "0" ] then # output to terminal echo "SteamID ${socket} left the server (was probably ${SteamID[$socket]})" # send to Discord curl -X POST --data "{\"content\": \"SteamID ${socket} left the server (was probably ${SteamID[$socket]})\"}" --header "Content-Type:application/json" "$WEBHOOK_URL" fi else # this should never happen but is included for failover echo "unhandled entry:" "$entry" fi fi done -
waggz81 revised this gist
Feb 16, 2021 . 1 changed file with 23 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,17 @@ #!/bin/bash ########################## # Change these variables # ########################## FILE="/path/to/console.log" WEBHOOK_URL="CHANGEME" STEAMKEY="CHANGEME" ########################## # script logic declare -A SteamID lastSteamID=0 havePlayerName=0 tail -Fn0 $FILE | \ while read -r line ; do @@ -17,12 +25,22 @@ while read -r line ; do if grep -iq "Got handshake from client" <<< "$entry" then lastSteamID=$(sed -n -e 's/^.*Got handshake from client //p' <<< "$entry") haveSteamID=1 elif grep -iq "Got character ZDOID from" <<< "$entry" then playerName=$(sed -n -e 's/^.*Got character ZDOID from //p' <<< "$entry") havePlayerName=1 SteamID[$lastSteamID]=$playerName if [[ havePlayerName -eq 1 && haveSteamID -eq 1 ]] then response=$(curl -Gs -d "steamids=${lastSteamID}" -d "key=${STEAMKEY}" https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/) profile=$(jq '.response.players[0].profileurl' <<< $response | tr -d "\"") personaname=$(jq '.response.players[0].personaname' <<< $response | tr -d "\"") echo "Player ${playerName} (probably ${lastSteamID}) has joined the server - Steam profile ${personaname} (${profile})" curl -X POST --data "{\"content\": \"Player ${playerName} (probably ${lastSteamID}) has joined the server - Steam profile ${personaname} (${profile})\"}" --header "Content-Type:application/json" "$WEBHOOK_URL" havePlayerName=0 haveSteamID=0 fi elif grep -iq "Closing socket" <<< "$entry" then socket=$(sed -n -e 's/^.*Closing socket //p' <<< "$entry") -
waggz81 revised this gist
Feb 16, 2021 . No changes.There are no files selected for viewing
-
waggz81 created this gist
Feb 16, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ #!/bin/bash FILE="/full/path/to/log" WEBHOOK_URL="CHANGME" declare -A SteamID lastSteamID=null tail -Fn0 $FILE | \ while read -r line ; do if grep -iq "Closing socket\|Got handshake from client\|Got character ZDOID from" <<< "$line" then IFS=':' tokens=( $line ) entry=$(echo "${tokens[3]}" | xargs) entry="$(echo $entry | sed 's/\r//g')" if grep -iq "Got handshake from client" <<< "$entry" then lastSteamID=$(sed -n -e 's/^.*Got handshake from client //p' <<< "$entry") elif grep -iq "Got character ZDOID from" <<< "$entry" then playerName=$(sed -n -e 's/^.*Got character ZDOID from //p' <<< "$entry") SteamID[$lastSteamID]=$playerName echo "Player ${playerName} (probably ${lastSteamID}) has joined the server" curl -X POST --data "{\"content\": \"Player ${playerName} (probably ${lastSteamID}) has joined the server\"}" --header "Content-Type:application/json" "$WEBHOOK_URL" elif grep -iq "Closing socket" <<< "$entry" then socket=$(sed -n -e 's/^.*Closing socket //p' <<< "$entry") if [ "$socket" != "0" ] then echo "SteamID ${socket} left the server (was probably ${SteamID[$socket]})" curl -X POST --data "{\"content\": \"SteamID ${socket} left the server (was probably ${SteamID[$socket]})\"}" --header "Content-Type:application/json" "$WEBHOOK_URL" fi else echo "unhandled entry:" "$entry" fi fi done