-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-notify.sh
63 lines (47 loc) · 1.56 KB
/
setup-notify.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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 <ntfy_url> <basic_auth> <topic> <text message>\"
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 /"