Created
March 5, 2016 13:16
-
-
Save EggieCode/87146da11f3a67e1a14e to your computer and use it in GitHub Desktop.
Block value servers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import re | |
import os | |
import sys | |
import subprocess | |
try: input = raw_input | |
except NameError: pass | |
bat_text = [] | |
rules = { | |
'aus': [ | |
'103.10.125.0-103.10.125.255' | |
], | |
'brazil': [ | |
'209.197.29.0-209.197.29.255', | |
'209.197.25.0-209.197.25.255', | |
'205.185.194.0-205.185.194.255' | |
], | |
'dubai': [ | |
'185.25.183.0-185.25.183.255' | |
], | |
'eue': [ | |
'146.66.155.0-146.66.155.255', | |
'185.25.182.0-185.25.182.255' | |
], | |
'euw': [ | |
'146.66.152.0-146.66.152.255', | |
'146.66.158.0-146.66.158.255', | |
'146.66.159.0-146.66.159.255' | |
], | |
'india': [ | |
'180.149.41.0-180.149.41.255', | |
'116.202.224.146' | |
], | |
'russia': [ | |
'146.66.156.0-146.66.156.255', | |
'146.66.157.0-146.66.157.255', | |
'185.25.180.0-185.25.180.255', | |
'185.25.181.0-185.25.181.255' | |
], | |
'safrica': [ | |
'152.111.192.0-152.111.192.255', | |
'197.80.200.0-197.80.200.255', | |
'196.38.180.0-196.38.180.255' | |
], | |
'sgp': [ | |
'103.28.54.0-103.28.54.255', | |
'103.28.55.0-103.28.55.255', | |
'103.10.124.0-103.10.124.255' | |
], | |
'use': [ | |
'208.78.164.0-208.78.164.255', | |
'208.78.165.0-208.78.165.255', | |
'208.78.166.0-208.78.166.255' | |
], | |
'usw': [ | |
'192.69.96.0-192.69.96.255', | |
'192.69.97.0-192.69.97.255' | |
] | |
} | |
def query_yes_no(question, default="yes"): | |
"""Ask a yes/no question via raw_input() and return their answer. | |
"question" is a string that is presented to the user. | |
"default" is the presumed answer if the user just hits <Enter>. | |
It must be "yes" (the default), "no" or None (meaning | |
an answer is required of the user). | |
The "answer" return value is True for "yes" or False for "no". | |
""" | |
valid = {"yes": True, "y": True, "ye": True, | |
"no": False, "n": False} | |
if default is None: | |
prompt = " [y/n] " | |
elif default == "yes": | |
prompt = " [Y/n] " | |
elif default == "no": | |
prompt = " [y/N] " | |
else: | |
raise ValueError("invalid default answer: '%s'" % default) | |
while True: | |
sys.stdout.write(question + prompt) | |
choice = input().lower() | |
if default is not None and choice == '': | |
return valid[default] | |
elif choice in valid: | |
return valid[choice] | |
else: | |
sys.stdout.write("Please respond with 'yes' or 'no' " | |
"(or 'y' or 'n').\n") | |
if not os.geteuid() == 0: | |
sys.exit('Script must be run as root') | |
def ban_rules(ips, name = 'VALUE SERVER'): | |
for ip in ips: | |
subprocess.call([ | |
'/sbin/iptables','-I', 'INPUT', | |
'-m', 'iprange', '--src-range', ip, | |
'-j', 'DROP', | |
'-m', 'comment','--comment' ,name]) | |
for (server,ips) in rules.items(): | |
if query_yes_no("Block %s value server?" % server, default = 'no'): | |
ban_rules(ips, "Valve server %s"%server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment