@@ -18,29 +18,39 @@ A simple guide to setup fail2ban action for sending an alert message to discord
1818 - Create the file ` /usr/local/bin/fail2ban-discord.sh` :
1919
2020 ` ` ` bash
21- #! /bin/bash
22-
21+ #! /usr/ bin/bash
22+
2323 JAIL=" $1 "
2424 IP=" $2 "
2525 MATCHES=" $3 "
2626
27- WEBHOOK_URL=" YOUR-WEBHOOK-URL"
27+ WEBHOOK_URL=" https://discord.com/api/webhooks/1304853928789278730/e3AGSi6ZqUWmX7bWUa8xmvHqes5zAn_ZQQEHBAadqTCugVnfhAzwbuNj6TZoF-vkUaRL"
28+
29+ JSON=" $( curl -s https://api.iplocation.net/\? ip\= $IP ) "
30+ COUNTRY=$( echo " $JSON " | jq -r ' .country_name' )
31+ CC=$( echo " $JSON " | jq -r ' .country_code2' | tr ' A-Z' ' a-z' )
32+
33+ # echo $JSON
34+ # echo $COUNTRY
35+ # echo $CC
2836
2937 HOSTNAME=$( hostname)
3038 TIMESTAMP=$( date " +%Y-%m-%d %H:%M:%S" )
3139
32- MESSAGE=" ** Fail2Ban Alert**
33- **Server:** ${HOSTNAME}
34- **Time:** ${TIMESTAMP}
35- **Jail:** \` ${JAIL} \`
36- **Banned IP:** \` ${IP} \`
37- **Reason:** ${MATCHES} "
40+ MESSAGE=" **🚨 Fail2Ban Alert**
41+ **Server:** $HOSTNAME
42+ **Time:** $TIMESTAMP
43+ **Jail:** \` $JAIL \`
44+ **Banned IP:** \` $IP \`
45+ **Attacked From:** $COUNTRY :flag_$CC :
46+ **Reason:** $MATCHES "
47+
3848
39- # Send to Discord
4049 curl -s -H " Content-Type: application/json" \
4150 -X POST \
4251 -d " $( jq -nc --arg content " $MESSAGE " ' {content: $content}' ) " \
4352 " $WEBHOOK_URL "
53+
4454 ` ` `
4555
4656 - Make it executable:
@@ -87,18 +97,19 @@ A simple guide to setup fail2ban action for sending an alert message to discord
8797 To test, you can intentionally trigger a failed login from a different IP or use:
8898
8999 ` ` ` bash
90- sudo fail2ban-client set sshd banip 192.168.0.1
100+ sudo fail2ban-client set sshd banip 222.65.14.143
91101 ` ` `
92102
93- All are done! . You should receive an alert in your Discord channel like below.
94-
95- ` ` ` text
96- Fail2Ban Alert
97- Server: srv627828
98- Time: 2025-05-29 00:06:40
99- Jail: sshd
100- Banned IP: 191.7.190.74
101- Reason: May 28 23:53:53 srv627828 sshd[52304]: Invalid user katarina from 191.7.190.74 port 44770
102- May 28 23:53:53 srv627828 sshd[52304]: Failed password for invalid user katarina from 191.7.190.74 port 44770 ssh2
103- May 29 00:06:39 srv627828 sshd[52915]: Failed password for postfix from 191.7.190.74 port 30736 ssh2
104- ` ` `
103+ All are done! . You should receive an alert in your Discord channel like below.
104+
105+ ` ` ` text
106+ 🚨 Fail2Ban Alert
107+ Server: srv627828
108+ Time: 2025-06-30 22:36:19
109+ Jail: sshd
110+ Banned IP: 222.65.14.143
111+ Attacked From: China :flag_cn:
112+ Reason: Jun 30 22:36:14 srv627828 sshd[2154564]: Invalid user user from 222.65.14.143 port 17607
113+ Jun 30 22:36:16 srv627828 sshd[2154566]: Invalid user user from 222.65.14.143 port 17608
114+ Jun 30 22:36:17 srv627828 sshd[2154568]: Invalid user user from 222.65.14.143 port 17609
115+ ` ` `
0 commit comments