in reply to Scanning multiple files from Snort rules one by one and extracting a particular part to another file - File Handling

Hi edison.pioneer,

Your immediate error:

print "RULE_FILE : $rulefile \n";
At this point in the code, the $rulefile has not been declared (which is done with the my built-in), leading to a compile-time error, and has not been defined or initialized (which is done by assigning a value to it), leading to a run-time error once the compile-time error has been fixed.

Next error (assuming you have added the my function for every new variable):

my $rulesdir = "C:\\Snort\\rules\\*.rules";
This does not define a directory, so that the next code line will not work as expected. If you want to define a directory, you may want to have:
my $rulesdir = "C:\\Snort\\rules\\";
or perhaps simply:
my $rulesdir = "C:/Snort/rules/";
Next issue:
@rulefiles = `ls $rulesdir\/*.rules`;
From looking at your directory paths, you're working on Windows. The ls command is a Unix system command, it will not work under Windows. You should use Perl internal commands rather than system calls whenever possible. Try this:
my $rulesdir = "C:\\Snort\\rules"; my @rulefiles = glob "$ruledir/*.rules";
Another issue (assuming that you have used my and indented your code correctly, as recommended, and straitening a bit the syntax):
for my $rulefile (@rulefiles){ open my $INFILE, "<", $rulefile or die "Can't open $rulefile $!"; my @rules = <$INFILE>; close $INFILE; }
Here, I have corrected and/or improved the syntax, but you have a serious algorithmic problem: you are looping over a list of files (@rulefiles) and, each time through the loop, are assigning the contents of the file to @rules, meaning that when you read the content of the second file, you clobber the previous content of @rules (i.e. what you read from the first file), and so on, so that, in fine, you end up with only the content of the last file.

There are several (actually, many) other issues, but at least, I hope this will help you going forward.

  • Comment on Re: Scanning multiple files from Snort rules one by one and extracting a particular part to another file - File Handling
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Scanning multiple files from Snort rules one by one and extracting a particular part to another file - File Handling
by Anonymous Monk on Jul 22, 2015 at 09:31 UTC

    Laurent_R: ...From looking at your directory paths, you're working on Windows. The ls command is a Unix system command, it will not work under Windows. You should use Perl internal commands rather than system calls whenever possible. Try this: ...

    FWIW, even unix system commands can be available for windows , ls.exe , tar.exe ... not uncommon with perl programmers

      Yes, sure, you're right, it can be available for Windows, but that requires some specific software installation and it is usually not available on a common (or fresh) Windows installation.
Re^2: Scanning multiple files from Snort rules one by one and extracting a particular part to another file - File Handling
by edison.pioneer (Initiate) on Jul 22, 2015 at 18:10 UTC
    Thanks for pointing out my mistakes!! About ls being a UNIX tool and not Windows. I googled and came across a suggestion on some online forum saying that in environmental variables in PATH I must be adding "C:/Windows/System32" and then reboot. Unsurprisingly, it didn't work!