#!/usr/bin/python
+###############################################################################
+# #
+# IPFire.org - A linux based firewall #
+# Copyright (C) 2007, 2008, 2009 Michael Tremer & Christian Schmidt #
+# #
+# This program 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 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program 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 this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+###############################################################################
-import sys,os,re
-#print sys.argv
+import os
+import sys
+import re
-if len(sys.argv) < 2:
- print 'Es wurden keine Parameter uebergeben.'
- sys.exit()
+from IPy import IP
#os.system('modprobe ipt_recent ip_list_tot=1000')
-m = re.findall(r"[1-9]{1,1}[0-9]{0,2}\.[1-9]{1,1}[0-9]{0,2}\.[1-9]{1,1}[0-9]{0,2}\.[1-9]{1,1}[0-9]{0,2}", sys.argv[1])
-#print m
-os.system('echo %s > /proc/net/ipt_recent/BLOCK' % m[0])
+try:
+ ip = IP(sys.argv[1])
+except KeyError:
+ sys.stderr.write("Not enough parameters given.\n")
+ sys.exit(1)
+except ValueError:
+ sys.stderr.write("Given value is not a valid ip address.\n")
+ sys.exit(1)
+
+try:
+ f = open("/proc/net/ipt_recent/BLOCK", "w")
+ f.write("%s\n" % ip)
+ f.close()
+except:
+ sys.stderr.write("Cannot write ip \"%s\" to file.\n" % ip)
+ sys.exit(1)
+
+sys.exit(0)