Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Create a ipset for blocking networks based on internet sources

by mimosinnet (Sexton)
on Apr 24, 2012 at 18:12 UTC ( #966919=note: print w/ replies, xml ) Need Help??


in reply to Re: Create a ipset for blocking networks based on internet sources
in thread Create a ipset for blocking networks based on internet sources

Jwkrahn, thanks very much for the comments and the links where to find the information. It has been great to have some insight on how to reduce code and to understand the logic behind it. It has also been useful to look at the system command and see how it works with lists (so I now use lists in all the system calls instead of scalars). I have spend some time getting the meaning of ( 0 ) x $ulr_number. Good trick!

I have included your suggestions into the original message. Hope this is all right.

Very impressed with perl expressiveness: easy to start write code, tricky to master it! For somebody like me without computer knowledge or experience is very exciting! Very greateful!

Cheers!


Comment on Re^2: Create a ipset for blocking networks based on internet sources
Re^3: Create a ipset for blocking networks based on internet sources
by jwkrahn (Monsignor) on Apr 25, 2012 at 01:49 UTC

    You're welcome.

    I made a little mistake in one sugestion:

    qr/(^([0-9]{1,3}\.){3}[0-9]{1,3})/, ... qr/^(\d.*\d)/, ... qr/(.*)/, ... foreach ( $response->content =~ /$regex/g ) {

    And you "corrected" thusly:

    qr/\n(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/, # 46.137.194.0 + 46.137.194.255 24 2650

    The problem with that is that that pattern will match every line except the first line.    The proper solution is to use the /m option so that the pattern will match at the beginning of every line:

    qr/(^([0-9]{1,3}\.){3}[0-9]{1,3})/m, ... qr/^(\d.*\d)/m, ... qr/(.*)/, ... foreach ( $response->content =~ /$regex/g ) {


    my @sys = (qw(ipset create), "temp_$set_name", split / /,$set_ +type); ... @sys = (qw(ipset create -exist), $set_name, split / /,$set_typ +e);

    The use of / / with split may not do what you want, and it certainly is not what the shell would do.    You should use ' ' instead:

    my @sys = (qw(ipset create), "temp_$set_name", split ' ',$set_ +type); ... @sys = (qw(ipset create -exist), $set_name, split ' ',$set_typ +e);


    $fh->open("> $f_dates_last") || die "Unable to save timestamp urls in +$f_dates_last: $?";

    The $? variable will have no useful information if open fails.    You should use $! or $^E instead.

      Thanks jwkrahn, I really appreciate the corrections! I have included them in the script and read the section on error variables in perlvar.

      Also, my first version used Moose (influenced by the book "Modern Perl"). After looking at the script, and comparing it with the bash scripts, it took more time and resources. I guess that Moose is for larger and more complex projects, so I have rewritten in what I assume is "procedural programming". It has been trivial to have Moose out, transforming the package into a subroutine.

      One thing is puzzeling me is the regex. In a file in this format:

      Start End Netblock Attacks Name Country email 116.45.99.0 116.45.99.255 24 1799 46.21.150.0 46.21.150.255 24 1708 121.243.146.0 121.243.146.255 24 1446

      Applying this regex:

       qr/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/m

      Gives me:

      116.45.99.0 46.21.150.0 121.243.146.0

      And this regex:

      qr/^((\d{1,3}\.){3}\d{1,3})/m

      Gimes me:

      116.45.99.0 99. 46.21.150.0 150. 121.243.146.0 146.

      I would expect the same result.

        qr/^((\d{1,3}\.){3}\d{1,3})/m

        The problem there is that you have two sets of capturing parentheses.    You can change the inner capturing parentheses to non-capturing parentheses:

        qr/^((?:\d{1,3}\.){3}\d{1,3})/m

        And you could change the foreach loop:

        foreach ( $response->content =~ /$regex/g ) { @sys = (qw(ipset add), "temp_$set_name", $_); system(@sys) == 0 or die "Unable to add $_ to temp_$set_na +me because: $?"; }

        To a while loop that only accesses $1:

        while ( $response->content =~ /$regex/g ) { @sys = (qw(ipset add), "temp_$set_name", $1); system(@sys) == 0 or die "Unable to add $_ to temp_$set_na +me because: $?"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://966919]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-07-26 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls