From 7a28861fc709d488c59a28ecf58e4ef5e5b79f4d Mon Sep 17 00:00:00 2001 From: sebres Date: Fri, 7 Feb 2020 13:52:45 +0100 Subject: [PATCH] review of command line: more long-named options can be supplied via command line --- fail2ban/client/fail2bancmdline.py | 33 ++++++++++++++++-------------- man/fail2ban-client.1 | 6 +++--- man/fail2ban-server.1 | 6 +++--- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/fail2ban/client/fail2bancmdline.py b/fail2ban/client/fail2bancmdline.py index 3c6bd0bf..53c86de6 100644 --- a/fail2ban/client/fail2bancmdline.py +++ b/fail2ban/client/fail2bancmdline.py @@ -35,7 +35,8 @@ logSys = getLogger("fail2ban") def output(s): # pragma: no cover print(s) -CONFIG_PARAMS = ("socket", "pidfile", "logtarget", "loglevel", "syslogsocket",) +# Config parameters required to start fail2ban which can be also set via command line (overwrite fail2ban.conf), +CONFIG_PARAMS = ("socket", "pidfile", "logtarget", "loglevel", "syslogsocket") # Used to signal - we are in test cases (ex: prevents change logging params, log capturing, etc) PRODUCTION = True @@ -94,9 +95,9 @@ class Fail2banCmdLine(): output("and bans the corresponding IP addresses using firewall rules.") output("") output("Options:") - output(" -c configuration directory") - output(" -s socket path") - output(" -p pidfile path") + output(" -c, --conf configuration directory") + output(" -s, --socket socket path") + output(" -p, --pidfile pidfile path") output(" --pname name of the process (main thread) to identify instance (default fail2ban-server)") output(" --loglevel logging level") output(" --logtarget logging target, use file-name or stdout, stderr, syslog or sysout.") @@ -130,17 +131,15 @@ class Fail2banCmdLine(): """ for opt in optList: o = opt[0] - if o == "-c": + if o in ("-c", "--conf"): self._conf["conf"] = opt[1] - elif o == "-s": + elif o in ("-s", "--socket"): self._conf["socket"] = opt[1] - elif o == "-p": + elif o in ("-p", "--pidfile"): self._conf["pidfile"] = opt[1] - elif o.startswith("--log") or o.startswith("--sys"): - self._conf[ o[2:] ] = opt[1] - elif o in ["-d", "--dp", "--dump-pretty"]: + elif o in ("-d", "--dp", "--dump-pretty"): self._conf["dump"] = True if o == "-d" else 2 - elif o == "-t" or o == "--test": + elif o in ("-t", "--test"): self.cleanConfOnly = True self._conf["test"] = True elif o == "-v": @@ -164,12 +163,14 @@ class Fail2banCmdLine(): from ..server.mytime import MyTime output(MyTime.str2seconds(opt[1])) return True - elif o in ["-h", "--help"]: + elif o in ("-h", "--help"): self.dispUsage() return True - elif o in ["-V", "--version"]: + elif o in ("-V", "--version"): self.dispVersion(o == "-V") return True + elif o.startswith("--"): # other long named params (see also resetConf) + self._conf[ o[2:] ] = opt[1] return None def initCmdLine(self, argv): @@ -186,7 +187,8 @@ class Fail2banCmdLine(): try: cmdOpts = 'hc:s:p:xfbdtviqV' cmdLongOpts = ['loglevel=', 'logtarget=', 'syslogsocket=', 'test', 'async', - 'pname=', 'timeout=', 'str2sec=', 'help', 'version', 'dp', '--dump-pretty'] + 'conf=', 'pidfile=', 'pname=', 'socket=', + 'timeout=', 'str2sec=', 'help', 'version', 'dp', '--dump-pretty'] optList, self._args = getopt.getopt(self._argv[1:], cmdOpts, cmdLongOpts) except getopt.GetoptError: self.dispUsage() @@ -228,7 +230,8 @@ class Fail2banCmdLine(): if not conf: self.configurator.readEarly() conf = self.configurator.getEarlyOptions() - self._conf[o] = conf[o] + if o in conf: + self._conf[o] = conf[o] logSys.info("Using socket file %s", self._conf["socket"]) diff --git a/man/fail2ban-client.1 b/man/fail2ban-client.1 index 32a90851..ad4fa0ed 100644 --- a/man/fail2ban-client.1 +++ b/man/fail2ban-client.1 @@ -10,13 +10,13 @@ Fail2Ban v0.10.5 reads log file that contains password failure report and bans the corresponding IP addresses using firewall rules. .SH OPTIONS .TP -\fB\-c\fR +\fB\-c\fR, \fB\-\-conf\fR configuration directory .TP -\fB\-s\fR +\fB\-s\fR, \fB\-\-socket\fR socket path .TP -\fB\-p\fR +\fB\-p\fR, \fB\-\-pidfile\fR pidfile path .TP \fB\-\-pname\fR diff --git a/man/fail2ban-server.1 b/man/fail2ban-server.1 index d75158a8..c7516cc8 100644 --- a/man/fail2ban-server.1 +++ b/man/fail2ban-server.1 @@ -10,13 +10,13 @@ Fail2Ban v0.10.5 reads log file that contains password failure report and bans the corresponding IP addresses using firewall rules. .SH OPTIONS .TP -\fB\-c\fR +\fB\-c\fR, \fB\-\-conf\fR configuration directory .TP -\fB\-s\fR +\fB\-s\fR, \fB\-\-socket\fR socket path .TP -\fB\-p\fR +\fB\-p\fR, \fB\-\-pidfile\fR pidfile path .TP \fB\-\-pname\fR