You could probably use something like this:

#!/usr/bin/perl use warnings; use strict; # # This script grabs ip addresses from my firewall log file # and adds them to a blacklist for my iptables ruleset. # ## NOTE - This script must be run as root use Socket; # Check to make sure root is running this $< and die "You must run this program as root!\n"; my $log = '/var/log/iptables.log'; my $blacklist = '/var/log/blacklist'; # Open log file, retrieve list of ip addresses and write them # to the blacklist open IN, "<", $log or die "Can not open $log $!"; my %seen; while ( <IN> ) { next unless /\S/; if ( /SRC=([0-9.]+) / ) { next if $1 =~ /^192\.168/; $seen{ inet_aton( $1 ) }++; } } close IN; # Sort my list of IP addresses my @sorted = map inet_ntoa( $_ ), sort keys %seen; # Create clean blacklist file and append iptables rules open BL, '>', $blacklist or die "Cannot open $blacklist $!"; foreach my $ip ( @sorted ) { print BL "$ip\n"; 0 == system '/sbin/iptables', '-A', 'BLACKLIST', '-p', 'all', '-s' +, $ip, '-d', '0/0', '-j', 'LOG', '--log-prefix', 'IPTABLES:Blacklist: + ' or die "system /sbin/iptables failed: $?"; 0 == system '/sbin/iptables', '-A', 'BLACKLIST', '-p', 'all', '-s' +, $ip, '-d', '0/0', '-j', 'DROP' or die "system /sbin/iptables failed: $?"; } close BL; chmod 0600, $blacklist;

In reply to Re^3: Questions regarding regular expressions and arrays by jwkrahn
in thread Questions regarding regular expressions and arrays by at2marty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.