From 4f99e00b9c4c4fec4115e5a30129862f0af7df5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Fri, 1 Mar 2024 10:47:28 +0100 Subject: [PATCH] Add the bash script for login and update the readme.md --- README.md | 43 +++++++++++++++++++++++++++++++++ setup-notify.sh | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 setup-notify.sh diff --git a/README.md b/README.md index 9cda74a..017fc2c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ # ntfy_alerts Personal ntfy alerts system + + +# Python ntfy.py +## Description: +This script is used to watch the github repos and send a notification to the ntfy server when a new release is published. +## Utilisation: +````python +python ntfy.py +```` +auth: can be generataed by the folowing command: echo -n 'username:password' | base64 +ntfy_url: the url of the ntfy server including the topic + +Acctualy the watched repos list is hardcoded in the ntfy.py file under the name of watched_repos_list. +## TODO: +- [ ] Add the watched repos list as a parameter +- [ ] Add the watched repos list as a file +- [ ] Add the watched repos list as a database +- [ ] Add the watched repos list as a web interface +# Bash setup-notify.sh +## Description: +This script is used to setup the ntfy notification system on ssh login for a new server. +## Utilisation: +````bash +bash setup-notify.sh +```` +ntfy_url: the url of the ntfy server +username: the username of the user +password: the password of the user +topic: the topic of the notification + +This script will create a send-notify.sh in the root of your disk and add the login-notify.sh to the /etc/profile.d/ folder. +# Bash send-notify.sh +## Description: +This script is used to send a notification to the ntfy server. +## Utilisation: +````bash +bash send-notify.sh +```` +ntfy_url: the url of the ntfy server +basic_auth: the basic auth of the user +topic: the topic of the notification +message: the message of the notification + diff --git a/setup-notify.sh b/setup-notify.sh new file mode 100644 index 0000000..c17cae9 --- /dev/null +++ b/setup-notify.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# Script pour créer les fichiers login-notify.sh et send-notify.sh dans /etc/profile.d/ + +# Contenu du fichier login-notify.sh +LOGIN_NOTIFY_CONTENT="#!/bin/bash + +NTFY_URL=\$1 +username=\$2 +password=\$3 +Basic=\$(echo -n \"\$username:\$password\" | base64) +TOPIC=\$4 + +IP=\"\$(echo \"\${SSH_CONNECTION}\" | awk -F' ' '{print \$1}')\" # ne fonctionne pas pour une connexion locale +DATE=\"\$(date)\" +NAME=\"\$(whoami)\" +NB_USERS=\$(who | wc -l) + +MESSAGE=\" +New login to \${HOSTNAME} server! +\\\"\${NAME}\\\" from \\\"\${IP}\\\" +\${NB_USERS} users connected +\${DATE} +\" + +/send-notify.sh \"\${NTFY_URL}\" \"\${Basic}\" \"\${TOPIC}\" \"\${MESSAGE}\" +" + +# Contenu du fichier send-notify.sh +SEND_NOTIFY_CONTENT="#!/bin/bash + +set -eu + +if [ \"\$#\" -ne 4 ]; then + echo \"Usage: \$0 \" + echo \"Help basic_auth: \\\"echo -n 'testuser:fakepassword' | base64\\\"\" + exit 1 +fi + +NTFY_URL=\$1 +BASIC_AUTH=\$2 +TOPIC=\$3 +TEXT=\$4 + +RES=\$(curl -i -s -X POST -H \"Authorization: Basic \${BASIC_AUTH}\" -d \"\${TEXT}\" \"\${NTFY_URL}/\${TOPIC}\") +STATUS_CODE=\$(echo \"\$RES\" | head -n 1 | awk -F' ' '{print \$2}') + +if [[ \$STATUS_CODE -ne 200 ]] ; then + echo \"error while sending alert\" + echo \"\${RES}\" + exit 1 +fi +" + +# Création des fichiers +echo "$LOGIN_NOTIFY_CONTENT" | sudo tee /etc/profile.d/login-notify.sh > /dev/null +echo "$SEND_NOTIFY_CONTENT" | sudo tee /send-notify.sh > /dev/null + +# Attribution des permissions +sudo chmod +x /etc/profile.d/login-notify.sh /send-notify.sh + +echo "Fichiers créés avec succès dans /etc/profile.d/ et /" +