You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5.3 KiB
5.3 KiB
Alias | Tag | Date | DocType | Hierarchy | TimeStamp | Link | location | CollapseMetaTable | |||
---|---|---|---|---|---|---|---|---|---|---|---|
|
2022-03-13 | WebClipping | 2022-03-13 | https://github.com/deividgdt/fail2ban_telegram_notifications | true |
Parent:: Selfhosting, VPS Console Dialogue
name Save
type command
action Save current file
id Save
^button-fail2bantelegramnotificationsNSave
GitHub - deividgdt/fail2ban_telegram_notifications: Sending fail2ban notifications using a Telegram bot
Fail2ban Telegram Notifications
Sending Configuring Fail2ban notifications using a Telegram bot
Installation and configuration
- Add the following two lines, for example, to SSHD in the file /etc/fail2ban/jail.conf, make sure to tab the word telegram.
~~~bash
action = iptables[name=SSH, port=22, protocol=tcp]
telegram
~~~
Example:
-
Download the file telegram.conf and move it to /etc/fail2ban/action.d/
-
Download the file send_telegram_notif.sh move it to /etc/fail2ban/scripts/
-
Modify the file /etc/fail2ban/scripts/send_telegram_notif.sh and add your Token and your Chat ID:
~~~bash
telegramBotToken=YOUR_BOT_TOKEN
telegramChatID=YOUR_CHAT_ID
~~~
- Make the file executable
~~~bash
chmod +x /etc/fail2ban/scripts/send\_telegram\_notif.sh
~~~
- Restart the Configuring Fail2ban service and enjoy!
~~~bash
systemctl restart fail2ban
~~~
Usage
- /etc/fail2ban/scripts/send_telegram_notif.sh -a [ start || stop ] || [ -n $NAME -b $IP || -n $NAME -u $IP ]"
- -a (action)
- -n (jail name)
- -b (ban)
- -u (unban)
telegram.conf
~~~bash
[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
~~~
send_telegram_notif.sh
~~~python
#!/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
~~~