CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Capturing regular expression into 2 arrays

 



firefli
New User

Apr 4, 2014, 2:22 PM

Post #1 of 4 (4383 views)
Capturing regular expression into 2 arrays Can't Post

 I'm trying to populate the two arrays @IPADR and @MACADR using a binding to a slurped file using captures from regular expressions shown below. In between the two captures I put ".*" but that isn't the question. I had ".*" on the assumption that the second capture would limit the dot's greediness.

If I limit the captures to 1 capture, and remove the other capture [this-> (192\.168\.1\.[\d]+) or that->([A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2})] and just use a single related array on the far left (@IPADR or @MACADR), I can print the 100 values (either only MACADDRESSES or only IPADDRESSES) from the 100 nodes using a single regular expression capture. As soon as I try to put a second capture into the regular expression, I get no output (actually I ended up getting one macaddress and one IPaddress but want a 100 of each) and no error message (running perl -w script.pl). I thought two captures could be used to populate separate arrays but there is something I am unaware of. The eginv.txt file that the regular expression is bound to has around 100 nodes in it, each with their own rows for mac addresses and ip addresses etc. I'd like to get the values for @IPADR and @MACADR into their respective arrays so I could print them so that each node is described on one line like this "NODENAME, IPADDRESS, MACADDRESS, SOMETHINGELSE...." Can I get that with some modifcations to what I have so far?


Thanks




This is the source of the information the script is running against showing one node of about 100.

Code
Nmap scan report for somenode.somedomain.com (192.x.x.x) 
Host is up (0.032s latency).
Not shown: 974 closed ports
PORT STATE SERVICE
53/tcp open domain
...
49160/tcp open unknown
MAC Address: 24:34:E4:57:aB:BC (some company)
Device type: general purpose
Running: Microsoft Windows 7|2008
OS CPE: cpe:/o:microsoft:windows_7::-
cpe:/o:microsoft:windows_7::sp1
cpe:/o:microsoft:windows_server_2008::sp1
cpe:/o:microsoft:windows_8
OS details: Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, or Windows 8
Network Distance: 1 hop


=============================================================================================

Below is the script.This gives 100 MACADDRESSES but I also want the 100 IPADDRESSES

=============================================================================================

Code
my $file = 'eginv.txt';  

{
local( $/ ) ;
open( my $fh, $file ) or die "Oops file dead\n";
my $TEXT = <$fh>;
my $IPADDR=();
my $MACADDR=();
my $RUN=();
my $OSDETL=();
my $HOP=();
my @MACADR=();
my @IPADR=();

#(@IPADR,@MACADR) = $TEXT =~ /(192\.168\.1\.[\d]+).*([A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2})/gs;
(@MACADR) = $TEXT =~ /([A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2})/gs;

#foreach my $ial (@IPADR) {
#print "$ial\n";
# }

foreach my $mal (@MACADR) {
print "$mal\n";
}



(This post was edited by Laurent_R on Apr 5, 2014, 2:22 AM)


Laurent_R
Veteran / Moderator

Apr 5, 2014, 2:24 AM

Post #2 of 4 (4373 views)
Re: [firefli] Capturing regular expression into 2 arrays [In reply to] Can't Post

Please use code tags for your code and data samples. I have edited your post to include them this time, please do it yourself in the future. Thanks.


FishMonger
Veteran / Moderator

Apr 5, 2014, 7:10 AM

Post #3 of 4 (4365 views)
Re: [firefli] Capturing regular expression into 2 arrays [In reply to] Can't Post

Personally, I would not slurp the entire file into a scalar. I would process it line-by-line or in record mode so that I can work on one scan at a time. I'd also probably use a different data structure, such as a HoH (Hash of Hashes).

A line-by-line approach on your sample data using the 2 arrays would look like this:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @MACADR;
my @IPADR;

while (<DATA>) {
chomp;
push @IPADR, $1 if /^Nmap scan report .+? \(([^)]+)\)/;
push @MACADR, $1 if /^MAC Address:\s+(\S+)/;
}
print Dumper \@IPADR, \@MACADR;


__DATA__
Nmap scan report for somenode.somedomain.com (192.x.x.x)
Host is up (0.032s latency).
Not shown: 974 closed ports
PORT STATE SERVICE
53/tcp open domain
...
49160/tcp open unknown
MAC Address: 24:34:E4:57:aB:BC (some company)
Device type: general purpose
Running: Microsoft Windows 7|2008
OS CPE: cpe:/o:microsoft:windows_7::-
cpe:/o:microsoft:windows_7::sp1
cpe:/o:microsoft:windows_server_2008::sp1
cpe:/o:microsoft:windows_8
OS details: Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, or Windows 8
Network Distance: 1 hop


outputs:

Code
$VAR1 = [ 
'192.x.x.x'
];
$VAR2 = [
'24:34:E4:57:aB:BC'
];


Using a HoH would look like this:

Code
my (%nmap_scan, $host, $IP); 

while (<DATA>) {
chomp;
if(/^Nmap scan report for (.+?) \(([^)]+)\)/) {
$host = $1;
$IP = $2
}

if(/^MAC Address:\s+(\S+)/) {
$nmap_scan{$host} = { IP => $IP, MAC => $1};
undef $host;
undef $IP;
}
}
print Dumper \%nmap_scan;


Which outputs:

Code
$VAR1 = { 
'somenode.somedomain.com' => {
'IP' => '192.x.x.x',
'MAC' => '24:34:E4:57:aB:BC'
}
};



(This post was edited by FishMonger on Apr 5, 2014, 7:12 AM)


FishMonger
Veteran / Moderator

Apr 5, 2014, 7:16 AM

Post #4 of 4 (4362 views)
Re: [firefli] Capturing regular expression into 2 arrays [In reply to] Can't Post

You might want to take a look at these 2 modules.

Nmap::Scanner - Perform and manipulate nmap scans using perl
http://search.cpan.org/~maxschube/Nmap-Scanner-1.0/lib/Nmap/Scanner.pm

Nmap::Parser - parse nmap scan data with perl
http://search.cpan.org/~apersaud/Nmap-Parser-1.31/Parser.pm

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives