• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

What is the differnce between System and System::Execute in Perl?

I am not a perl expert but in maintaining code, I am seeing System::Execute.  I am do not know what the command !System::Execute($cmd) does or rather System::Execute.  
I am also wondering what is the difference between using System($cmd) and System::Execute($cmd)

One example is:
if (!System::Execute($cmd))
{
      $::ErrorMessage = "Error calling server $::NameServer";
      return 1;
}

I assume that these commands are used in a Unix environment and not a Windows environment.
0
gppkuntz
Asked:
gppkuntz
1 Solution
 
TintinCommented:
System::Execute is not a CPAN module, so it's probably been written by the author of the script.  Without seeing the contents of System::Execute, it's impossible to say exactly what it does.

0
 
Adam314Commented:
Going along with Tintin, is there a "use System" anywhere (likely near the top) of the script?
If so, post the code for the System.pm file.
0
 
ozoCommented:
Did you load a module like
use System;
System::Execute($cmd) would call the Execute sub in the System module
you would need to consult documentation for that module to find what it does
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
gppkuntzAuthor Commented:
I missed the fact that one was a lower case "s" and the other upper case, system($cmd).
The System.pm is pasted below:
package System;
use File::Basename;
BEGIN {
        $::InstallDir = dirname("$0");
        push @INC, "$::InstallDir";

        select(STDERR);
        $| = 1;
        select(STDOUT);
        $| = 1;
        if ($ENV{'ProgRoot'} eq "")
            {$ENV{'ProgRoot'} = "$::InstallDir/..";
             print "Using default ProgRoot=$ENV{'ProgRoot'}\n";}
        push @INC, "$ENV{'ProgRoot'}/engine";
}

use Environment;
use Keyword;

BEGIN { }

#---------------------------------------------------------------------------------
# function: System
# description: Performs a specified system command
# parameters: ShellCommand OutputLog ErrorLog ContinueFlag
#---------------------------------------------------------------------------------
sub Execute {

      my ( $Command, $OutputLog, $ErrorLog, $ContinueFlag, $IsSilent ) = @_;
      my ( $Keyword, $IsPrintCommand );

      $IsPrintCommand = "yes";

      #---------------------------------------------------
      # check to see if secure keyword in command
      #---------------------------------------------------

      foreach $Keyword ($::KeywordsSecure) {
            if ( rindex( "$Command", "##" . $Keyword . "##" ) != -1 ) {

                  #---------------------------------------------------
                  # secure keyword exit, do not print value in log
                  #---------------------------------------------------
                  $IsPrintCommand = "no";
            }
      }

      #---------------------------------------------------
      # if keyword exists, print preprocessed command
      #---------------------------------------------------

      if ( "$IsPrintCommand" eq "yes" ) {
            if ( rindex( "$Command", "##" ) != -1 ) {
                  debugPrint("Executing $Command...\n") unless ($IsSilent);
            }
      }
      debugPrint("Executing $Command...\n") ;

      #---------------------------------------------------
      # if keywords exist, expand command keywords
      #---------------------------------------------------

      $Command = Keyword::Expand("$Command");

      #---------------------------------------------------
      # if specified, redirect output and error
      #---------------------------------------------------

      if ( "$OutputLog" ne "" ) {
            $Command = "$Command >$OutputLog";
      }

      if ( "$ErrorLog" ne "" ) {
            $Command = "$Command 2>$ErrorLog";
      }
      if ( system("$Command") ) {
            #---------------------------------------------------
            # shell command failed
            #---------------------------------------------------

            if ( "$IsPrintCommand" eq "yes" ) {
                  $::ErrorMessage = "Command failed for $Command";
                  infoPrint("Command failed for $Command \n") unless ($IsSilent);
            }
            else {
                  $::ErrorMessage = "Command failed";
                  infoPrint("Command failed \n") unless ($IsSilent);
            }

            if ( "$ContinueFlag" ne "yes" ) { return 0 }

            infoPrint("\nError: $::ErrorMessage") unless ($IsSilent);
            infoPrint("\nError: Continue On error set and processing continued\n\n")
              unless ($IsSilent);
                 return 0;  #when it fails
      }

      return 1;
}

sub infoPrint {
      print( $_[0] ."\n\n" );
      return 1;
}

sub debugPrint {
      if ( lc($::DEBUG_MODE) eq "yes" ) {
            print( $_[0] );
      }
      return 1;
}

sub rmdir {
      my ($dir) = $_[0];
      if ( lc($::DEBUG_MODE) eq "yes" ) {
            print("Attempting to clean up directory: $dir\n");
      }

      # do minimum security check, we do not want to rm -rf the whose root!
      if ( $dir =~ /^ *\/ *$/ ) {
            print("Can not clean root dir in this unix host!\n");
            return 0;
      }
      else {
            print("Cleaning up $dir:\n");
            system("rm -rf $dir");
      }
      return 1;
}

END { }

return 1;
0
 
Adam314Commented:
First, the rmdir function has the comment "do minimum security check, we do not want to rm -fr the whose root".
A malicious user could easily get around the regex using "/tmp/.." - allowing the root to be deleted.  Or the $dir passed could contain some_dir;some_other_command, would would allow the user of this function to run whatever command they wanted.

The System::Execute function you have runs the built-in system command, with a different options:
    The command is replaced using the Keyword::Command function.  Don't know what this function does.
        (look in the Keyword.pm file to see)
    The second parameter, if supplied, is where the commands STDOUT will be redirected
    The third parameter, if supplied, is where the commands STDERR will be redirected
    It looks to see if the command is "secure", supposedly to not print the command in a log
        But the debugPrint will print it anyways (later on it isn't printed though)
    If the system command returned a TRUE value, 0 is returned
    If the system command returned a FALSE value, 1 is returned
0
 
gppkuntzAuthor Commented:
Thanks, I appreciate your time.  The keyword.pm appears not to have been completed before the originator left.  I was hoping that the built-in-system commands were implemented with just some options.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now