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: Intermediate:
OpenDir Problem

 



jroberson
Novice

Oct 14, 2008, 9:47 AM

Post #1 of 12 (975 views)
OpenDir Problem Can't Post

Greetings. I am not an expert by any means. I have modified this script in order to parse data and insert into MySQL db. I have having problems with the OpenDir syntax. Any insight what the issue is, I would truly appreciate it.

#!/usr/lib/perl
use strict;
use DBI;

# Connect To Database
my $db = "DB";
my $dsn = "DBI:mysql:database=$db;host=localhost;port=3306;mysql_read_default_file=/etc/mysql/my.cnf";
my $user = "root";
my $pass = "Pwd";

my $dbh = DBI->connect($dsn,$user, $pass, {'RaiseError' => 1});

# Retrieve and open file
my $dir = "/home/Myfolder/"
my opendir(DIR,$dir) || die "Folder not found: $!";
@files = grep (/\.nbe$/, readdir(DIR));

while (my $file = readdir (DIR))
{
my $line = $_;
if ($line =~/results/) # Only pull results from the nbe file.
{
our @values = split (/\|/, $line);
# Only pull full results lines (not just posrtscan entries)

if ($values[5] ne '')
{
my $SQL = "INSERT INTO ScanResults SET IP=' ".$values[2]."',
Port='".$values[3]."', PluginID='".$values[4]."', Type='".$values[5]."',
Description=".$dbh->quote($values[6])."";

print "$SQL\n";

my $sth = $dbh->prepare($SQL) or die "Cannot prepare statement:
$DBI::errstr\n";

$sth->execute() or die "Cannot execute statement: $DBI::errstr\n";
$sth->finish();
}
}
}
$dbh->disconnect ();
close(DIR);

Code



jroberson
Novice

Oct 14, 2008, 9:53 AM

Post #2 of 12 (974 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post

Sorry,

I did not include error message:

No such class opendir at ./nbeparser3a.pl line 15, near "my opendir"
syntax error at ./nbeparser3a.pl line 15, near "my opendir"
Global symbol "$dir" requires explicit package name at ./nbeparser3a.pl line 15.
Global symbol "@files" requires explicit package name at ./nbeparser3a.pl line 16.
Execution of ./nbeparser3a.pl aborted due to compilation errors.

Thanks!


shawnhcorey
Enthusiast


Oct 14, 2008, 10:31 AM

Post #3 of 12 (970 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post


In Reply To
Sorry,

I did not include error message:

No such class opendir at ./nbeparser3a.pl line 15, near "my opendir"
syntax error at ./nbeparser3a.pl line 15, near "my opendir"
Global symbol "$dir" requires explicit package name at ./nbeparser3a.pl line 15.
Global symbol "@files" requires explicit package name at ./nbeparser3a.pl line 16.
Execution of ./nbeparser3a.pl aborted due to compilation errors.

Thanks!


You got the 'my' in the wrong place.


Code
opendir(DIR,$dir) || die "Folder not found: $!"; 
my @files = grep (/\.nbe$/, readdir(DIR));


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


jroberson
Novice

Oct 14, 2008, 10:53 AM

Post #4 of 12 (969 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post

Thanks for your help. I get the following error after the modification. Do you have any suggestions.

Global symbol "$dir" requires explicit package name at ./nbeparser3a.pl line 15.
Execution of ./nbeparser3a.pl aborted due to compilation errors.


KevinR
Veteran


Oct 14, 2008, 11:44 AM

Post #5 of 12 (968 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post


Code
# Retrieve and open file 
my $dir = "/home/Myfolder/";<--missing semi-colon in the code you posted
opendir(DIR,$dir) || die "Folder not found: $!";
@files = grep (/\.nbe$/, readdir(DIR));

-------------------------------------------------


jroberson
Novice

Oct 14, 2008, 12:22 PM

Post #6 of 12 (966 views)
Re: [KevinR] OpenDir Problem [In reply to] Can't Post

