Whats wrong with this code to read file?

MJ55

I have been trying to read a file called "perlthisfile.txt" which is basically the output of nmap on my computer.

I want to get only the ip addresses printed out, so i wrote the following code but it is not working:

#!/usr/bin/perl  
use strict;  
use warnings;  
use Scalar::Util qw(looks_like_number);

print"\n running \n";
open (MYFILE, 'perlthisfile.txt') or die "Cannot open file\n";

while(<MYFILE>) {
    chomp;
    my @value = split(' ', <MYFILE>);
    print"\n before foreach \n";
    foreach my $val (@value) {
        if (looks_like_number($val)) {
            print "\n looks like number block \n";
            if ($val == /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5})/) {
                print "\n$val\n";
            }
        }
    }
}
close(MYFILE);
exit 0;

And when i ran this code the output was:

  running 
  before foreach 
  before foreach 
  looks like number block 
  before foreach 
  looks like number block 
  before foreach 
  looks like number block

My perlthisfile.txt:

  Starting Nmap 6.00 ( http://nmap.org ) at 2013-10-16 22:59 EST
  Nmap scan report for BoB2.iiNet (10.1.1.1)
  Nmap scan report for android-fbff3c3812154cdc (10.1.1.3)
  All 1000 scanned ports on android-fbff3c3812154cdc (10.1.1.3) are closed
  Nmap scan report for 10.1.1.5
  All 1000 scanned ports on 10.1.1.5 are open|filtered
  Nmap scan report for 10.1.1.6
  All 1000 scanned ports on 10.1.1.6 are closed
mpapec

You had a few errors, one of which is regex which should have optional part for port number (: and following \d{1,5})

#!/usr/bin/perl  
use strict;  
use warnings;  

open (my $MYFILE, '<', 'perlthisfile.txt') or die $!;

my $looks_like_ip = qr/( \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} (?: : \d{1,5})? )/x;
while (<$MYFILE>) {
    chomp;
    my @value = split;
    print"\n before foreach \n";
    foreach my $val (@value) {
      if (my ($match) = $val =~ /$looks_like_ip/){
        print "\n$match\n";
      }
      # else { print "$val doesn't contain IP\n" }
    }
}
close($MYFILE) or warn $!;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Whats wrong with this code to read file?

From Dev

javascript Whats wrong with this code

From Dev

Whats wrong in the following code?

From Dev

whats wrong with the android code ?

From Dev

Whats wrong with this java code

From Dev

Whats wrong with this glsl code?

From Dev

Whats wrong with this piece of code?

From Dev

Whats wrong with this JSP code

From Dev

Whats wrong with this C code lines

From Dev

Whats wrong with this code? replace method

From Dev

Whats is wrong with this real mode code

From Dev

Whats wrong with my jQuery code?

From Dev

Whats wrong with my php code?

From Dev

Whats wrong with this code? Objective C

From Dev

Whats wrong with this Jquery selector code?

From Dev

Whats wrong with the given Java Code

From Dev

whats wrong with this code of reading with mmap?

From Dev

Whats wrong with my sql code

From Dev

Process finished with exit code 0 - whats wrong?

From Dev

whats wrong with my capitalize letters in words code?

From Dev

new to c, whats wrong with my code

From Dev

WHats wrong with this code for palindrome of a string array

From Dev

Whats wrong with this c code(linked list implementation)?

From Dev

Whats wrong with my jQuery Code for RadioButtonList

From Dev

python requests read from file status_code wrong

From Dev

Can someone look at my code and tell me whats wrong?

From Dev

Whats wrong in this code? im trying to make a login page

From Dev

Whats wrong with my code?(changing pictures using javascript)

From Dev

Can any body tell whats wrong with this code (New to python:()