fail2ban.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. Install_fail2ban() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. src_url=http://mirrors.linuxeye.com/oneinstack/src/fail2ban-${fail2ban_ver}.tar.gz && Download_src
  13. tar xzf fail2ban-${fail2ban_ver}.tar.gz
  14. pushd fail2ban-${fail2ban_ver} > /dev/null
  15. sed -i 's@for i in xrange(50)@for i in range(50)@' fail2ban/__init__.py
  16. ${python_install_dir}/bin/python setup.py install
  17. if [ -e /bin/systemctl ]; then
  18. /bin/cp build/fail2ban.service /lib/systemd/system/
  19. systemctl enable fail2ban
  20. else
  21. if [ "${PM}" == 'yum' ]; then
  22. /bin/cp files/redhat-initd /etc/init.d/fail2ban
  23. sed -i "s@^FAIL2BAN=.*@FAIL2BAN=${python_install_dir}/bin/fail2ban-client@" /etc/init.d/fail2ban
  24. sed -i 's@Starting fail2ban.*@&\n [ ! -e "/var/run/fail2ban" ] \&\& mkdir /var/run/fail2ban@' /etc/init.d/fail2ban
  25. chmod +x /etc/init.d/fail2ban
  26. chkconfig --add fail2ban
  27. chkconfig fail2ban on
  28. elif [ "${PM}" == 'apt-get' ]; then
  29. /bin/cp files/debian-initd /etc/init.d/fail2ban
  30. sed -i 's@2 3 4 5@3 4 5@' /etc/init.d/fail2ban
  31. sed -i "s@^DAEMON=.*@DAEMON=${python_install_dir}/bin/\$NAME-client@" /etc/init.d/fail2ban
  32. chmod +x /etc/init.d/fail2ban
  33. update-rc.d fail2ban defaults
  34. fi
  35. fi
  36. [ -z "`grep ^Port /etc/ssh/sshd_config`" ] && now_ssh_port=22 || now_ssh_port=`grep ^Port /etc/ssh/sshd_config | awk '{print $2}' | head -1`
  37. [ "${PM}" == 'yum' ] && LOGPATH=/var/log/secure
  38. [ "${PM}" == 'apt-get' ] && LOGPATH=/var/log/auth.log
  39. cat > /etc/fail2ban/jail.local << EOF
  40. [DEFAULT]
  41. ignoreip = 127.0.0.1/8
  42. bantime = 86400
  43. findtime = 600
  44. maxretry = 5
  45. [ssh-iptables]
  46. enabled = true
  47. filter = sshd
  48. action = iptables[name=SSH, port=${now_ssh_port}, protocol=tcp]
  49. logpath = ${LOGPATH}
  50. EOF
  51. cat > /etc/logrotate.d/fail2ban << EOF
  52. /var/log/fail2ban.log {
  53. missingok
  54. notifempty
  55. postrotate
  56. ${python_install_dir}/bin/fail2ban-client flushlogs >/dev/null || true
  57. endscript
  58. }
  59. EOF
  60. sed -i 's@^iptables = iptables.*@iptables = iptables@' /etc/fail2ban/action.d/iptables-common.conf
  61. kill -9 `ps -ef | grep fail2ban | grep -v grep | awk '{print $2}'` > /dev/null 2>&1
  62. service fail2ban start
  63. popd > /dev/null
  64. if [ -e "${python_install_dir}/bin/fail2ban-server" ]; then
  65. echo; echo "${CSUCCESS}fail2ban installed successfully! ${CEND}"
  66. else
  67. echo; echo "${CFAILURE}fail2ban install failed, Please try again! ${CEND}"
  68. fi
  69. popd > /dev/null
  70. }
  71. Uninstall_fail2ban() {
  72. service fail2ban stop
  73. ${python_install_dir}/bin/pip uninstall -y fail2ban > /dev/null 2>&1
  74. rm -rf /etc/init.d/fail2ban /etc/fail2ban /etc/logrotate.d/fail2ban /var/log/fail2ban.* /var/run/fail2ban
  75. echo; echo "${CMSG}fail2ban uninstall completed${CEND}";
  76. }