Skip to content

Instantly share code, notes, and snippets.

@waggz81
Last active February 27, 2021 22:50
Show Gist options
  • Select an option

  • Save waggz81/0355b4bed02dd214b16afb032acb17df to your computer and use it in GitHub Desktop.

Select an option

Save waggz81/0355b4bed02dd214b16afb032acb17df to your computer and use it in GitHub Desktop.

Revisions

  1. waggz81 revised this gist Feb 17, 2021. 1 changed file with 53 additions and 35 deletions.
    88 changes: 53 additions & 35 deletions logwatcher.sh
    Original file line number Diff line number Diff line change
    @@ -8,49 +8,67 @@ WEBHOOK_URL="CHANGEME"
    STEAMKEY="CHANGEME"
    ##########################

    # script logic
    ### 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 )

    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")
    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")
    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
    # 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
  2. waggz81 revised this gist Feb 16, 2021. 1 changed file with 23 additions and 5 deletions.
    28 changes: 23 additions & 5 deletions logwatcher.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,17 @@
    #!/bin/bash

    FILE="/full/path/to/log"
    WEBHOOK_URL="CHANGME"
    ##########################
    # Change these variables #
    ##########################
    FILE="/path/to/console.log"
    WEBHOOK_URL="CHANGEME"
    STEAMKEY="CHANGEME"
    ##########################

    # script logic
    declare -A SteamID
    lastSteamID=null
    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
    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"
    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")
  3. waggz81 revised this gist Feb 16, 2021. No changes.
  4. waggz81 created this gist Feb 16, 2021.
    38 changes: 38 additions & 0 deletions logwatcher.sh
    Original 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