#!/bin/bash
# Filename: fail2ban_install.sh

if [[ $(id -u) != "0" ]]; then
    echo "Please use root role run this script"
    exit 1
fi

yum -y install python iptables rsyslog
service rsyslog restart

cd /usr/local/src
if [[ ! -e fail2ban-0.9.1.tar.gz ]]; then
    wget -c https://down.vqiu.cn/package/tarball/fail2ban/fail2ban-0.9.1.tar.gz
fi
tar zxvf fail2ban-0.9.1.tar.gz
cd fail2ban-0.9.1
python setup.py install

echo "Copy configure file..."
\cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sed -i 's/# \[sshd\]/\[sshd\]/g' /etc/fail2ban/jail.local
sed -i 's/# enabled = true/enabled = true/g' /etc/fail2ban/jail.local

echo "Copy init files..."
mkdir /var/run/fail2ban
cd /etc/init.d/
wget -c https://down.vqiu.cn/package/tarball/fail2ban/init.d.fail2ban -O fail2ban
sed -i 's#%(sshd_log)s#/var/log/secure#g' /etc/fail2ban/jail.local
chmod +x /etc/init.d/fail2ban

echo "Start fail2ban..."
/etc/init.d/fail2ban start
