From 93ec9e01d402d9fcbb83f4548de1eab0bf581f13 Mon Sep 17 00:00:00 2001 From: sebres Date: Fri, 17 Mar 2017 10:03:34 +0100 Subject: [PATCH] fixes a small blemish by output in beautifier; command "unban" returns a count of tickets that were flushed --- fail2ban/client/beautifier.py | 7 ++++++- fail2ban/server/transmitter.py | 8 ++++---- fail2ban/tests/fail2banclienttestcase.py | 8 ++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/fail2ban/client/beautifier.py b/fail2ban/client/beautifier.py index df44afbb..4d9e549f 100644 --- a/fail2ban/client/beautifier.py +++ b/fail2ban/client/beautifier.py @@ -89,6 +89,8 @@ class Beautifier: val = " ".join(map(str, res1[1])) if isinstance(res1[1], list) else res1[1] msg.append("%s %s:\t%s" % (prefix1, res1[0], val)) msg = "\n".join(msg) + elif len(inC) < 2: + pass # to few cmd args for below elif inC[1] == "syslogsocket": msg = "Current syslog socket is:\n" msg += "`- " + response @@ -110,6 +112,8 @@ class Beautifier: else: msg = "Current database purge age is:\n" msg += "`- %iseconds" % response + elif len(inC) < 3: + pass # to few cmd args for below elif inC[2] in ("logpath", "addlogpath", "dellogpath"): if len(response) == 0: msg = "No file is currently monitored" @@ -178,7 +182,8 @@ class Beautifier: msg += ", ".join(response) except Exception: logSys.warning("Beautifier error. Please report the error") - logSys.error("Beautify %r with %r failed", response, self.__inputCmd) + logSys.error("Beautify %r with %r failed", response, self.__inputCmd, + exc_info=logSys.getEffectiveLevel()<=logging.DEBUG) msg = repr(msg) + repr(response) return msg diff --git a/fail2ban/server/transmitter.py b/fail2ban/server/transmitter.py index 265b9704..ad21b851 100644 --- a/fail2ban/server/transmitter.py +++ b/fail2ban/server/transmitter.py @@ -108,11 +108,11 @@ class Transmitter: value = command[1:] # if all ips: if len(value) == 1 and value[0] == "--all": - self.__server.setUnbanIP() - return + return self.__server.setUnbanIP() + cnt = 0 for value in value: - self.__server.setUnbanIP(None, value) - return None + cnt += self.__server.setUnbanIP(None, value) + return cnt elif command[0] == "echo": return command[1:] elif command[0] == "sleep": diff --git a/fail2ban/tests/fail2banclienttestcase.py b/fail2ban/tests/fail2banclienttestcase.py index adaf4719..683368ee 100644 --- a/fail2ban/tests/fail2banclienttestcase.py +++ b/fail2ban/tests/fail2banclienttestcase.py @@ -1075,6 +1075,14 @@ class Fail2banServerTest(Fail2banClientServerBase): "[test-jail1] Ban 192.0.2.4", all=True ) + # unban all (just to test command, already empty - nothing to unban): + self.pruneLog("[test-phase 7b]") + self.execSuccess(startparams, + "--async", "unban", "--all") + self.assertLogged( + "Flush ban list", + "Unbanned 0, 0 ticket(s) in 'test-jail1'", all=True) + # backend-switch (restart instead of reload): self.pruneLog("[test-phase 8a]") _write_jail_cfg(enabled=[1], backend="xxx-unknown-backend-zzz")