- Some object-oriented code testing

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@6 a942ae1a-1317-0410-a47c-b1dcaea8d605
0.5
Cyril Jaquier 2004-10-09 15:33:33 +00:00
parent 5eb163bc03
commit f943028449
4 changed files with 139 additions and 1 deletions

View File

@ -20,9 +20,17 @@
#
# $Revision$
__author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
import posix,sys,os
import string,re,time
from firewall.iptables import Iptables
def checkForRoot():
""" Check for root user.
"""
@ -32,6 +40,7 @@ def checkForRoot():
else:
return False
# start: To be removed
def executeCmd(cmd):
return #os.system(cmd)
@ -44,6 +53,7 @@ def banIP(ip):
iptables = 'iptables -I INPUT 1 -i eth0 -s '+ip+' -j DROP'
executeCmd(iptables)
print iptables
# end:
def checkForUnBan(banList, currentTime, banTime):
""" Check for user to remove from ban list.
@ -87,11 +97,21 @@ def parseLogLine(line):
if __name__ == "__main__":
# start: For object oriented testing
f = Iptables()
f.banIP('11', 1231)
f.banIP('13', 1232)
f.banIP('13', 1233)
f.unBanIP('11')
f.viewBanList()
f.flushBanList()
# end:
if not checkForRoot():
print "You must be root."
#sys.exit(-1)
logPath = './log/temp'
logPath = './log-test/test'
banTime = 60
ignoreIPs = '127.0.0.1'

25
firewall/__init__.py Normal file
View File

@ -0,0 +1,25 @@
# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Author: Cyril Jaquier
#
# $Revision$
__author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"

52
firewall/firewall.py Normal file
View File

@ -0,0 +1,52 @@
# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Author: Cyril Jaquier
#
# $Revision$
__author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
class Firewall:
banList = dict()
def addBanIP(self, ip, time):
self.banList[ip] = time
def delBanIP(self, ip):
del self.banList[ip]
def flushBanList(self):
iterBanList = self.banList.iteritems()
for i in range(len(self.banList)):
element = iterBanList.next()
ip = element[0]
self.unBanIP(ip)
def executeCmd(self, cmd):
return #os.system(cmd)
def viewBanList(self):
iterBanList = self.banList.iteritems()
for i in range(len(self.banList)):
element = iterBanList.next()
print element

41
firewall/iptables.py Normal file
View File

@ -0,0 +1,41 @@
# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Author: Cyril Jaquier
#
# $Revision$
__author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
from firewall import Firewall
class Iptables(Firewall):
def banIP(self, ip, time):
query = 'iptables -I INPUT 1 -i eth0 -s '+str(ip)+' -j DROP'
self.addBanIP(ip, time)
self.executeCmd(query)
print query
def unBanIP(self, ip):
query = 'iptables -D INPUT -i eth0 -s '+str(ip)+' -j DROP'
self.delBanIP(ip)
self.executeCmd(query)
print query