Commit a946d15f authored by Ruzal Gimazov's avatar Ruzal Gimazov

Added configparser, renamed configfile to settings.ini, added new message to chanal 'by'

parent f321c3cb
...@@ -33,7 +33,7 @@ def get_ip_redis_server (path_to_config, path_to_log): ...@@ -33,7 +33,7 @@ def get_ip_redis_server (path_to_config, path_to_log):
# Читаем некоторые значения из конфиг. файла. # Читаем некоторые значения из конфиг. файла.
redis_server = config.get("Settings", "redis_server", fallback = "No such things as redis_server") redis_server = config.get("Settings", "redis_server", fallback = "No such things as redis_server")
if redis_server == "No such things as monsters": if redis_server == "No such things as redis_server":
config.set("Settings", "redis_server", "10.20.30.101") config.set("Settings", "redis_server", "10.20.30.101")
with open(path_to_config, "w") as config_file: with open(path_to_config, "w") as config_file:
config_file.write(config) config_file.write(config)
...@@ -48,12 +48,10 @@ def get_ip_redis_server (path_to_config, path_to_log): ...@@ -48,12 +48,10 @@ def get_ip_redis_server (path_to_config, path_to_log):
try: try:
path_to_log = '/var/log/eterban/eterban.log' path_to_log = '/var/log/eterban/eterban.log'
log = open (path_to_log, 'a') log = open (path_to_log, 'a')
log.close()
except: except:
try: try:
path_to_log = '/var/log/eterban.log' path_to_log = '/var/log/eterban.log'
log = open (path_to_log,'a') log = open (path_to_log,'a')
log.close()
except: except:
print ("Unknown error with logfile") print ("Unknown error with logfile")
sys.exit() sys.exit()
...@@ -74,8 +72,8 @@ p = r.pubsub() ...@@ -74,8 +72,8 @@ p = r.pubsub()
p.subscribe('ban', 'unban', 'by') p.subscribe('ban', 'unban', 'by')
for message in p.listen(): for message in p.listen():
if message is not None and message['type']=='message' and message['channel'] == b'ban': if message is not None and message['type']=='message' and message['channel'] == b'ban':
#print (message) ip = message['data'].decode('utf-8')
ip = message['data'].decode('utf-8') ip = message['data'].decode('utf-8')
#ban = 'ipset -A blacklist ' + ip #ban = 'ipset -A blacklist ' + ip
ban = 'fail2ban-client set blacklist banip ' + ip ban = 'fail2ban-client set blacklist banip ' + ip
...@@ -94,19 +92,18 @@ for message in p.listen(): ...@@ -94,19 +92,18 @@ for message in p.listen():
tcp_drop = 'conntrack -D -s ' + ip tcp_drop = 'conntrack -D -s ' + ip
subprocess.Popen(tcp_drop, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell = True) subprocess.Popen(tcp_drop, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell = True)
#subprocess.Popen(tcp_drop, shell = True) #subprocess.Popen(tcp_drop, shell = True)
elif message is not None and message['type'] =='message' and message['channel'] == b'by': elif message is not None and message['type'] =='message' and message['channel'] == b'by':
info = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()) info = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime())
info += " " + message['data'].decode('utf-8') + "\n" info += " " + message['data'].decode('utf-8') + "\n"
#print (info) #print (info)
log.write (info) log.write(info)
log.flush() log.flush()
elif message is not None: elif message is not None:
#print ("AHTUNG!!1!", message) #print ("AHTUNG!!1!", message)
info = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()) info = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime())
info += " Unknown message: " + str(message) + "\n" info += " Unknown message: " + str(message) + "\n"
#print (info) #print (info)
log.write (info) log.write(info)
log.flush() log.flush()
else: else:
pass pass
#!/usr/bin/python3 #!/usr/bin/python3
import sys, redis import redis
import sys
import configparser
import os
f = open ('/etc/eterban/eterban.conf','r') def createConfig(path_to_config):
line = f.readline() """
f.close() Create a config file
if line[:10] == "host_redis": """
i = 10 config = configparser.ConfigParser()
while (line[i] == ' '): config.add_section("Settings")
i+=1 config.set("Settings", "redis_server", "10.20.30.101")
i+=1 config.set("Settings", "hostname", "UNKNOWN")
while (line[i] == ' '):
i+=1 with open(path_to_config, "w") as config_file:
config.write(config_file)
sys.exit()
if line[-1] == '\n': def get_ip_redis_server (path_to_config):
host_redis = line[i:-1] if not os.path.exists(path_to_config):
createConfig (path_to_config)
config = configparser.ConfigParser()
config.read(path_to_config)
# Читаем некоторые значения из конфиг. файла.
redis_server = config.get("Settings", "redis_server", fallback = "No such things as redis_server")
hostname = config.get("Settings", "hostname", fallback = "No such things as hostname")
if redis_server == "No such things as redis_server":
config.set("Settings", "redis_server", "10.20.30.101")
with open(path_to_config, "a") as config_file:
config_file.write(config)
sys.exit()
else: else:
host_redis = line[i:] return (redis_server, hostname)
r = redis.Redis (host=host_redis)
r.publish ('ban', sys.argv[1]) path_to_config = '/etc/eterban/settings.ini'
\ No newline at end of file redis_server, hostname = get_ip_redis_server (path_to_config)
r = redis.Redis (host=redis_server)
r.publish ('ban', sys.argv[1])
message = sys.argv[1] + " was blocked by " + hostname
r.publish ('by', message)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment