Fail2ban Ban Alerts to Telegram Without a Bot Token

Fail2ban Ban Alerts to Telegram Without a Bot Token

push.tg

Fail2ban can run any command when it bans an IP, so getting a Telegram message for each ban doesn't need BotFather, a bot token or a chat ID. One small action file with a curl line is enough: push.tg turns that POST into a message in your Telegram chat.

Step 1: get your endpoint (30 seconds)

Open @trafficRobot in Telegram and press Start. You get an address like k3x9q@push.tg. Your endpoint is the part before the @: https://push.tg/k3x9q. Every example below uses k3x9q, so swap in your own key.

There's no bot to create, no chat_id to look up and no token to keep safe. Add @trafficRobot to a group and the group gets its own address, so everyone on call sees the messages.

Step 2: create the action file

Save this as /etc/fail2ban/action.d/pushtg.local:

[Definition]
norestored = 1
actionstart =
actionstop =
actioncheck =
actionban = curl -s -m 10 --data-urlencode "data=fail2ban on <fq-hostname>: banned <ip> in jail <name> after <failures> failures" "<url>"
actionunban =

[Init]
url = https://push.tg/k3x9q

Fail2ban fills in the tags in angle brackets: <ip> is the banned address, <name> is the jail (Fail2ban passes the jail name automatically), <failures> is how many matching log lines triggered the ban, and <fq-hostname> is the server's own hostname.

norestored = 1 is the same switch the bundled mail actions use: bans restored from the database after a restart don't send a second message.

Why --data-urlencode "data=...": push.tg reads the text from a form field called data, and URL-encoding keeps the message in one piece even if a tag contains characters like & or =.

Step 3: add it to a jail

In /etc/fail2ban/jail.local, keep the normal ban action and add pushtg on the next line, indented:

[sshd]
enabled = true
action = %(action_)s
         pushtg

%(action_)s is the stock "ban only" shortcut from jail.conf, so the firewall ban still happens; pushtg just runs alongside it. To use it for every jail, put the same action = lines in the [DEFAULT] section instead.

Then reload:

sudo fail2ban-client reload

Step 4: test it

Ban a documentation-range address by hand, check Telegram, then unban it:

sudo fail2ban-client set sshd banip 203.0.113.7
sudo fail2ban-client set sshd unbanip 203.0.113.7

If nothing arrives, run the curl part on the server by hand to rule out the network:

curl -d "test from $(hostname)" https://push.tg/k3x9q

Busy server?

An internet-facing SSH port can collect dozens of bans a day. If that's too chatty, add the action only to the jails you care about (a web login, a mail server), or point a quiet group at it. Each Telegram chat or group gets its own push.tg address, so ban noise can live in its own group and real outages in another.

Check that the message really arrived. push.tg answers HTTP 200 even when the key is wrong; the body then says {"error":"no such user"}. Send one test by hand and look at Telegram before you trust it:

Official references: jail.conf(5): action files and action tags, fail2ban-client(1): banip / unbanip / reload.

Open @trafficRobot · push.tg

More copy-paste guides

Report Page