From c367e9ff9d61f9cee619c19278a2bdc6d8fc7637 Mon Sep 17 00:00:00 2001 From: VVelox Date: Mon, 20 Feb 2017 03:49:50 -0600 Subject: [PATCH] now requires cron usage --- snmp/fail2ban | 90 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/snmp/fail2ban b/snmp/fail2ban index 1500fd6..3c52126 100644 --- a/snmp/fail2ban +++ b/snmp/fail2ban @@ -2,42 +2,82 @@ # Add this to your snmpd.conf as below. # extend fail2ban /etc/snmp/fail2ban +# +# Then add to your cron tab... +# */3 * * * * /root/snmp-extends/fail2ban.pl -u #make sure this path is correct my $f2bc="/usr/local/bin/fail2ban-client"; +# The cache file to use. +my $cache='/var/cache/fail2ban'; + + ## ## you should not have to touch anything below this ## use strict; use warnings; +use Getopt::Std; -#gets a list of jails -my $jailsOutput=`$f2bc status`; -my @jailsOutputA=split(/\n/, $jailsOutput); -my ( $jailsS )=grep( /Jail\ list/, @jailsOutputA ); -$jailsS=~s/.*\://; -$jailsS=~s/\s//g; -my @jails=split(/\,/, $jailsS); +$Getopt::Std::STANDARD_HELP_VERSION = 1; +sub main::VERSION_MESSAGE { + print "fail2ban-client SNMP extend 0.0.0\n"; +}; -#process jail -my $int=0; -my $total=0; -my $toReturn=''; -while(defined($jails[$int])){ - #get the total for this jail - my $jailStatusOutput=`fail2ban-client status $jails[$int]`; - my @jailStatusOutputA=split(/\n/, $jailStatusOutput); - my ( $jailTotal )=grep(/Currently\ banned\:/, @jailStatusOutputA); - $jailTotal=~s/.*\://; - $jailTotal=~s/\s//g; - - #tally the total and add this jail to the list - $total=$total+$jailTotal; - $toReturn=$toReturn.$jails[$int].' '.$jailTotal."\n"; - - $int++; +sub main::HELP_MESSAGE { + print "\n". + "-u Update '".$cache."'\n"; } -print $total."\n".$toReturn; +#gets the options +my %opts=(); +getopts('u', \%opts); + +if (defined( $opts{u} )){ + + #gets a list of jails + my $jailsOutput=`$f2bc status`; + my @jailsOutputA=split(/\n/, $jailsOutput); + my ( $jailsS )=grep( /Jail\ list/, @jailsOutputA ); + $jailsS=~s/.*\://; + $jailsS=~s/\s//g; + my @jails=split(/\,/, $jailsS); + + #process jail + my $int=0; + my $total=0; + my $toReturn=''; + while(defined($jails[$int])){ + + #get the total for this jail + my $jailStatusOutput=`fail2ban-client status $jails[$int]`; + my @jailStatusOutputA=split(/\n/, $jailStatusOutput); + my ( $jailTotal )=grep(/Currently\ banned\:/, @jailStatusOutputA); + $jailTotal=~s/.*\://; + $jailTotal=~s/\s//g; + + #tally the total and add this jail to the list + $total=$total+$jailTotal; + $toReturn=$toReturn.$jails[$int].' '.$jailTotal."\n"; + + $int++; + } + + open(my $writefh, ">", $cache) or die "Can't open '".$cache."'"; + print $writefh $total."\n".$toReturn; + close($writefh); + + exit 0; +} + + +my $old=''; +open(my $readfh, "<", $cache) or die "Can't open '".$cache."'"; +# if this is over 2048, something is most likely wrong +read($readfh , $old , 10240); +close($readfh); +print $old; + +