[help]fail2ban 的 telegram 通知一直无法成功

298 天前
 justincnn

听了论坛的话,在 vps 装了 fail2ban 之后,已经清净了好多, 为了方便管理,想装了 telegram 的通知,搜索了教程之后,发现一直无法成功, V 版的大神,方便的话,帮忙看下,谢谢,

[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

在/etc/fail2ban/中新建 scripts 目录,并新增 send_telegram_notif.sh 脚本文件写入


#!/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='xxxxx' 
#此处替换为自己 Telegram 机器人 Token
# Telegram Chat ID
telegramChatID='xxxxx'#此处替换为自己的 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 on `hostname`."
;;
                stop)
                        talkToBot "Fail2ban has been stopped on `hostname`."
;;
*)
                        echo "Incorrect option"
                        exit 1;
;;
esac
elif[[ ${ban} == "y"]]; then
        talkToBot "[${jail_name}] The IP: ${ip_add_ban} has been banned on `hostname`."
        exit 0;
elif[[ ${unban} == "y"]]; then
        talkToBot "[${jail_name}] The IP: ${ip_add_unban} has been unbanned on `hostname`."
        exit 0;
else
        info
fi

执行:

chmod +x send_telegram_notif.sh

修改:/etc/fail2ban/jail.local 配置文件,将启用的 jail 的 action 下添加一个 telegram ,

action  = iptables[name=SSH,port=2202,protocol=tcp]
            telegram

重启 fail2ban

systemctl restart fail2ban
434 次点击
所在节点    Telegram
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1021048

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX