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: Re: [firefli] Capturing regular expression into 2 arrays: Edit Log



FishMonger
Veteran / Moderator

Apr 5, 2014, 7:10 AM


Views: 4929
Re: [firefli] Capturing regular expression into 2 arrays

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)


Edit Log:
Post edited by FishMonger (Veteran) on Apr 5, 2014, 7:12 AM


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

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