class Fail2banClient: def __init__(self): self.__stream = None self.__configurator = Configurator() self.__conf = dict() self.__conf["conf"] = "/etc/fail2ban" self.__conf["socket"] = None logSys.setLevel(logging.WARNING) stdout = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(levelname)-6s %(message)s') stdout.setFormatter(formatter) logSys.addHandler(stdout) self.__configurator.setBaseDir(self.__conf["conf"]) self.__configurator.readEarly() socket = self.__configurator.getEarlyOptions() if self.__conf["socket"] == None: self.__conf["socket"] = socket["socket"] logSys.info("Using socket file " + self.__conf["socket"]) def __processCmd(self, jail = "", listjails = False, showRet = True): cmd = [] if listjails is True: cmd.append(['status']) elif jail != "": cmd.append(['status', jail]) for c in cmd: retval = False try: logSys.debug("%s" % c) client = CSocket(self.__conf["socket"]) ret = client.send(c) if ret[0] == 0: if listjails is False: retval = ret[1][1][1][0][1] else: retval = [i.strip() for i in ret[1][1][1].split(",")] if showRet: logSys.debug("OK : " + `ret[1]`) logSys.debug("retval: %s" % retval) return retval else: logSys.debug("NOK: " + `ret[1].args`) logSys.debug(ret[1]) return except socket.error: if showRet: logSys.error("Unable to contact server. Is it running?") return except Exception, e: if showRet: logSys.error(e) return
def testConfigurator(self): configurator = Configurator() configurator.setBaseDir('config') self.assertEqual(configurator.getBaseDir(), 'config') configurator.readEarly() opts = configurator.getEarlyOptions() # our current default settings self.assertEqual(opts['socket'], '/var/run/fail2ban/fail2ban.sock') self.assertEqual(opts['pidfile'], '/var/run/fail2ban/fail2ban.pid') # and if we force change configurator's fail2ban's baseDir # there should be an error message (test visually ;) -- # otherwise just a code smoke test) configurator._Configurator__jails.setBaseDir('/tmp') self.assertEqual(configurator._Configurator__jails.getBaseDir(), '/tmp') self.assertEqual(configurator.getBaseDir(), 'config')
def __init__(self): self.__stream = None self.__configurator = Configurator() self.__conf = dict() self.__conf["conf"] = "/etc/fail2ban" self.__conf["socket"] = None logSys.setLevel(logging.WARNING) stdout = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(levelname)-6s %(message)s') stdout.setFormatter(formatter) logSys.addHandler(stdout) self.__configurator.setBaseDir(self.__conf["conf"]) self.__configurator.readEarly() socket = self.__configurator.getEarlyOptions() if self.__conf["socket"] == None: self.__conf["socket"] = socket["socket"] logSys.info("Using socket file " + self.__conf["socket"])
def testConfigurator(self): configurator = Configurator() configurator.setBaseDir('config') self.assertEqual(configurator.getBaseDir(), 'config') configurator.readEarly() opts = configurator.getEarlyOptions() # our current default settings self.assertEqual(opts['socket'], '/var/run/fail2ban/fail2ban.sock') self.assertEqual(opts['pidfile'], '/var/run/fail2ban/fail2ban.pid') configurator.getOptions() configurator.convertToProtocol() commands = configurator.getConfigStream() # and there is logging information left to be passed into the # server self.assertEqual(sorted(commands), [['set', 'loglevel', 3], ['set', 'logtarget', '/var/log/fail2ban.log']]) # and if we force change configurator's fail2ban's baseDir # there should be an error message (test visually ;) -- # otherwise just a code smoke test) configurator._Configurator__jails.setBaseDir('/tmp') self.assertEqual(configurator._Configurator__jails.getBaseDir(), '/tmp') self.assertEqual(configurator.getBaseDir(), 'config')