NAME

 CLRunner - Command Line Runner - Running command line programs and capturing the output


VERSION

 0.08


SUPPORTED PLATFORMS

 Windows


SYNOPSIS

  my $x=CLRunner->new("dir","*.pl");
  $x->setlogtag("DIRCMD");
  $x->setuplogfile($x->gettimestampfilename().".log");
  print "Logging Status is set to: $x->{LoggingStatus}\n";
  $x->writetolog("Running command: dir *.pl");
  $x->runcommand();
  print $x->getresults();
  $x->writetolog("Done!");


DESCRIPTION

 CLRunner provides an object oriented interface for running command 
 line programs from within a Perl script.  It was developed under
 Windows 2000 and Windows XP Pro using Perl version 5.6.1, ActiveState
 binary build 638.
 It uses basic redirection "2>&1 |" to pipe stdout and stderr.  The output
 is captured and then can search or print.
 I wrote this for myself, while I continue to learn Perl.  If you find it
 useful - please let me know.


METHODS

new($pathtofile,$parameters,$logtag)

 Creates the object
 my $x = CLRunner->new("c:\\windows\\system32\\netstat.exe","-a","NETSTAT-A");
 my $y = CLRunner->new("c:\\windows\\system32\\netstat.exe","-n","NETSTAT-N");

version()

 Returns version number of the CLRunner library.

runcommand()

 Runs the command specified in the command line
 $x->runcommand();

setparams($newparams)

 Sets the command parameters for runcommand().  This can be used
 if you want to change the parameters after you've already created 
 your object.
 $x->setparams("*.txt");

setcommand($newcommand)

 Sets the actual command to run for runcommand().  This can be used 
 if you want to change the command line program after you've already
 created your object.
 $x->setparams("c:\\windows\\system32\\netstat.exe");

getoutput()

 Returns the output of the command run
 $x->runcommand();
 print $x->getoutput();

isstringinoutput($string)

 Search the output generated from runcommand() for the specified string.  If it 
 exists returns 1, if not 0.
 $x=CLRunner->new("c:\\windows\\system32\\netstat","-a");
 $x->runcommand();
 if ($x->isstringinoutput("ESTABLISHED")) { 
      print "There's an established link!\n"; 
 }

getlinefromoutput($string,$occurrence)

 Search the output line by line for the nth $occurrence of $string.  Returns 
 the line, or "" if occurrence not found.
 $x=CLRunner->new("dir","c:\\windows\\system32");
 $x->runcommand();
 print $x->getlinefromoutput("netstat.exe");

findresults($string,$prefix,$noresultmessage)

 Searches each line in the output for $string.  If found it appends 
 line with $prefix and returns all lines.  If not found, returns 
 $noresultmessage.
 $x=CLRunner->new("c:\\windows\\system32\\netstat.exe","-a");
 $x->runcommand();
 print $x->findresults("ESTABLISHED","Connection: ","No established connections");

setlogfile($filenamepath)

 Sets the name of the log file to use for this object. If the file does not exist it will be
 created.  If it does exist, it will be appended.
 Returns 1 if successful, a value less than 1 if not
 $x->setlogfile("c:\\temp\\".$x->gettimestampfilename().".log");

setlogtag($tag)

 Puts the specified text before each log entry. Log entries from 
 multiple objects can be identified in one log file.
 $x->setlogtag("NETSTAT-A");
 $y->setlogtag("NETSTAT-N");

writetolog($messagetext)

 Writes entries to the log file
 $x->writetolog("command completed");

logoutput()

 Writes the command output to the log file.  Returns 1 if successful, otherwise
 a value less than 1.
 $x->logoutput();

setloggingoff()

 Turns logging off.  Returns 1 if successful.
 $x->setloggingoff();

setloggingon()

 Turns logging on. Returns 1 if successful.
 $x->setloggingon();

writetotextfile($filename,$text)

 Write $text to a simple text file $filename.  If the file does not exist it will 
 be created.  If it already exists, it will be appended, unless $text is null
 then file will be overwritten.  Returns 1 on success, otherwise a value less than 1.
 $x->writetotextfile("c:\\temp\\parameters.txt","archive=".$a);

readtextfile($filename)

 Read in all lines of a simple text file.  Treated as if the runcommand()
 was executed.  Use getoutput() to retrieve the lines.  Returns 1 if 
 successful, otherwise a value less than 1.
 $x->readtextfile("c:\\temp\\parameters.txt")

timestamp()

 Returns a timestamp string in the form of "dd-mm-yyyy hh:mm:ss"

gettimestampfilename()

 Returns a base filename (no extension) in the form of "dd-mm-yyhhmmss".
 $x->setlogfile("c:\\temp\\".$x->gettimestampfilename().".log");

sleepinterval($seconds,$char)

 Invokes a sleep-like mechanism, printing a character $char for every second
 specified.  If no character specified, then just sleeps for the number of
 seconds.
 $x->sleepinterval(10,".");


EXAMPLE

 This example is one of the main reasons I started coding this library.  Every 
 interval the nestat command is run.  All "ESTABLISHED" connections are displayed
 and logged.  I wanted to log who was connecting to my machine, or
 what my machine was connecting to.
 use CLRunner;
 my $x;
 my $conns;
 $x=CLRunner->new("c:\\windows\\system32\\netstat.exe","-a","NETSTAT");
 $x->setlogfile("c:\\temp\\".$x->gettimestampfilename().".log");
 while (1) {
    $x->runcommand();
    $conns = $x->findresults("ESTABLISHED","Connections: ","No Connections Found");
    print $conns;
    $x->writetolog($conns);
    $x->sleepinterval(15,"*");
 }


NOTE

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


REVISIONS

 2005/04/24 - 0.08 - Released to the public


AUTHOR

 Scott Horvath (horvathsm@yahoo.com) (http://www.sweetybug.com)
 This is the first time I ever submitted something to the Perl community.  
 I hope it is useful in some way. Please let me know what you think.  Thanks!