iptables GeoIP 적용을 위한 국가별 DB설치 스크립트

#!/bin/bash
######################################################################
#
# iptables의 GeoIP 적용을 위한 국가별 DB 준비 작업
#
######################################################################
SCRIPT_NAME=$(basename $0)
CURRENT_DIR=$(pwd)
XTABLES_ADDONS_VER=2.14

######################################################################
#
# Error Handling
#
######################################################################
function error_exit
{
  clear
  echo "${SCRIPT_NAME}: ${1:-\"Unknown Error\"}" 1>&2
  exit 1
}

######################################################################
#
# 실행에 필요한 패키지 설치
#
######################################################################
function yum_prepare() {
  yum -y install perl-NetAddr-IP perl-Net-CIDR-Lite perl-Text-CSV_XS iptables-devel kernel-devel
}

######################################################################
#
# xtables-addons 다운로드
#
######################################################################
function download_xtables_addons() {
  if [ -d xtables-addons-* ]
  then
    rm -rf xtables-addons-*
  fi
  if [ -f xtables-addons-${XTABLES_ADDONS_VER}.tar.xz ]
  then
    rm -f xtables-addons-${XTABLES_ADDONS_VER}.tar.xz
  fi
  wget https://sourceforge.net/projects/xtables-addons/files/Xtables-addons/xtables-addons-${XTABLES_ADDONS_VER}.tar.xz/download -O xtables-addons-${XTABLES_ADDONS_VER}.tar.xz
  if [ ! -f xtables-addons-${XTABLES_ADDONS_VER}.tar.xz ]
  then
    error_exit
  fi
  tar -xJf xtables-addons-*.tar.xz
  rm -f xtables-addons-*.tar.xz
}

######################################################################
#
# 신DB > 구DB로 변환하기 위한 GeoLite2xtables 다운로드
#
######################################################################
function download_geolite2xtables() {
  if [ -d GeoLite2xtables ]
  then
    rm -rf GeoLite2xtables
  fi
  git clone https://github.com/mschmitt/GeoLite2xtables.git
  if [ ! -d GeoLite2xtables ]
  then
    error_exit
  fi
}

######################################################################
#
# 신DB를 구DB로 변환하는 작업
#
######################################################################
function conv_geolite2xtables() {
  if [ -d /usr/share/xt_geoip ]
  then
    rm -rf /usr/share/xt_geoip
  fi
  rm -f /tmp/GeoLite2-Country-Blocks-*
  rm -f /tmp/CountryInfo.txt

  mkdir -p /usr/share/xt_geoip/{BE,LE}
  cd ${CURRENT_DIR}/GeoLite2xtables
  echo "***********************00_download_geolite2"
  ./00_download_geolite2
  echo "***********************10_download_countryinfo"
  ./10_download_countryinfo
  echo "***********************20_convert_geolite2"
  cat /tmp/GeoLite2-Country-Blocks-IPv{4,6}.csv | ./20_convert_geolite2 /tmp/CountryInfo.txt > /usr/share/xt_geoip/GeoIP-legacy.csv
  echo "***********************xt_geoip_build"
  cd ${CURRENT_DIR}/xtables-addons-${XTABLES_ADDONS_VER}/geoip
  ./xt_geoip_build -D /usr/share/xt_geoip /usr/share/xt_geoip/GeoIP-legacy.csv
  echo "***********************tar -cvzf xt_geoip.tar.gz /usr/share/xt_geoip"
  if [ -f /usr/share/xt_geoip/GeoIP-legacy.csv ]
  then
    rm -f /usr/share/xt_geoip/GeoIP-legacy.csv
  fi
  cd ${CURRENT_DIR}
  tar -cvzf xt_geoip.tar.gz /usr/share/xt_geoip
}

######################################################################
#
# 다운받은 파일들 삭제
#
######################################################################
function clean_data() {
  cd ${CURRENT_DIR}
  if [ -d xtables-addons-* ]
  then
    rm -rf xtables-addons-*
  fi
  if [ -d GeoLite2xtables ]
  then
    rm -rf GeoLite2xtables
  fi
  rm -f /tmp/GeoLite2-Country-Blocks-*
  rm -f /tmp/CountryInfo.txt
}

yum_prepare
download_xtables_addons
download_geolite2xtables
conv_geolite2xtables
clean_data
위로 스크롤