Commit 0beb4a7d authored by Vitaly Lipatov's avatar Vitaly Lipatov

ban.py: drop config creating

parent d7af677b
#!/usr/bin/python3 #!/usr/bin/python3
import redis import redis
import sys import sys
import configparser import configparser
import os import os
import socket import socket
def createConfig(path_to_config): def get_settings (path_to_config):
"""
Create a config file
"""
config = configparser.ConfigParser()
config.add_section("Settings")
config.set("Settings", "redis_server", "10.20.30.101")
config.set("Settings", "hostname", socket.gethostname())
with open(path_to_config, "w") as config_file:
config.write(config_file)
sys.exit()
def get_ip_redis_server (path_to_config):
if not os.path.exists(path_to_config): if not os.path.exists(path_to_config):
createConfig (path_to_config) print("Missed config file")
sys.exit()
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(path_to_config) config.read(path_to_config)
# Читаем некоторые значения из конфиг. файла. # Читаем некоторые значения из конфиг. файла.
redis_server = config.get("Settings", "redis_server", fallback = "localhost")
redis_server = config.get("Settings", "redis_server", fallback = "No such things as redis_server")
hostname = config.get("Settings", "hostname", fallback = socket.gethostname()) hostname = config.get("Settings", "hostname", fallback = socket.gethostname())
if redis_server == "No such things as redis_server": return (redis_server, hostname)
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:
return (redis_server, hostname)
path_to_config = '/etc/eterban/settings.ini' path_to_config = '/etc/eterban/settings.ini'
redis_server, hostname = get_ip_redis_server (path_to_config) redis_server, hostname = get_settings (path_to_config)
r = redis.Redis (host=redis_server) r = redis.Redis (host=redis_server)
r.publish ('ban', sys.argv[1]) r.publish ('ban', sys.argv[1])
...@@ -47,4 +29,4 @@ try: ...@@ -47,4 +29,4 @@ try:
except: except:
message = sys.argv[1] + " was blocked by " + hostname + " (set block: [name=NAME_OF_RULE] on " + hostname + ":/etc/fail2ban/jail.conf)" message = sys.argv[1] + " was blocked by " + hostname + " (set block: [name=NAME_OF_RULE] on " + hostname + ":/etc/fail2ban/jail.conf)"
r.publish ('by', message) 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