mirror of https://github.com/fail2ban/fail2ban
Added ability to set log file encoding with fail2ban-regex
parent
66367876bb
commit
578d9bed1b
|
@ -22,7 +22,7 @@ __author__ = "Cyril Jaquier, Yaroslav Halchenko"
|
||||||
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2012 Yaroslav Halchenko"
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2012 Yaroslav Halchenko"
|
||||||
__license__ = "GPL"
|
__license__ = "GPL"
|
||||||
|
|
||||||
import getopt, sys, time, logging, os
|
import getopt, sys, time, logging, os, locale
|
||||||
|
|
||||||
# Inserts our own modules path first in the list
|
# Inserts our own modules path first in the list
|
||||||
# fix for bug #343821
|
# fix for bug #343821
|
||||||
|
@ -77,6 +77,7 @@ class Fail2banRegex:
|
||||||
self.__ignoreregex = list()
|
self.__ignoreregex = list()
|
||||||
self.__failregex = list()
|
self.__failregex = list()
|
||||||
self.__verbose = False
|
self.__verbose = False
|
||||||
|
self.encoding = locale.getpreferredencoding()
|
||||||
# Setup logging
|
# Setup logging
|
||||||
logging.getLogger("fail2ban").handlers = []
|
logging.getLogger("fail2ban").handlers = []
|
||||||
self.__hdlr = logging.StreamHandler(Fail2banRegex.test)
|
self.__hdlr = logging.StreamHandler(Fail2banRegex.test)
|
||||||
|
@ -110,6 +111,7 @@ class Fail2banRegex:
|
||||||
print "This tools can test regular expressions for \"fail2ban\"."
|
print "This tools can test regular expressions for \"fail2ban\"."
|
||||||
print
|
print
|
||||||
print "Options:"
|
print "Options:"
|
||||||
|
print " -e, --encoding set the file encoding"
|
||||||
print " -h, --help display this help message"
|
print " -h, --help display this help message"
|
||||||
print " -V, --version print the version"
|
print " -V, --version print the version"
|
||||||
print " -v, --verbose verbose output"
|
print " -v, --verbose verbose output"
|
||||||
|
@ -141,6 +143,8 @@ class Fail2banRegex:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif opt[0] in ["-v", "--verbose"]:
|
elif opt[0] in ["-v", "--verbose"]:
|
||||||
self.__verbose = True
|
self.__verbose = True
|
||||||
|
elif opt[0] in ["-e", "--encoding"]:
|
||||||
|
self.encoding = opt[1]
|
||||||
|
|
||||||
#@staticmethod
|
#@staticmethod
|
||||||
def logIsFile(value):
|
def logIsFile(value):
|
||||||
|
@ -318,8 +322,8 @@ if __name__ == "__main__":
|
||||||
fail2banRegex = Fail2banRegex()
|
fail2banRegex = Fail2banRegex()
|
||||||
# Reads the command line options.
|
# Reads the command line options.
|
||||||
try:
|
try:
|
||||||
cmdOpts = 'hVcv'
|
cmdOpts = 'e:hVcv'
|
||||||
cmdLongOpts = ['help', 'version', 'verbose']
|
cmdLongOpts = ['encoding=', 'help', 'version', 'verbose']
|
||||||
optList, args = getopt.getopt(sys.argv[1:], cmdOpts, cmdLongOpts)
|
optList, args = getopt.getopt(sys.argv[1:], cmdOpts, cmdLongOpts)
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
fail2banRegex.dispUsage()
|
fail2banRegex.dispUsage()
|
||||||
|
@ -346,13 +350,15 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
if fail2banRegex.logIsFile(cmd_log):
|
if fail2banRegex.logIsFile(cmd_log):
|
||||||
try:
|
try:
|
||||||
if sys.version_info >= (3,):
|
hdlr = open(cmd_log, 'rb')
|
||||||
hdlr = open(cmd_log, encoding='utf-8', errors='ignore')
|
|
||||||
else:
|
|
||||||
hdlr = open(cmd_log)
|
|
||||||
print "Use log file : " + cmd_log
|
print "Use log file : " + cmd_log
|
||||||
print
|
print
|
||||||
for line in hdlr:
|
for line in hdlr:
|
||||||
|
try:
|
||||||
|
line = line.decode(fail2banRegex.encoding, 'strict')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
if sys.version_info >= (3,): # Python 3 must be decoded
|
||||||
|
line = line.decode(fail2banRegex.encoding, 'ignore')
|
||||||
fail2banRegex.testIgnoreRegex(line)
|
fail2banRegex.testIgnoreRegex(line)
|
||||||
fail2banRegex.testRegex(line)
|
fail2banRegex.testRegex(line)
|
||||||
except IOError, e:
|
except IOError, e:
|
||||||
|
|
Loading…
Reference in New Issue