diff --git a/bin/fail2ban-client b/bin/fail2ban-client index bc0c0be8..dc2f7d84 100755 --- a/bin/fail2ban-client +++ b/bin/fail2ban-client @@ -176,7 +176,7 @@ class Fail2banClient: if showRet: self.__logSocketError() return False - except Exception, e: + except Exception as e: if showRet: logSys.error(e) return False @@ -429,7 +429,7 @@ class Fail2banClient: elif not cmd == "": try: self.__processCommand(shlex.split(cmd)) - except Exception, e: + except Exception as e: logSys.error(e) except (EOFError, KeyboardInterrupt): print @@ -451,7 +451,7 @@ class Fail2banClient: ret = self.__configurator.getOptions(jail) self.__configurator.convertToProtocol() self.__stream = self.__configurator.getConfigStream() - except Exception, e: + except Exception as e: logSys.error("Failed during configuration: %s" % e) ret = False return ret diff --git a/bin/fail2ban-server b/bin/fail2ban-server index f522f418..23eae136 100755 --- a/bin/fail2ban-server +++ b/bin/fail2ban-server @@ -127,7 +127,7 @@ class Fail2banServer: self.__conf["pidfile"], self.__conf["force"]) return True - except Exception, e: + except Exception as e: logSys.exception(e) self.__server.quit() return False diff --git a/fail2ban/client/configparserinc.py b/fail2ban/client/configparserinc.py index 7bbc7886..81f5bfb3 100644 --- a/fail2ban/client/configparserinc.py +++ b/fail2ban/client/configparserinc.py @@ -168,7 +168,7 @@ after = 1.conf parser, i = self._getSharedSCPWI(resource) if not i: return [] - except UnicodeDecodeError, e: + except UnicodeDecodeError as e: logSys.error("Error decoding config file '%s': %s" % (resource, e)) return [] diff --git a/fail2ban/client/configreader.py b/fail2ban/client/configreader.py index c6dd1b60..a48a0cc6 100644 --- a/fail2ban/client/configreader.py +++ b/fail2ban/client/configreader.py @@ -221,7 +221,7 @@ class ConfigReaderUnshared(SafeConfigParserWithIncludes): if not pOptions is None and option[1] in pOptions: continue values[option[1]] = v - except NoSectionError, e: + except NoSectionError as e: # No "Definition" section or wrong basedir logSys.error(e) values[option[1]] = option[2] diff --git a/fail2ban/client/fail2banregex.py b/fail2ban/client/fail2banregex.py index c2b2e894..71f50955 100755 --- a/fail2ban/client/fail2banregex.py +++ b/fail2ban/client/fail2banregex.py @@ -329,7 +329,7 @@ class Fail2banRegex(object): if ret is not None: found = True regex = self._ignoreregex[ret].inc() - except RegexException, e: + except RegexException as e: output( e ) return False return found @@ -346,7 +346,7 @@ class Fail2banRegex(object): regex = self._failregex[match[0]] regex.inc() regex.appendIP(match) - except RegexException, e: + except RegexException as e: output( e ) return False except IndexError: @@ -510,7 +510,7 @@ class Fail2banRegex(object): output( "Use log file : %s" % cmd_log ) output( "Use encoding : %s" % self.encoding ) test_lines = self.file_lines_gen(hdlr) - except IOError, e: + except IOError as e: output( e ) return False elif cmd_log == "systemd-journal": # pragma: no cover diff --git a/fail2ban/client/jailreader.py b/fail2ban/client/jailreader.py index c86c3153..01765148 100644 --- a/fail2ban/client/jailreader.py +++ b/fail2ban/client/jailreader.py @@ -171,7 +171,7 @@ class JailReader(ConfigReader): self.__actions.append(action) else: raise AttributeError("Unable to read action") - except Exception, e: + except Exception as e: logSys.error("Error in action definition " + act) logSys.debug("Caught exception: %s" % (e,)) return False diff --git a/fail2ban/server/database.py b/fail2ban/server/database.py index 9f562511..34a8b39a 100644 --- a/fail2ban/server/database.py +++ b/fail2ban/server/database.py @@ -42,7 +42,7 @@ if sys.version_info >= (3,): try: x = json.dumps(x, ensure_ascii=False).encode( locale.getpreferredencoding(), 'replace') - except Exception, e: # pragma: no cover + except Exception as e: # pragma: no cover logSys.error('json dumps failed: %s', e) x = '{}' return x @@ -51,7 +51,7 @@ if sys.version_info >= (3,): try: x = json.loads(x.decode( locale.getpreferredencoding(), 'replace')) - except Exception, e: # pragma: no cover + except Exception as e: # pragma: no cover logSys.error('json loads failed: %s', e) x = {} return x @@ -70,7 +70,7 @@ else: try: x = json.dumps(_normalize(x), ensure_ascii=False).decode( locale.getpreferredencoding(), 'replace') - except Exception, e: # pragma: no cover + except Exception as e: # pragma: no cover logSys.error('json dumps failed: %s', e) x = '{}' return x @@ -79,7 +79,7 @@ else: try: x = _normalize(json.loads(x.decode( locale.getpreferredencoding(), 'replace'))) - except Exception, e: # pragma: no cover + except Exception as e: # pragma: no cover logSys.error('json loads failed: %s', e) x = {} return x @@ -175,7 +175,7 @@ class Fail2BanDb(object): logSys.info( "Connected to fail2ban persistent database '%s'", filename) - except sqlite3.OperationalError, e: + except sqlite3.OperationalError as e: logSys.error( "Error connecting to fail2ban persistent database '%s': %s", filename, e.args[0]) diff --git a/fail2ban/server/filter.py b/fail2ban/server/filter.py index 45ae7704..98d0bce3 100644 --- a/fail2ban/server/filter.py +++ b/fail2ban/server/filter.py @@ -105,7 +105,7 @@ class Filter(JailThread): logSys.warning( "Mutliline regex set for jail '%s' " "but maxlines not greater than 1") - except RegexException, e: + except RegexException as e: logSys.error(e) raise e @@ -138,7 +138,7 @@ class Filter(JailThread): try: regex = Regex(value) self.__ignoreRegex.append(regex) - except RegexException, e: + except RegexException as e: logSys.error(e) raise e @@ -536,7 +536,7 @@ class Filter(JailThread): failRegex.getMatchedLines()]) if not checkAllRegex: break - except RegexException, e: # pragma: no cover - unsure if reachable + except RegexException as e: # pragma: no cover - unsure if reachable logSys.error(e) return failList @@ -660,15 +660,15 @@ class FileFilter(Filter): try: has_content = log.open() # see http://python.org/dev/peps/pep-3151/ - except IOError, e: + except IOError as e: logSys.error("Unable to open %s" % filename) logSys.exception(e) return False - except OSError, e: # pragma: no cover - requires race condition to tigger this + except OSError as e: # pragma: no cover - requires race condition to tigger this logSys.error("Error opening %s" % filename) logSys.exception(e) return False - except Exception, e: # pragma: no cover - Requires implemention error in FileContainer to generate + except Exception as e: # pragma: no cover - Requires implemention error in FileContainer to generate logSys.error("Internal errror in FileContainer open method - please report as a bug to https://github.com/fail2ban/fail2ban/issues") logSys.exception(e) return False @@ -858,11 +858,11 @@ class DNSUtils: # retrieve ip (todo: use AF_INET6 for IPv6) try: return set([i[4][0] for i in socket.getaddrinfo(dns, None, socket.AF_INET, 0, socket.IPPROTO_TCP)]) - except socket.error, e: + except socket.error as e: logSys.warning("Unable to find a corresponding IP address for %s: %s" % (dns, e)) return list() - except socket.error, e: + except socket.error as e: logSys.warning("Socket error raised trying to resolve hostname %s: %s" % (dns, e)) return list() @@ -871,7 +871,7 @@ class DNSUtils: def ipToName(ip): try: return socket.gethostbyaddr(ip)[0] - except socket.error, e: + except socket.error as e: logSys.debug("Unable to find a name for the IP %s: %s" % (ip, e)) return None diff --git a/fail2ban/server/filterpoll.py b/fail2ban/server/filterpoll.py index d0b37775..6d9f84b3 100644 --- a/fail2ban/server/filterpoll.py +++ b/fail2ban/server/filterpoll.py @@ -138,7 +138,7 @@ class FilterPoll(FileFilter): logSys.debug("%s has been modified", filename) self.__prevStats[filename] = stats return True - except OSError, e: + except OSError as e: logSys.error("Unable to get stat on %s because of: %s" % (filename, e)) self.__file404Cnt[filename] += 1 diff --git a/fail2ban/server/filterpyinotify.py b/fail2ban/server/filterpyinotify.py index 100ad233..d551e0f4 100644 --- a/fail2ban/server/filterpyinotify.py +++ b/fail2ban/server/filterpyinotify.py @@ -44,7 +44,7 @@ if not hasattr(pyinotify, '__version__') \ try: manager = pyinotify.WatchManager() del manager -except Exception, e: +except Exception as e: raise ImportError("Pyinotify is probably not functional on this system: %s" % str(e)) diff --git a/fail2ban/server/jail.py b/fail2ban/server/jail.py index a866cb51..d5a0384f 100644 --- a/fail2ban/server/jail.py +++ b/fail2ban/server/jail.py @@ -106,7 +106,7 @@ class Jail: logSys.info("Initiated %r backend" % b) self.__actions = Actions(self) return # we are done - except ImportError, e: + except ImportError as e: # Log debug if auto, but error if specific logSys.log( logging.DEBUG if backend == "auto" else logging.ERROR, diff --git a/fail2ban/server/server.py b/fail2ban/server/server.py index 3e371945..f3d44829 100644 --- a/fail2ban/server/server.py +++ b/fail2ban/server/server.py @@ -108,20 +108,20 @@ class Server: pidFile = open(pidfile, 'w') pidFile.write("%s\n" % os.getpid()) pidFile.close() - except IOError, e: + except IOError as e: logSys.error("Unable to create PID file: %s" % e) # Start the communication logSys.debug("Starting communication") try: self.__asyncServer.start(sock, force) - except AsyncServerException, e: + except AsyncServerException as e: logSys.error("Could not start server: %s", e) # Removes the PID file. try: logSys.debug("Remove PID file %s" % pidfile) os.remove(pidfile) - except OSError, e: + except OSError as e: logSys.error("Unable to remove PID file: %s" % e) logSys.info("Exiting Fail2ban") @@ -543,7 +543,7 @@ class Server: # the child gets a new PID, making it impossible for its PID to equal its # PGID. pid = os.fork() - except OSError, e: + except OSError as e: return((e.errno, e.strerror)) # ERROR (return a tuple) if pid == 0: # The first child. @@ -564,7 +564,7 @@ class Server: # fork guarantees that the child is no longer a session leader, thus # preventing the daemon from ever acquiring a controlling terminal. pid = os.fork() # Fork a second child. - except OSError, e: + except OSError as e: return((e.errno, e.strerror)) # ERROR (return a tuple) if (pid == 0): # The second child. diff --git a/fail2ban/server/transmitter.py b/fail2ban/server/transmitter.py index 0d9f0fe4..636b90da 100644 --- a/fail2ban/server/transmitter.py +++ b/fail2ban/server/transmitter.py @@ -56,7 +56,7 @@ class Transmitter: try: ret = self.__commandHandler(command) ack = 0, ret - except Exception, e: + except Exception as e: logSys.warning("Command %r has failed. Received %r" % (command, e)) ack = 1, e diff --git a/fail2ban/tests/clientreadertestcase.py b/fail2ban/tests/clientreadertestcase.py index ee362e3d..8b6564a3 100644 --- a/fail2ban/tests/clientreadertestcase.py +++ b/fail2ban/tests/clientreadertestcase.py @@ -410,7 +410,7 @@ class FilterReaderTest(unittest.TestCase): # from testcase01 filterReader.get('Definition', 'failregex') filterReader.get('Definition', 'ignoreregex') - except Exception, e: # pragma: no cover - failed if reachable + except Exception as e: # pragma: no cover - failed if reachable self.fail('unexpected options after readexplicit: %s' % (e)) diff --git a/fail2ban/tests/samplestestcase.py b/fail2ban/tests/samplestestcase.py index a40a11da..327410bc 100644 --- a/fail2ban/tests/samplestestcase.py +++ b/fail2ban/tests/samplestestcase.py @@ -94,7 +94,7 @@ def testSampleRegexsFactory(name, basedir): if jsonREMatch: try: faildata = json.loads(jsonREMatch.group(1)) - except ValueError, e: + except ValueError as e: raise ValueError("%s: %s:%i" % (e, logFile.filename(), logFile.filelineno())) line = next(logFile) diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index e091c935..ee641d41 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -184,13 +184,13 @@ def gatherTests(regexps=None, no_network=False): try: from ..server.filtergamin import FilterGamin filters.append(FilterGamin) - except Exception, e: # pragma: no cover + except Exception as e: # pragma: no cover logSys.warning("Skipping gamin backend testing. Got exception '%s'" % e) try: from ..server.filterpyinotify import FilterPyinotify filters.append(FilterPyinotify) - except Exception, e: # pragma: no cover + except Exception as e: # pragma: no cover logSys.warning("I: Skipping pyinotify backend testing. Got exception '%s'" % e) for Filter_ in filters: @@ -199,7 +199,7 @@ def gatherTests(regexps=None, no_network=False): try: # pragma: systemd no cover from ..server.filtersystemd import FilterSystemd tests.addTest(unittest.makeSuite(filtertestcase.get_monitor_failures_journal_testcase(FilterSystemd))) - except Exception, e: # pragma: no cover + except Exception as e: # pragma: no cover logSys.warning("I: Skipping systemd backend testing. Got exception '%s'" % e) # Server test for logging elements which break logging used to support