#!/bin/bash # Sends text messages using telegram api # to alert server administrator of ip banning. # # Requires one argument, one of the following: # start # stop # ban # unban # # Optional second argument: IP for ban/unban #replace this with your own telegram contact to=Telegram_peer_replace_this # Display usage information function show_usage { echo "Usage: $0 action " echo "Where action is start, stop, ban, unban" echo "and ip is optional passed to ban, unban" exit } # Actually send telegram message # Expects the telegram content (body) to be passed # as argument. function send_telegram { msg="[`date -Iminutes`] - `hostname`: Notice: $1 " echo "$msg" >> /var/log/fail2ban-telegram.log (echo "contact_list";sleep 30;echo "msg $to $msg"; echo "safe_quit") | telegram-cli exit } # Check for script arguments if [ $# -lt 1 ] then show_usage fi # Take action depending on argument if [ "$1" = 'start' ] then message="Fail2ban just started." send_telegram "$message" elif [ "$1" = 'stop' ] then message="Fail2ban just stopped." send_telegram "$message" elif [ "$1" = 'ban' ] then message=$([ "$2" != '' ] && echo "Fail2ban just banned $2" || echo 'Fail2ban just banned an ip.' ) send_telegram "$message" elif [ "$1" = 'unban' ] then message=$([ "$2" != '' ] && echo "Fail2ban just unbanned $2" || echo "Fail2ban just unbanned an ip." ) send_telegram "$message" else show_usage fi