-
Notifications
You must be signed in to change notification settings - Fork 8
/
send_telegram_notif.sh
82 lines (74 loc) · 1.68 KB
/
send_telegram_notif.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# Version 1.0
# Send Fail2ban notifications using a Telegram Bot
# Add to the /etc/fail2ban/jail.conf:
# [sshd]
# ***
# action = iptables[name=SSH, port=22, protocol=tcp]
# telegram
# Create a new file in /etc/fail2ban/action.d with the following information:
# [Definition]
# actionstart = /etc/fail2ban/scripts/send_telegram_notif.sh -a start
# actionstop = /etc/fail2ban/scripts/send_telegram_notif.sh -a stop
# actioncheck =
# actionban = /etc/fail2ban/scripts/send_telegram_notif.sh -n <name> -b <ip>
# actionunban = /etc/fail2ban/scripts/send_telegram_notif.sh -n <name> -u <ip>
#
# [Init]
# init = 123
# Telegram BOT Token
telegramBotToken='YOUR_BOT_TOKEN'
# Telegram Chat ID
telegramChatID='YOUR_CHAT_ID'
function talkToBot() {
message=$1
curl -s -X POST https://api.telegram.org/bot${telegramBotToken}/sendMessage -d text="${message}" -d chat_id=${telegramChatID} > /dev/null 2>&1
}
if [ $# -eq 0 ]; then
echo "Usage $0 -a ( start || stop ) || -b \$IP || -u \$IP"
exit 1;
fi
while getopts "a:n:b:u:" opt; do
case "$opt" in
a)
action=$OPTARG
;;
n)
jail_name=$OPTARG
;;
b)
ban=y
ip_add_ban=$OPTARG
;;
u)
unban=y
ip_add_unban=$OPTARG
;;
\?)
echo "Invalid option. -$OPTARG"
exit 1
;;
esac
done
if [[ ! -z ${action} ]]; then
case "${action}" in
start)
talkToBot "Fail2ban has been started"
;;
stop)
talkToBot "Fail2ban has been stopped"
;;
*)
echo "Incorrect option"
exit 1;
;;
esac
elif [[ ${ban} == "y" ]]; then
talkToBot "[${jail_name}] The IP: ${ip_add_ban} has been banned"
exit 0;
elif [[ ${unban} == "y" ]]; then
talkToBot "[${jail_name}] The IP: ${ip_add_unban} has been unbanned"
exit 0;
else
info
fi