-
Notifications
You must be signed in to change notification settings - Fork 25
/
fail2ban-telegram.sh
60 lines (49 loc) · 1.16 KB
/
fail2ban-telegram.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# 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=<put your api key here>
chatId=<put your chat id here>
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