tips:fail2ban

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tips:fail2ban [2019/04/15 08:20] – [telegram action] scipiotips:fail2ban [2019/04/15 09:04] (current) – [telegram action] scipio
Line 22: Line 22:
 fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/giano-login.conf --print-all-matched fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/giano-login.conf --print-all-matched
 </code> </code>
 +
 +===== action =====
 +
 +<file ini action.d/telegram.conf>
 +[Definition]
 +actionstart = /usr/local/bin/telegram-send -g --format markdown "`uname -n`: [Fail2Ban] jail <name> è stata avviata"
 +actionstop = /usr/local/bin/telegram-send -g --format markdown "`uname -n`: [Fail2Ban] jail <name> è stata fermata"
 +actioncheck =
 +actionban = /usr/local/bin/telegram-send -g --format markdown "`uname -n`: [Fail2Ban] IP <ip> è stato bannato dopo <failures> tentativi falliti dalla jail <name>"
 +actionunban = 
 +
 +[Init]
 +init = 'Fail2Ban Telegram plugins activated"
 +</file>
  
 ===== jail ===== ===== jail =====
Line 34: Line 48:
 bantime = 6000 bantime = 6000
 maxretry = 3 maxretry = 3
 +action = %(action_)s
 +         telegram[name=GIANO]
 </file> </file>
  
Line 63: Line 79:
 ===== telegram action ===== ===== telegram action =====
  
-<file bash /etc/fail2ban/scripts/telegram.sh+<code
-#!/bin/bash                                                                                                                                    +pip install telegram-send 
-                                                                                                                                               +</code>
-# 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 +
-}+
  
 +create configuration file with token and chat id
 +<file ini /etc/telegram-send.conf>
 +[telegram]
 +chat_id = 
 +token = 
 +</file>
  
-# Send notification +test (-g option to use /etc/telegram-send.con) 
-function send_msg { +<code> 
-  apiToken= +telegram-send -g "hello, world" 
-  chatId= +</code>
-  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 
- 
-</file> 
  
  
  • tips/fail2ban.1555309214.txt.gz
  • Last modified: 2019/04/15 08:20
  • by scipio