Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:59:09+00:00 2026-06-17T05:59:09+00:00

I’m having difficulty with one little bit of my code. open (files,$list); while (my

  • 0

I’m having difficulty with one little bit of my code.

open ("files","$list");
while (my $sort = <files>) {
  chomp $sort;
  foreach my $key (sort keys %ips) {
    if ($key =~ $sort) {
      print "key $key\n";
      my $match =qx(iptables -nL | grep $key 2>&1);
      print "Match Results $match\n";
      chomp $match;
      my $banned = $1 if $match =~ (/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/);
      print "Banned Results $banned\n";
      if ($key =~ $banned) {
        print "Already banned $banned\n";
      } else {
        system ("iptables -A INPUT -s $key -j DROP");
        open my $fh, '>>', 'banned.out';
        print "Match Found we need to block it $key\n";
        print $fh "$key:$timestamp\n";
        close $fh;
      }
    }
  }
}

So basically what I’m doing is opening a list of addresses 1 per line.

Next I’m sorting down my key variable from another section of my script and matching it with my list, if it matches then it continues on to the if statement.

Now with that matched key I need to check and see if its blocked already or not, so I’m using a qx to execute iptables and grep for that variable. If it matches everything works perfectly.

If it does not match, in other words my iptables -nL | grep $key returns a blank value instead of moving on to my else statement it “grabs” that blank value for $match and continues to execute.

For the life of me I can’t figure out how to strip that blank value out and basically show it as no return.

I know there are modules for iptables etc however I have to keep this script as generic as possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:59:11+00:00Added an answer on June 17, 2026 at 5:59 am

    The problem is that, when iptables returns no results, $banned is left at its default value of undef. Used as a regex, $banned matches every string, so your condition:

    if ($key =~ $banned) {
    

    always matches. I think what you meant to write was probably

    if ($key eq $banned) {
    

    which will fail if either $banned is undef (because $matched was empty or didn’t match the regex) or if the IP address you pulled out with the regex was somehow different from $key.

    If you’re confident that the first IP in the iptables result will be the same as $key then you could simplify your condition to just

    if ($match =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.