Thanks a bunch!! That resolved my issue.


jroberson
Novice

Oct 15, 2008, 11:12 AM

Post #7 of 12 (953 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post

For some read, I am not getting any output. The script runs without error. The directory is correct and the source files exist. Do you think I need to use 'foreach' command. If so, where do I use it. If initially tried it in front of "my @files = grep...", but received errors.

Please, any input.


KevinR
Veteran


Oct 15, 2008, 11:42 AM

Post #8 of 12 (952 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post

I have no idea why sometimes there are no results returned. If there are files in the directory that end with .nbe it does not matter how you find them in the directory, using grep or glob or readdir(), they all should work. My suspicion is you are assuming something that is not correct or your script maybe does not have permission to access certain directories.
-------------------------------------------------


jroberson
Novice

Oct 15, 2008, 12:17 PM

Post #9 of 12 (950 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post

Thanks for your replay. I have confirmed the permissions and are many '.nbe' files in the directory. There should be output on the screen once the script is complete with input in the db. I get nothing. There are no errors. Any suggestions?


KevinR
Veteran


Oct 15, 2008, 12:31 PM

Post #10 of 12 (948 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post

post your code and explain anything that is not clear about what you are doing.
-------------------------------------------------


jroberson
Novice

Oct 16, 2008, 4:58 AM

Post #11 of 12 (931 views)
Re: [KevinR] OpenDir Problem [In reply to] Can't Post

Below is my current code:

#!/usr/lib/perl
use strict;
use DBI;

# Connect To Database
my $db = "xxx";
my $dsn = "DBI:mysql:database=$db;host=localhost;port=3306;mysql_read_default_file=/etc/mysql/my.cnf";
my $user = "xxx";
my $pass = "xxx";

my $dbh = DBI->connect($dsn,$user, $pass, {'RaiseError' => 1});

# Retrieve and open file
my $dir = ".";
opendir(DIR,$dir) || die "Folder not found: $!";
my @files = grep (/\.nbe$/, readdir(DIR));

while (my $file = readdir (DIR))
{
my $line = $_;
if ($line =~/results/) # Only pull results from the nbe file.
{
our @values = split (/\|/, $line);
# Only pull full results lines (not just posrtscan entries)

if ($values[5] ne '')
{
my $SQL = "INSERT INTO ScanResults SET IP=' ".$values[2]."',
Port='".$values[3]."', PluginID='".$values[4]."', Type='".$values[5]."',
Description=".$dbh->quote($values[6])."";

print "$SQL\n";

my $sth = $dbh->prepare($SQL) or die "Cannot prepare statement:
$DBI::errstr\n";

$sth->execute() or die "Cannot execute statement: $DBI::errstr\n";
$sth->finish();
}
}
}
$dbh->disconnect ();
close(DIR);


On the line :

while (my $file = readdir (DIR))

1. Will each line in the file be read. If not do I need to use 'foreach' code and if so where do I enter the information?
2. Is there a way to remove the '\n' from the information was it is entered into the database? => I was able to parse a single file, but not all the '.nbe' files in the directory with opendir.

Thanks for your help.


KevinR
Veteran


Oct 16, 2008, 12:16 PM

Post #12 of 12 (924 views)
Re: [jroberson] OpenDir Problem [In reply to] Can't Post

I don't think you can do this:


Code
my @files = grep (/\.nbe$/, readdir(DIR)); 

while (my $file = readdir (DIR))
{


the grep line read all the data into the @files array. When you try and read from the DIR filehandle in the 'while' loop the buffer that held the data is empty.

You should be looping through @files:


Code
my @files = grep (/\.nbe$/, readdir(DIR)); 
closedir (DIR); #<--- you should close the DIR here

foreach my $file (@files)
{
do something with $file
}


This line looks like you want to search and find something in the nbe files:


Code
if ($line =~/results/) # Only pull results from the nbe file.


But you never opened the nbe file.

There might be other problems but those need to be fixed first.
-------------------------------------------------

 
 


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

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