Bash Modified alert file for Raspbery Pi on Raspbian

I'm a bit new to here on Stackoverflow (which you will probably see from my reputation), but I was wondering how to make an audio alert or some other type of change to the auto-polling log file for failed login attempts for RDP sessions (in particular XRDP) on Rasbperry Pi.

Since this is a Raspberry Pi, I'm sure you can understand why I want to run it headless.

I've searched many times and seem to have found links or ways to make audible warnings when connected directly to the Pi. I want this warning to appear while running in a main silent terminal window. I would prefer a beep for new log activity (if the size of the log file has changed, beep) and then the log will be checked at regular intervals (i.e. every 30 seconds or 1 minute, etc.).

My log location is here if it helps me give me an idea on how to start doing this Bash script / var / log / xrdp.log

+3


source to share


1 answer


Here is the gist of a bash script that will do exactly what you want, over SSH, on a Raspberry Pi (or any Linux):

https://gist.github.com/free5ty1e/300adb0800ba45f3fe4e

#!/bin/bash

# xrdpLogMonitor.sh <optional timeout in seconds>
# This script will check and spit out your xrdp log file every X seconds
# (default 30 if not specified)
# If the file size has changed since your last check, your terminal will beep (system alert)

logFileName="/var/log/xrdp.log"

if [ $# -eq 0 ];
then
    echo "No arguments supplied, will use default time between log polls (30 seconds)"
    secondsBetweenLogPolls=30
else
    echo "Using supplied timeout of $1 seconds between log polls"
    secondsBetweenLogPolls=$1
fi


function updateLogModifiedTimeAndBeepIfChanged()
{
    lastLogModifiedTime=$LogModifiedTime
    LogModifiedTime="$(stat --printf="%Z" $logFileName)"
    if [ "$LogModifiedTime" != "$lastLogModifiedTime" ];
    then
        echo NEW LOG ACTIVITY CAPTURED!!!!

        #Below line creates the terminal beep
        echo -ne '\a'
    fi
}

while [  1 -lt 2 ]; do
    updateLogModifiedTimeAndBeepIfChanged
    echo "$(ls -l $logFileName)"
    echo "Polling  logfile $logFileName which was last modified at $LogModifiedTime..."

    #You will need sudo on the pi to cat this xrdp log
    sudo cat $logFileName

    #Uncomment the following line to search, for example, for "USER:" and display only those lines that contain it:
    #sudo cat $logFileName | grep USER:

    echo "$(date) <--- this is now"
    sleep $secondsBetweenLogPolls
done

      

Once you've created the xrdpLogMonitor.sh file, don't forget to install it as executable by typing:



chmod +x ./xrdpLogMonitor.sh

      

Then execute it by typing:

./xrdpLogMonitor.sh

      

+3


source







All Articles