Hey guys,
There have been many people that have helped me in the loss of my perl virginity over the past two weeks. I've finally come up with a product that is 90% complete. All I have left to do is get file permissions for each of the folders and files that are found in these long directory/file lists. I'm completely stumped on how to do this. I've looked through some co-workers unix text books and googled some stuff but I just can't get anything to work the way I want it to work.
So what I want to do is simply write a subroutine that I can call foreach element in my array of files and folders, that will get the permissions for that file. I want to know who can do what with it.
Please note I can't use modules, and I've made it this far not using any. Without further ado, here's the code thus far:
CODE
#!/usr/bin/perl
###############################################################
#This script is used to ensure the permissions of significant
#files are set correctly, and that the correct number of files
#and directories are existent in the proper location.
###############################################################
#Used to detect all directories in a given original directory.
sub getfulldirectorylist {
my @final = ();
my $dirtop = @_[0];
print "Reading data from $dirtop\n";
my @dirlist = `find $dirtop -type d`;
my $dirlist = @dirlist;
for ($i = 0; $i < $dirlist; $i++) {
my $temp = @dirlist[$i];
$temp =~ s/( |\$)/\\$1/g;
if ($temp =~ m/\/store\/ariel/) {
}
else {
@final[$i] = $temp;
}
}
return @final;
}
#Used to detect all files found in a given directory.
sub getdirectorycontents {
my $dirname = @_[0];
my @contents = `ls -A $dirname`;
return @contents;
}
#Used to detect the permissions of a given file.
sub getpermissions {
my ($filename, $location) = @_;
my $permission = `cd $location; ls -l | grep $filename | grep nobody`;
if ($permission) {
return "nobody";
}
my $permission = `cd $location; ls -l | grep $filename | grep root`;
if ($permission) {
return "root";
}
}
#Compares the baseline file with the file being tested.
sub compare {
my ($baseline, $test) = @_;
open(FILE, $baseline) or die "Can't open $baseline : $!";
my @baselinefile = <FILE>;
close(FILE);
open(TEST, $test) or die "Can't open $test : $!";
my @testfile = <TEST>;
close(TEST);
my $baselinefile = @baselinefile;
my $testfile = @testfile;
print "\nStarting matching process\n";
my @testresults = ();
$found = 0;
$unfound = 0;
my @unfoundlist = ();
for ($i = 0; $i < $baselinefile; $i++) {
if (($i % 10000) == 0) {
print ".";
}
for ($j = 0; $j < $testfile; $j++) {
if (@baselinefile[$i] eq @testfile[$j]) {
$found = 1;
last;
}
}
if ($found == 0) {
push(@unfoundlist, "@baselinefile[$i]");
$unfound++;
}
$found = 0;
}
$result = $baselinefile - $unfound;
print "\n$result out of $baselinefile files & folders were sucessfully identified\n\n";
print "Writing unfound results to text file: results.txt\n\n";
print "Comparison complete.\n";
write_file("results.txt", @unfoundlist);
}
#Used for writing the file to be tested.
sub write_file {
my ($f, @data ) = @_;
@data = () unless @data;
open F, "> $f" or die "Can't open $f : $!";
print F @data;
close F;
}
#Will populate a file with a list all the directories and files
sub get_info {
my $filename = @_[0];
my @list = ();
print "\n";
for my $topdir ("/store", "/opt/qradar") {
my @dir = getfulldirectorylist("$topdir");
foreach $dir (@dir) {
my @content = getdirectorycontents($dir);
push(@list, "$dir");
foreach $content (@content) {
push(@list, "$content");
}
push(@list, "\n");
}
}
write_file("$filename", @list);
}
##############################################################
#Usability for simple creation of a baseline file or test file
##############################################################
print "What would you like to do?\n1. Create a baseline - type '1'.\n2. Test a box - type '2'.\n";
$input = <STDIN>;
chomp $input;
if ("$input" eq "1") {
get_info("baseline.txt");
print "Baseline written. See baseline.txt for results\n";
}
elsif ("$input" eq "2") {
get_info("test.txt");
compare("baseline.txt", "test.txt");
}
Thanks Guys!!
Edit *** Ignore my getpermissions sub routine. It needs to be replaced!
This post has been edited by n00bc0der: 5 Jun, 2009 - 06:02 AM