bashscripts/updates_notifier.sh

37 lines
1.2 KiB
Bash
Raw Permalink Normal View History

2023-01-21 00:33:52 +01:00
#!/bin/bash
# uncomment when use script from cron
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
2023-01-25 00:21:59 +01:00
#verify run as root or sudo
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
2023-01-21 00:33:52 +01:00
# vars gotify
2024-05-02 10:56:18 +02:00
readonly GOTIFY_TOKEN="<TOKEN_GOTIFY_APP>"
readonly GOTIFY_HOST="<GOTIFY_HOST>"
readonly GOTIFY_PRIORITY=5
2023-01-21 00:33:52 +01:00
2023-01-21 01:49:59 +01:00
apt update -qq
2023-01-21 00:33:52 +01:00
#https://askubuntu.com/questions/269606/apt-get-count-the-number-of-updates-available
num=`apt-get -q -y --ignore-hold --allow-change-held-packages --allow-unauthenticated -s dist-upgrade | /bin/grep ^Inst | wc -l`;
if [ "$num" -eq "0" ]; then
echo "No updates";
exit;
else
#echo "There are $num updates";
updates=`apt-get --fix-missing --no-download -s dist-upgrade -V | grep '=>' | awk '{print$1}';`
updates=$(echo $updates|tr -d '\n')
OUT="There are **$num** updates: \r"
OUT="${OUT} Packages: *$updates*"
2024-05-02 10:56:18 +02:00
# send gotify notification
TITLE="$(hostname) pending updates"
EXTRAS="{\"client::display\": {\"contentType\": \"text/markdown\"}}"
curl -X POST "$GOTIFY_HOST/message?token=$GOTIFY_TOKEN" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"message\": \"${OUT}\", \"priority\": ${GOTIFY_PRIORITY}, \"title\": \"${TITLE}\", \"extras\": ${EXTRAS} }"
2023-01-21 00:33:52 +01:00
fi