mirror of https://github.com/fail2ban/fail2ban
ENH: deleted trailing spaces in fail2ban- cmdline tools
Now it was already a mix, and Cyril is not working on this code any longer so no need to maintain this convention.pull/163/merge
parent
7cf509378c
commit
ffbbb9f8a3
|
@ -63,7 +63,7 @@ class Fail2banClient:
|
||||||
self.__conf["interactive"] = False
|
self.__conf["interactive"] = False
|
||||||
self.__conf["socket"] = None
|
self.__conf["socket"] = None
|
||||||
self.__conf["pidfile"] = None
|
self.__conf["pidfile"] = None
|
||||||
|
|
||||||
def dispVersion(self):
|
def dispVersion(self):
|
||||||
print "Fail2Ban v" + version
|
print "Fail2Ban v" + version
|
||||||
print
|
print
|
||||||
|
@ -73,7 +73,7 @@ class Fail2banClient:
|
||||||
print
|
print
|
||||||
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
||||||
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
||||||
|
|
||||||
def dispUsage(self):
|
def dispUsage(self):
|
||||||
""" Prints Fail2Ban command line options and exits
|
""" Prints Fail2Ban command line options and exits
|
||||||
"""
|
"""
|
||||||
|
@ -95,17 +95,17 @@ class Fail2banClient:
|
||||||
print " -V, --version print the version"
|
print " -V, --version print the version"
|
||||||
print
|
print
|
||||||
print "Command:"
|
print "Command:"
|
||||||
|
|
||||||
# Prints the protocol
|
# Prints the protocol
|
||||||
printFormatted()
|
printFormatted()
|
||||||
|
|
||||||
print
|
print
|
||||||
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
||||||
|
|
||||||
def dispInteractive(self):
|
def dispInteractive(self):
|
||||||
print "Fail2Ban v" + version + " reads log file that contains password failure report"
|
print "Fail2Ban v" + version + " reads log file that contains password failure report"
|
||||||
print "and bans the corresponding IP addresses using firewall rules."
|
print "and bans the corresponding IP addresses using firewall rules."
|
||||||
print
|
print
|
||||||
|
|
||||||
def __sigTERMhandler(self, signum, frame):
|
def __sigTERMhandler(self, signum, frame):
|
||||||
# Print a new line because we probably come from wait
|
# Print a new line because we probably come from wait
|
||||||
|
@ -139,10 +139,10 @@ class Fail2banClient:
|
||||||
elif opt[0] in ["-V", "--version"]:
|
elif opt[0] in ["-V", "--version"]:
|
||||||
self.dispVersion()
|
self.dispVersion()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def __ping(self):
|
def __ping(self):
|
||||||
return self.__processCmd([["ping"]], False)
|
return self.__processCmd([["ping"]], False)
|
||||||
|
|
||||||
def __processCmd(self, cmd, showRet = True):
|
def __processCmd(self, cmd, showRet = True):
|
||||||
beautifier = Beautifier()
|
beautifier = Beautifier()
|
||||||
for c in cmd:
|
for c in cmd:
|
||||||
|
@ -167,7 +167,7 @@ class Fail2banClient:
|
||||||
logSys.error(e)
|
logSys.error(e)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
##
|
##
|
||||||
# Process a command line.
|
# Process a command line.
|
||||||
#
|
#
|
||||||
|
@ -241,13 +241,13 @@ class Fail2banClient:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return self.__processCmd([cmd])
|
return self.__processCmd([cmd])
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Start Fail2Ban server.
|
# Start Fail2Ban server.
|
||||||
#
|
#
|
||||||
# Start the Fail2ban server in daemon mode.
|
# Start the Fail2ban server in daemon mode.
|
||||||
|
|
||||||
def __startServerAsync(self, socket, pidfile, force = False):
|
def __startServerAsync(self, socket, pidfile, force = False):
|
||||||
# Forks the current process.
|
# Forks the current process.
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
|
@ -278,7 +278,7 @@ class Fail2banClient:
|
||||||
except OSError:
|
except OSError:
|
||||||
logSys.error("Could not start %s" % self.SERVER)
|
logSys.error("Could not start %s" % self.SERVER)
|
||||||
os.exit(-1)
|
os.exit(-1)
|
||||||
|
|
||||||
def __waitOnServer(self):
|
def __waitOnServer(self):
|
||||||
# Wait for the server to start
|
# Wait for the server to start
|
||||||
cnt = 0
|
cnt = 0
|
||||||
|
@ -306,16 +306,16 @@ class Fail2banClient:
|
||||||
cnt += 1
|
cnt += 1
|
||||||
if self.__conf["verbose"] > 1:
|
if self.__conf["verbose"] > 1:
|
||||||
sys.stdout.write('\n')
|
sys.stdout.write('\n')
|
||||||
|
|
||||||
|
|
||||||
def start(self, argv):
|
def start(self, argv):
|
||||||
# Command line options
|
# Command line options
|
||||||
self.__argv = argv
|
self.__argv = argv
|
||||||
|
|
||||||
# Install signal handlers
|
# Install signal handlers
|
||||||
signal.signal(signal.SIGTERM, self.__sigTERMhandler)
|
signal.signal(signal.SIGTERM, self.__sigTERMhandler)
|
||||||
signal.signal(signal.SIGINT, self.__sigTERMhandler)
|
signal.signal(signal.SIGINT, self.__sigTERMhandler)
|
||||||
|
|
||||||
# Reads the command line options.
|
# Reads the command line options.
|
||||||
try:
|
try:
|
||||||
cmdOpts = 'hc:s:p:xdviqV'
|
cmdOpts = 'hc:s:p:xdviqV'
|
||||||
|
@ -324,9 +324,9 @@ class Fail2banClient:
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
self.dispUsage()
|
self.dispUsage()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.__getCmdLineOptions(optList)
|
self.__getCmdLineOptions(optList)
|
||||||
|
|
||||||
verbose = self.__conf["verbose"]
|
verbose = self.__conf["verbose"]
|
||||||
if verbose <= 0:
|
if verbose <= 0:
|
||||||
logSys.setLevel(logging.ERROR)
|
logSys.setLevel(logging.ERROR)
|
||||||
|
@ -346,7 +346,7 @@ class Fail2banClient:
|
||||||
|
|
||||||
# Set the configuration path
|
# Set the configuration path
|
||||||
self.__configurator.setBaseDir(self.__conf["conf"])
|
self.__configurator.setBaseDir(self.__conf["conf"])
|
||||||
|
|
||||||
# Set socket path
|
# Set socket path
|
||||||
self.__configurator.readEarly()
|
self.__configurator.readEarly()
|
||||||
conf = self.__configurator.getEarlyOptions()
|
conf = self.__configurator.getEarlyOptions()
|
||||||
|
@ -360,7 +360,7 @@ class Fail2banClient:
|
||||||
ret = self.__readConfig()
|
ret = self.__readConfig()
|
||||||
self.dumpConfig(self.__stream)
|
self.dumpConfig(self.__stream)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
# Interactive mode
|
# Interactive mode
|
||||||
if self.__conf["interactive"]:
|
if self.__conf["interactive"]:
|
||||||
try:
|
try:
|
||||||
|
@ -401,14 +401,14 @@ class Fail2banClient:
|
||||||
self.__configurator.convertToProtocol()
|
self.__configurator.convertToProtocol()
|
||||||
self.__stream = self.__configurator.getConfigStream()
|
self.__stream = self.__configurator.getConfigStream()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def __readJailConfig(self, jail):
|
def __readJailConfig(self, jail):
|
||||||
self.__configurator.readAll()
|
self.__configurator.readAll()
|
||||||
ret = self.__configurator.getOptions(jail)
|
ret = self.__configurator.getOptions(jail)
|
||||||
self.__configurator.convertToProtocol()
|
self.__configurator.convertToProtocol()
|
||||||
self.__stream = self.__configurator.getConfigStream()
|
self.__stream = self.__configurator.getConfigStream()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
#@staticmethod
|
#@staticmethod
|
||||||
def dumpConfig(cmd):
|
def dumpConfig(cmd):
|
||||||
for c in cmd:
|
for c in cmd:
|
||||||
|
|
|
@ -50,24 +50,24 @@ class RegexStat:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s(%r) %d failed: %s" \
|
return "%s(%r) %d failed: %s" \
|
||||||
% (self.__class__, self.__failregex, self.__stats, self.__ipList)
|
% (self.__class__, self.__failregex, self.__stats, self.__ipList)
|
||||||
|
|
||||||
def inc(self):
|
def inc(self):
|
||||||
self.__stats += 1
|
self.__stats += 1
|
||||||
|
|
||||||
def getStats(self):
|
def getStats(self):
|
||||||
return self.__stats
|
return self.__stats
|
||||||
|
|
||||||
def getFailRegex(self):
|
def getFailRegex(self):
|
||||||
return self.__failregex
|
return self.__failregex
|
||||||
|
|
||||||
def appendIP(self, value):
|
def appendIP(self, value):
|
||||||
self.__ipList.extend(value)
|
self.__ipList.extend(value)
|
||||||
|
|
||||||
def getIPList(self):
|
def getIPList(self):
|
||||||
return self.__ipList
|
return self.__ipList
|
||||||
|
|
||||||
class Fail2banRegex:
|
class Fail2banRegex:
|
||||||
|
|
||||||
test = None
|
test = None
|
||||||
|
|
||||||
CONFIG_DEFAULTS = {'configpath' : "/etc/fail2ban/"}
|
CONFIG_DEFAULTS = {'configpath' : "/etc/fail2ban/"}
|
||||||
|
@ -87,7 +87,7 @@ class Fail2banRegex:
|
||||||
self.__logging_level = self.__verbose and logging.DEBUG or logging.WARN
|
self.__logging_level = self.__verbose and logging.DEBUG or logging.WARN
|
||||||
logging.getLogger("fail2ban").addHandler(self.__hdlr)
|
logging.getLogger("fail2ban").addHandler(self.__hdlr)
|
||||||
logging.getLogger("fail2ban").setLevel(logging.ERROR)
|
logging.getLogger("fail2ban").setLevel(logging.ERROR)
|
||||||
|
|
||||||
#@staticmethod
|
#@staticmethod
|
||||||
def dispVersion():
|
def dispVersion():
|
||||||
print "Fail2Ban v" + version
|
print "Fail2Ban v" + version
|
||||||
|
@ -99,7 +99,7 @@ class Fail2banRegex:
|
||||||
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
||||||
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
||||||
dispVersion = staticmethod(dispVersion)
|
dispVersion = staticmethod(dispVersion)
|
||||||
|
|
||||||
#@staticmethod
|
#@staticmethod
|
||||||
def dispUsage():
|
def dispUsage():
|
||||||
print "Usage: "+sys.argv[0]+" [OPTIONS] <LOG> <REGEX> [IGNOREREGEX]"
|
print "Usage: "+sys.argv[0]+" [OPTIONS] <LOG> <REGEX> [IGNOREREGEX]"
|
||||||
|
@ -128,7 +128,7 @@ class Fail2banRegex:
|
||||||
print
|
print
|
||||||
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
||||||
dispUsage = staticmethod(dispUsage)
|
dispUsage = staticmethod(dispUsage)
|
||||||
|
|
||||||
def getCmdLineOptions(self, optList):
|
def getCmdLineOptions(self, optList):
|
||||||
""" Gets the command line options
|
""" Gets the command line options
|
||||||
"""
|
"""
|
||||||
|
@ -204,7 +204,7 @@ class Fail2banRegex:
|
||||||
print "Use regex line : " + stripReg
|
print "Use regex line : " + stripReg
|
||||||
self.__failregex = [RegexStat(value)]
|
self.__failregex = [RegexStat(value)]
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def testIgnoreRegex(self, line):
|
def testIgnoreRegex(self, line):
|
||||||
found = False
|
found = False
|
||||||
for regex in self.__ignoreregex:
|
for regex in self.__ignoreregex:
|
||||||
|
@ -221,7 +221,7 @@ class Fail2banRegex:
|
||||||
finally:
|
finally:
|
||||||
self.__filter.delIgnoreRegex(0)
|
self.__filter.delIgnoreRegex(0)
|
||||||
logging.getLogger("fail2ban").setLevel(self.__logging_level)
|
logging.getLogger("fail2ban").setLevel(self.__logging_level)
|
||||||
|
|
||||||
def testRegex(self, line):
|
def testRegex(self, line):
|
||||||
found = False
|
found = False
|
||||||
for regex in self.__ignoreregex:
|
for regex in self.__ignoreregex:
|
||||||
|
@ -251,7 +251,7 @@ class Fail2banRegex:
|
||||||
logging.getLogger("fail2ban").setLevel(logging.CRITICAL)
|
logging.getLogger("fail2ban").setLevel(logging.CRITICAL)
|
||||||
for regex in self.__ignoreregex:
|
for regex in self.__ignoreregex:
|
||||||
self.__filter.delIgnoreRegex(0)
|
self.__filter.delIgnoreRegex(0)
|
||||||
|
|
||||||
def printStats(self):
|
def printStats(self):
|
||||||
print
|
print
|
||||||
print "Results"
|
print "Results"
|
||||||
|
@ -300,20 +300,20 @@ class Fail2banRegex:
|
||||||
print " %s (%s)%s" % (
|
print " %s (%s)%s" % (
|
||||||
ip[0], timeString, ip[2] and " (already matched)" or "")
|
ip[0], timeString, ip[2] and " (already matched)" or "")
|
||||||
print
|
print
|
||||||
|
|
||||||
print "Date template hits:"
|
print "Date template hits:"
|
||||||
for template in self.__filter.dateDetector.getTemplates():
|
for template in self.__filter.dateDetector.getTemplates():
|
||||||
if self.__verbose or template.getHits():
|
if self.__verbose or template.getHits():
|
||||||
print `template.getHits()` + " hit(s): " + template.getName()
|
print `template.getHits()` + " hit(s): " + template.getName()
|
||||||
print
|
print
|
||||||
|
|
||||||
print "Success, the total number of match is " + str(total)
|
print "Success, the total number of match is " + str(total)
|
||||||
print
|
print
|
||||||
print "However, look at the above section 'Running tests' which could contain important"
|
print "However, look at the above section 'Running tests' which could contain important"
|
||||||
print "information."
|
print "information."
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
fail2banRegex = Fail2banRegex()
|
fail2banRegex = Fail2banRegex()
|
||||||
# Reads the command line options.
|
# Reads the command line options.
|
||||||
|
|
|
@ -46,7 +46,7 @@ logSys = logging.getLogger("fail2ban")
|
||||||
# Its first goal was to protect a SSH server.
|
# Its first goal was to protect a SSH server.
|
||||||
|
|
||||||
class Fail2banServer:
|
class Fail2banServer:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__server = None
|
self.__server = None
|
||||||
self.__argv = None
|
self.__argv = None
|
||||||
|
@ -55,7 +55,7 @@ class Fail2banServer:
|
||||||
self.__conf["force"] = False
|
self.__conf["force"] = False
|
||||||
self.__conf["socket"] = "/var/run/fail2ban/fail2ban.sock"
|
self.__conf["socket"] = "/var/run/fail2ban/fail2ban.sock"
|
||||||
self.__conf["pidfile"] = "/var/run/fail2ban/fail2ban.pid"
|
self.__conf["pidfile"] = "/var/run/fail2ban/fail2ban.pid"
|
||||||
|
|
||||||
def dispVersion(self):
|
def dispVersion(self):
|
||||||
print "Fail2Ban v" + version
|
print "Fail2Ban v" + version
|
||||||
print
|
print
|
||||||
|
@ -65,7 +65,7 @@ class Fail2banServer:
|
||||||
print
|
print
|
||||||
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
||||||
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
||||||
|
|
||||||
def dispUsage(self):
|
def dispUsage(self):
|
||||||
""" Prints Fail2Ban command line options and exits
|
""" Prints Fail2Ban command line options and exits
|
||||||
"""
|
"""
|
||||||
|
@ -88,7 +88,7 @@ class Fail2banServer:
|
||||||
print " -V, --version print the version"
|
print " -V, --version print the version"
|
||||||
print
|
print
|
||||||
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
||||||
|
|
||||||
def __getCmdLineOptions(self, optList):
|
def __getCmdLineOptions(self, optList):
|
||||||
""" Gets the command line options
|
""" Gets the command line options
|
||||||
"""
|
"""
|
||||||
|
@ -109,11 +109,11 @@ class Fail2banServer:
|
||||||
if opt[0] in ["-V", "--version"]:
|
if opt[0] in ["-V", "--version"]:
|
||||||
self.dispVersion()
|
self.dispVersion()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def start(self, argv):
|
def start(self, argv):
|
||||||
# Command line options
|
# Command line options
|
||||||
self.__argv = argv
|
self.__argv = argv
|
||||||
|
|
||||||
# Reads the command line options.
|
# Reads the command line options.
|
||||||
try:
|
try:
|
||||||
cmdOpts = 'bfs:p:xhV'
|
cmdOpts = 'bfs:p:xhV'
|
||||||
|
@ -122,9 +122,9 @@ class Fail2banServer:
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
self.dispUsage()
|
self.dispUsage()
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
self.__getCmdLineOptions(optList)
|
self.__getCmdLineOptions(optList)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.__server = Server(self.__conf["background"])
|
self.__server = Server(self.__conf["background"])
|
||||||
self.__server.start(self.__conf["socket"],
|
self.__server.start(self.__conf["socket"],
|
||||||
|
@ -135,7 +135,7 @@ class Fail2banServer:
|
||||||
logSys.exception(e)
|
logSys.exception(e)
|
||||||
self.__server.quit()
|
self.__server.quit()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
server = Fail2banServer()
|
server = Fail2banServer()
|
||||||
if server.start(sys.argv):
|
if server.start(sys.argv):
|
||||||
|
|
Loading…
Reference in New Issue