Added failedlogins.sh

This commit is contained in:
Ze'ev Schurmann 2024-02-05 18:47:25 +02:00
parent 0f4bc39045
commit 64bf0fe310
2 changed files with 72 additions and 0 deletions

72
failedlogins.sh Normal file
View File

@ -0,0 +1,72 @@
#!/bin/bash
tgapi=""
tgch=""
nextcloudpath="/var/www/html"
wwwuser="www-data"
logmax="250"
logprefix="failedlogins"
function sendtelegram {
echo "Nextcloud $c failed logins..." > /tmp/ncfailedloginsentryheader.txt
echo "Server: $(hostname)" >> /tmp/ncfailedloginsentryheader.txt
echo "Path: $nextcloudpath" >> /tmp/ncfailedloginsentryheader.txt
echo "Date: $(date)" >> /tmp/ncfailedloginsentryheader.txt
echo "" >> /tmp/ncfailedloginsentryheader.txt
string=$(cat /tmp/ncfailedloginsentryheader.txt /tmp/ncfailedloginsentry.txt)
wget -qO- "https://api.telegram.org/bot$tgapi/sendMessage?chat_id=$tgch&text=$string" &> /dev/null
errorcode=$?
updatelog $errorcode
}
function updatelog {
logdata="Telegram Sent"
if [[ ! -z $1 ]]; then
if [[ $1 == "0" ]]; then
logerror="Successful!"
else
logerror="Sending Failed: Exit Code $1"
fi
fi
echo "$(date +%y%m%d).$(date +%H%M) - $logprefix - $logdata - $logerror" >> /var/log/nextcloud-telegram-notify.log
logsize=$(cat /var/log/nextcloud-telegram-notify.log | wc -l)
if [[ $logsize -gt $logmax ]]; then
mv /var/log/nextcloud-telegram-notify.log /tmp/nextcloud-telegram-notify.log
tail -n $logmax /tmp/nextcloud-telegram-notify.log > /var/log/nextcloud-telegram-notify.log
rm /tmp/nextcloud-telegram-notify.log
fi
}
sudo -u $wwwuser php "$nextcloudpath/occ" user:list > /tmp/ncchecknewusers.txt
array=($(cat $nextcloudpath/config/config.php | grep 'datadirectory'))
nextcloudfilespath=${array[2]:1:-2}
cat $nextcloudfilespath/nextcloud.log | grep '"level":2,' | grep '"Login failed:' > /tmp/ncfailedlogins.tmp
if [[ -f /tmp/ncfailedlogins.old ]]; then
rm -f /tmp/ncfailedlogins.changes.tmp
cat /tmp/ncfailedlogins.old /tmp/ncfailedlogins.old /tmp/ncfailedlogins.tmp | sort | uniq -u >> /tmp/ncfailedlogins.changes.tmp
rm -f /tmp/ncfailedlogins.new.*.tmp
size=$(cat /tmp/ncfailedlogins.changes.tmp | wc -l)
if [[ $size -gt 0 ]]; then
n=0
while read -r entry; do
echo "$n" "/tmp/ncfailedlogins.new.$n.tmp"
echo $entry > "/tmp/ncfailedlogins.new.$((n++)).tmp"
done < /tmp/ncfailedlogins.changes.tmp
rm -f /tmp/ncfailedloginsentry.txt
for ((c=0; c<$n; c++)); do
echo "$(cat "/tmp/ncfailedlogins.new.$c.tmp" | jq -r .message)" >> /tmp/ncfailedloginsentry.txt
echo "Time: $(cat "/tmp/ncfailedlogins.new.$c.tmp" | jq -r .time)" >> /tmp/ncfailedloginsentry.txt
echo "" >> /tmp/ncfailedloginsentry.txt
done
sendtelegram
else
echo "There are no failed logins to send..."
fi
else
echo "First run... saving current logins..."
fi
mv /tmp/ncfailedlogins.tmp /tmp/ncfailedlogins.old

BIN
images/failedlogins.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB