Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting returned data from a called Perl script

Status
Not open for further replies.

jvilla1983

Programmer
Dec 19, 2007
3
0
0
US
Hi,

Here's what I'm trying to do..


I have a script which I have set up to first check all Perl scripts in a folder, then a main script runs each script in that directory one at a time. When each subscript runs, it returns all gathered data back to the main script. From here, the data goes into an array and then is formatted and is either piped to a file or sent via smtp to a specified user.

The problem I'm having is getting the output back from each of the executed scripts. I'm trying something like..

my $testArray = `/usr/bin/perl $CurrentScript`;

Where $CurrentScript is the path and name to the file I want to execute. The name is peiced together with another portion of the main script and contains the name-XXX where XXX is the subscript number being run.


What am I doing wrong on this? Is there a better way that I should be doing this? I would like this to be modular in that if I put another file in the subscript directory it's seen and run if it's in the proper format.

Thanks!


-Security_Script.pl- *My Main Script*

#!/usr/bin/perl -w

my $HomeDIR = "/home/user/Scripts/";
my @OutputArray = ();
#First, compile the list of modules in the ~/modules file. This determines the size of the array that we are working with
#for the flags. Flags are tripped one way or another. Each module file sends back either a 0 or 1. The 0 meaning pass, 1 #means fail.


#Get Month/Day/Time place into variables..
my ($sec,$min,$hour,$mday,$mon,$year,$wday
,$yday,$isdst) = localtime time;

#offset year to be the current year
$year = $year + 1900;
$mon = $mon + 1;

if ($mon < 10) {
$mon = "0".$mon;
}

if ($mday < 10) {
$mday = "0". $mday;
}


#Create a file delimited by date LSS_DAY.MONTH.YEAR-hour.minute (Hours and minutes are 24 hour)
#This file will be used for all STIG testing output.. It's meant to be opened and closed in every module.

$fname = "LSS_".$mday.$mon.$year."-".$hour.$min.$sec;
open(FH,'>'.$fname) or die "Can't create " . $fname ." output because of error";
close(FH);

#lets user know of recorded output.. But, this can be overridden by adding an automated option if this script was to be
#automated.. (NOTE ADD ARGUMENT FOR THIS)

print ("\n\nOutput of this script will be recorded in ".$fname);
local($|) = (1);
print("\n\nPress <Enter> or <Return> to continue");
my $resp = <STDIN>;

#get the number of modules in a directory.. This determines the number of modules that are to be run during the
#script's duration. It's designed so that anyone can create a module and not need to hard code it in anywhere else
#AS LONG AS it's added into the module's directory and follows the script rules I've set forth. Please follow them...

#This will need to be changed to the /etc/SECScript/Modules/* Directory..

@files = <$HomeDIR . /Modules/*>;
$numfiles=0;
$progress=0;

foreach $files (@files){
$numfiles++;
}
print $numfiles;


foreach $files (@files) {
my @testArray = ();
print("\n\n"."Script number ".$progress);
my $CurrentScript = &FileName;
print $CurrentScript;
my $testArray = `/usr/bin/perl $CurrentScript|`;

print $testArray;
print("\n\n"."Module".$progress." completed..");
$progress = $progress + 1;
}



#This portion defines the subroutines used in this..

sub FileName{
my $CScript = "";
$CScript = $HomeDIR."Modules/Module-";

if($progress < 10){
$CScript = $CScript."00".$progress;
}
elsif(($progress < 100) && ($progress >= 10)){
$CScript = $CScript."0".$Progress;
}

$CScript = $CScript.".pl";

return $CScript;
}







*Example Module Script*..




#!/usr/bin/perl

#This script is the initialization script.. This basically tells the script
#what it's dealing with (Information gathering, etc). Inside here, start times for the script
#are documented, Kernel Version, RHEL Version and whatever I or anyone else can think up..


sub SystemDiags{
my $KernelVersion;
my $RHELSystemVersion;
my $DateTimeCheck;
my $SystemArch;

open(DATETIME, "date|");
$DateTimeCheck = <DATETIME>;
close(DATETIME);

open(RHELVER, "cat /etc/redhat-release|");
$RHELSystemVersion = <RHELVER>;
close(RHELVER);

open(KERNEL, "uname -r|");
$KernelVersion = <KERNEL>;
close(KERNEL);

open(SYSARCH, "uname -i|");
$SystemArch = <SYSARCH>;
close(SYSARCH);

#This was originally passing back an array, I thought I might have better
#luck using a delimiter and then parse the data for it afterwards.. It
#really didn't work.

return($KernelVersion."|".$RHELSystemVersion."|".$DateTimeCheck."|".$SystemArch."|"."/0");

}
 
Executing another script in backticks will shell it out as a new, separate process. It can't return data in the way you want, although if it does a print of each line to STDOUT the resulting print lines will be returned as an array (but you will have to chomp the array to remove the linefeeds).

It looks like the proper way to do it is with require; I've not had cause to use it personally, but there is an example in the perldoc which looks like it might do the trick...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top