+

Last commit for bin/fail2ban-jsonfile: 64c8ef57e9dfac97b2f342165aa9bebebfb0163e

Initial commit

Benjamin Renard [2014-12-31 15:04:38]
Initial commit
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, os
import json
from optparse import OptionParser
import logging

parser = OptionParser()

parser.add_option('-j',
                  '--json',
                  action="store",
                  type="string",
                  dest="jsonfile",
                  help="JSON Storage file path")

parser.add_option('-i',
                  '--ip',
                  action="store",
                  type="string",
                  dest="ip",
                  help="IP address")

parser.add_option('-a',
                  '--action',
                  action="store",
                  type="string",
                  dest="action",
                  help="The action : start / stop / ban / unban / check")

parser.add_option('-f',
                  '--failures',
                  action="store",
                  type="string",
                  dest="failures",
                  help="The number of failures")

parser.add_option('-t',
                  '--time',
                  action="store",
                  type="string",
                  dest="time",
                  help="The unix timestamp of the ban time")

parser.add_option('-l',
                  '--log-file',
                  action="store",
                  type="string",
                  dest="logfile",
                  help="Log file path")

parser.add_option('-u',
                  '--umask',
                  action="store",
                  type="int",
                  dest="umask",
                  help="JSON file UMASK",
		  default=0077)

parser.add_option('-v',
                  '--verbose',
                  action="store_true",
                  dest="verbose",
                  help="Enable verbose mode")

parser.add_option('-d',
                  '--debug',
                  action="store_true",
                  dest="debug",
                  help="Enable debug mode")

(options, args) = parser.parse_args()

logformat = '%(asctime)s - %(levelname)s - %(message)s'
if options.debug:
        loglevel = logging.DEBUG
elif options.verbose:
        loglevel = logging.INFO
else:
        loglevel = logging.WARNING

if options.logfile:
        logging.basicConfig(filename=options.logfile,level=loglevel,format=logformat)
else:
        logging.basicConfig(level=loglevel,format=logformat)

if options.action is None or options.jsonfile is None:
	logging.error('You must say what you want to do (-a) and the JSON file path (-j).')
	sys.exit(1)

if options.action in ['ban','unban','check'] and options.ip is None:
	logging.error('You must provide the IP address')
	sys.exit(1)

def load_jsonfile(filepath):
	try:
		json_data = open(filepath)
		data = json.load(json_data)
		return data
	except BaseException,e:
		logging.warning('Fail to load JSON file, return empty hash (Error : %s)' % e)
		return {}

def write_jsonfile(filepath,data):
	try:
		os.umask(options.umask)
		json_file = open(filepath, 'w')
		json.dump(data,json_file)
		return True
	except BaseException,e:
		logging.error('Fail to write JSON file : %s' % e)
		return False

if options.action=='start':
	# Purge data in JSON file
	if not write_jsonfile(options.jsonfile,{}):
		logging.error("Fail to purge JSON file")
		sys.exit(1)
elif options.action=='stop':
	# Remove JSON file
	try:
		os.unlink(options.jsonfile)
	except BaseException,e:
		logging.error('Fail to remove JSON file : %s' % e)
		sys.exit(1)
elif options.action=='ban':
	data=load_jsonfile(options.jsonfile)
	if options.ip not in data:
		data[options.ip]={
			'failures': options.failures,
			'time': options.time
		}
		if not write_jsonfile(options.jsonfile,data):
			sys.exit(1)
elif options.action=='unban':
	data=load_jsonfile(options.jsonfile)
	if options.ip in data:
		del data[options.ip]
		if not write_jsonfile(options.jsonfile,data):
			sys.exit(1)
elif options.action=='check':
	data=load_jsonfile(options.jsonfile)
	if options.ip not in data:
		sys.exit(1)
else:
	logging.error('Invalid action %s. Use -h parameter to get help.' % options.action)
	sys.exit(1)

sys.exit(0)
ViewGit