Advertisement
cooperlees

Untitled

Jun 4th, 2017
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.57 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. BASE=/etc/named/geodns
  4. VERBOSE=0
  5.  
  6. GeoURL="http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip"
  7. GeoDB="GeoIPCountryCSV.zip"
  8.  
  9. errorCheck() {
  10.         if [ $? -ne 0 ]; then
  11.                 echo "ERROR: $@"
  12.                 exit 69
  13.         fi
  14. }
  15.  
  16. if [ ! -d $BASE ]; then
  17.         echo "!-> $BASE does not exist. Make it so!"
  18.         exit 1
  19. else
  20.         cd $BASE
  21. fi
  22.  
  23. [ -f $GeoDB ] || wget -T 5 -t 1 $GeoURL > /tmp/geodns 2>&1
  24. errorCheck "Problem with file / wget"
  25.  
  26. if [ $VERBOSE != 0 ]; then
  27.         echo -n "Creating CBE (Country,Begin,End) CSV file..."
  28. fi
  29.  
  30. unzip -p $GeoDB GeoIPCountryWhois.csv | awk -F \" '{print $10","$6","$8}' > cbe.csv
  31. errorCheck "Unable to unzip and awk out the csv"
  32.  
  33. if [ $VERBOSE != 0 ]; then
  34.         echo -ne "DONE\nGenerating BIND GeoIP.acl file..."
  35. fi
  36.  
  37. (for c in $(awk -F , '{print $1}' cbe.csv | sort -u)
  38. do
  39.   echo "acl \"$c\" {"
  40.   grep "^$c," cbe.csv | awk -F , 'function s(b,e,l,m,n) {l = int(log(e-b+1)/log(2)); m = 2^32-2^l; n = and(m,e); if (n == and(m,b)) printf "\t%u.%u.%u.%u/%u;\n",b/2^24%256,b/2^16%256,b/2^8%256,b%256,32-l; else {s(b,n-1); s(n,e)}} s($2,$3)'
  41.   echo -e "};\n"
  42. done) > GeoIP.acl
  43.  
  44. rm -f cbe.csv
  45.  
  46. if [ $VERBOSE != 0 ]; then
  47.         echo "DONE"
  48. fi
  49.  
  50. # RM .zip ready for next months run :)
  51. rm -r $GeoDB
  52. errorCheck "Unable to rm $GeoDB"
  53.  
  54. # Restart Named - If conf is good
  55. named-checkconf
  56. if [ $? -eq 0 ]; then
  57.         /etc/init.d/named restart > /dev/null 2>&1
  58.         errorCheck "Unable to restart named"
  59. else
  60.         echo "!-> ERROR with named configuration on $HOSTNAME"
  61. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement