Created
December 15, 2023 00:47
-
-
Save GGontijo/e9cc444e3090b5f59b5ff1c5ae6018ce to your computer and use it in GitHub Desktop.
fail2ban notify ban through telegram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#/etc/fail2ban/scripts/fail2ban-telegram.sh | |
# Sends text messages using Telegram | |
# to alert webmaster of banning. | |
# Require one argument, one of the following | |
# start | |
# stop | |
# ban | |
# unban | |
# Optional second argument: Ip for ban/unband | |
# Display usage information | |
function show_usage { | |
echo "Usage: $0 action <ip>" | |
echo "Where action start, stop, ban, unban" | |
echo "and IP is optional passed to ban, unban" | |
exit | |
} | |
# Send notification | |
function send_msg { | |
apiToken="TELEGRAM_TOKEN" | |
chatId="TELEGRAM_CHAT_ID" | |
url="https://api.telegram.org/bot$apiToken/sendMessage" | |
curl -s -X POST $url -d chat_id=$chatId -d text="$1" | |
exit | |
} | |
# Check for script arguments | |
if [ $# -lt 1 ] | |
then | |
show_usage | |
fi | |
# Take action depending on argument | |
if [ "$1" = 'start' ] | |
then | |
msg='Fail2ban+just+started.' | |
send_msg $msg | |
elif [ "$1" = 'stop' ] | |
then | |
msg='Fail2ban+just+stoped.' | |
send_msg $msg | |
elif [ "$1" = 'ban' ] | |
then | |
msg=$([ "$2" != '' ] && echo "Fail2ban+just+banned+$2" || echo 'Fail2ban+just+banned+an+ip.' ) | |
send_msg $msg | |
elif [ "$1" = 'unban' ] | |
then | |
msg=$([ "$2" != '' ] && echo "Fail2ban+just+unbanned+$2" || echo "Fail2ban+just+unbanned+an+ip." ) | |
send_msg $msg | |
else | |
show_usage | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/etc/fail2ban/action.d/telegram.conf | |
# Fail2Ban configuration file | |
# | |
# Author: MushaGH | |
# | |
# | |
[Definition] | |
# Option: actionstart | |
# Notes.: command executed once at the start of Fail2Ban. | |
# Values: CMD | |
# | |
actionstart = /etc/fail2ban/scripts/fail2ban-telegram.sh start | |
# Option: actionstop | |
# Notes.: command executed once at the end of Fail2Ban | |
# Values: CMD | |
# | |
actionstop = /etc/fail2ban/scripts/fail2ban-telegram.sh stop | |
# Option: actioncheck | |
# Notes.: command executed once before each actionban command | |
# Values: CMD | |
# | |
actioncheck = | |
# Option: actionban | |
# Notes.: command executed when banning an IP. Take care that the | |
# command is executed with Fail2Ban user rights. | |
# Tags: See jail.conf(5) man page | |
# Values: CMD | |
# | |
actionban = /etc/fail2ban/scripts/fail2ban-telegram.sh ban <ip> | |
# Option: actionunban | |
# Notes.: command executed when unbanning an IP. Take care that the | |
# command is executed with Fail2Ban user rights. | |
# Tags: See jail.conf(5) man page | |
# Values: CMD | |
# | |
actionunban = /etc/fail2ban/scripts/fail2ban-telegram.sh unban <ip> | |
[Init] | |
init = 123 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment