dru145 has asked for the wisdom of the Perl Monks concerning the following question:
I wrote a CGI interface on a secure internal webserver that allows my team to upload a file and add the data to a MySQL DB via DBI. I'm not having any problems with .csv files, but this one is throwing me for a loop. I contemplated telling the team only to use programs that have the ability to produce .csv files, but that limits them to only a few useless programs and I would like to figure this out anyhow. I'm thinking the easiest way to add this data to the MySQL DB would be to build a hash that uses the ip as the key and the rest of the data will be the values, but then I have the scenario, where the banner for certain ports (ie. 80) have to stay with that port, so I guess I need another hash that uses the port number as the key and the banner as the value? Well, I know this is pretty far off, but here is my attempt at sorting out this mess. I would appreciate any assistance that my fellow monks could provide:* + 192.168.99.199 salamander.acme.com |___ 135 DCE endpoint resolution |___ 139 NETBIOS Session Service |___ 1031 BBN IAD |___ 5800 Virtual Network Computing server |___ 5900 Virtual Network Computing server |___ RFB 003.003. * + 192.168.99.131 AMSPWD099WKG |___ 135 DCE endpoint resolution |___ 139 NETBIOS Session Service |___ 445 Microsoft-DS |___ 1027 ICQ? |___ 1099 BBN IAD * + 192.168.99.133 VLW4S |___ 139 NETBIOS Session Service * + 192.168.99.136 WKBX0010B-A |___ 80 World Wide Web HTTP |___ HTTP/1.1 200 OK..Server: Microsoft-IIS/5.0..Date: + Thu, 14 Mar 2002 14:37:54 GMT..Connection: Keep-Alive..Content-Lengt +h: 1270.. |___ 135 DCE endpoint resolution |___ 139 NETBIOS Session Service |___ 443 https MCom |___ 445 Microsoft-DS |___ 1058 nim |___ 1059 nimreg |___ 1433 Microsoft-SQL-Server |___ 5003 Claris FileMaker Pro
This prints:#!/usr/bin/perl -w use strict; my $infile = './portscan2.txt'; my ($ip, @fields, @cname, @ports, @banners); my @field_names = qw(cname port banner); my %data; # Change the input record separator local $/ = "* +"; open INFILE, "$infile" or die "Can't open $infile: $!\n"; while (<INFILE>){ next unless /\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/; $ip = $1; push (@cname, $1) if /\b$ip\b\s{2,}(\w[^\s]*|\[Unknown\])\s{2}/; push (@ports, $1) if /((\d{1,5}?)\s{2}(\w[^\n]*))\s{2}/; push (@banners, $1) if /\Q|___\E\s+([^"]*)\.{1,3}\s+\n/; my $fields = [@cname, @ports, @banners]; @{$data{$ip}}{@field_names} = [$fields]; } foreach my $ip (keys %data) { print "IP = $ip, Computer Name = $data{$ip}{cname}->[0][0]\n"; } close INFILE;
IP = 192.168.99.136, Computer Name = salamander.acme.com IP = 192.168.99.199, Computer Name = salamander.acme.com IP = 192.168.99.131, Computer Name = salamander.acme.com IP = 192.168.99.133, Computer Name = salamander.acme.com
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Which Data Structure Should I Be Using?
by dragonchild (Archbishop) on Apr 02, 2002 at 16:32 UTC | |
by dru145 (Friar) on Apr 02, 2002 at 20:03 UTC | |
by dragonchild (Archbishop) on Apr 02, 2002 at 20:36 UTC |