bluethundr has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

Back again for some advice. I wrote a script to automate batch-banning of IPs on our netscaler load balancer. This is a _really_ simple script but for some reason this outputs nothing.

#!/usr/bin/perl use strict; my $workdir=~/txt; open BAD-IPS, "< $workdir/bad-ips" or die " Couldn't open file: $!"; my $i = 1; while (my $line = <BAD-IPS>) { chomp $line; print "add ns simpleacl bp-search-spammer$i DENY -srcIP $line -T +TL 43200"; print "hello thar!\n"; # debug line also prints nothing } close BAD-IPS;


[bluethundr@lcent5-1:~/perl] $:./bad_ips.pl [bluethundr@lcent5-1:~/perl] $:


All the ip list is, is a list of suscpected spammers one per line.

something kinda like this... totally fake of course, just in case I'm mistaken in identifying some of the IPs as those of dirty dirty spammers

123.45.67.2 123.45.67.3 123.45.67.4 123.45.67.5 123.45.67.6


Srsly.. nothing fancy at all. I'm hoping I'm missing something obvious as to why this script has no output at all.

I've tried this out on a few hosts including the one I developed it on, and 2 cloud instances. No love. I've had folks in IRC tell me "works for me", but I'm hoping I can get to the bottom of the issue here.

thanks monks!

Replies are listed 'Best First'.
Re: script outputs nothing/nada
by toolic (Bishop) on Jun 06, 2010 at 03:38 UTC
    That code does not even compile for me. Does it compile for you?
    my $workdir=~/txt;
    I changed that to:
    my $workdir="~/txt";
    but it would be better as:
    my $workdir = "$ENV{HOME}/txt";
    I also changed BAD-IPS to BADIPS everywhere. Those changes got the code to compile.

    One explanation for not getting any output is that your while loop is never entered -- check to see if the bad-ips file is empty. Also, try to print something before the while loop.

      Hi and thanks for playing! :-)

      Ok so it would seem (from the output..or lackthereof) of the debugging statements that the loop isn't even being entered. But _why_?

      here's the current state of affairs:

      #!/usr/bin/perl # use strict; # # my $workdir = "$ENV{HOME}/txt"; open (BADIPS), "< $workdir/bad_ips" or die "Couldn't open file: $!"; print "Can you hear me now?\n"; while (my $line = <BADIPS>) { chomp $line; print "add ns simpleacl bp-search-spammer DENY -srcIP $line -TTL + 43200"; print "Can you hear me now?\n"; } close BADIPS;
      This is what the output looks like:

      [bluethundr@lcent5-1:~/perl] $:./bad_ips.pl Can you hear me now?


      And I hear what you're saying about the input file (bad_ips) potentially being empty. But I gotta tell ya, it sure doesn't look that way to me! I am able to open the file and here is the output of ls:

      [bluethundr@lcent5-1:~/txt] $:ls -l bad_ips -rw-r--r-- 1 bluethundr bluethundr 822 Jun 5 04:24 bad_ips
      any thoughts? tx again!
        open (BADIPS), "< $workdir/bad_ips" or die "Couldn't open file: $!";

        If that's the code you're really using (it's different to your OP), try adding use warnings; after use strict;. That usage of open does something you really don't want (hint: it isn't trying to open bad_ips).

        Try:

        open BADIPS, "<", "$workdir/bad_ips" or die "Couldn't open file: $!";

        instead.

        You should also check the contents of bad_ips. It could be full of newlines.