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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture output of command and save to text file 1

Status
Not open for further replies.

n3tw0rkadm1n1strat0r

IS-IT--Management
Aug 29, 2006
119
US
I am new to perl and have started this script going off of another one. I am trying to capture the output of a command and save it to a text file. Here is what I have so far:

Code:
#!C:\Perl\bin\perl.exe

open (IN, "omnidb -session -latest -detail|") ;
while (<IN>)
{
$data = $_ ;


 
Ok so if I use perl to email, where would I put in the $file variable?

Code:
#!C:\Perl\bin\perl.exe
# Take output of Data Protector Session and send in an email
 
use warnings;
use strict;
use MIME::Lite;
use Getopt::Std;

my $file = "output_" . time() . ".txt";

open (IN, "omnidb -session -latest -detail|") or die "$!";
open(OUT, ">$file") or die "$!";
while (<IN>)
{
print OUT $_ ;
}

my $SMTP_SERVER = 'spec-mail.spectacor.comcast-spectacor.com';
my $DEFAULT_SENDER = 'ekrengel@comcast-spectacor.com';
my $DEFAULT_RECIPIENT = 'ekrengel@comcast-spectacor.com';
 
MIME::Lite->send('smtp', $SMTP_SERVER, Timeout=>60);
 
my (%o, $msg);
 
# process options
 
getopts('hf:t:s:', \%o);
 
$o{f} ||= $DEFAULT_SENDER;
$o{t} ||= $DEFAULT_RECIPIENT;
$o{s} ||= 'Your binary file, sir';
 
if ($o{h} or !@ARGV) {
    die "usage:\n\t$0 [-h] [-f from] [-t to] [-s subject] file ...\n";
}
 
# construct and send email
 
$msg = new MIME::Lite(
    From => $o{f},
    To   => $o{t},
    Subject => $o{s},
    Data => "Test",
    Type => "multipart/mixed",
);
 
while (@ARGV) {
  $msg->attach('Type' => 'application/octet-stream',
               'Encoding' => 'base64',
               'Path' => shift @ARGV);  #<---HERE MAYBE?
}
 
$msg->send(  );
 
Ok so if I use perl to email, where would I put in the $file variable?

Code:
#!C:\Perl\bin\perl.exe
# Take output of Data Protector Session and send in an email
 
use warnings;
use strict;
use MIME::Lite;
use Getopt::Std;

my $file = "output_" . time() . ".txt";

open (IN, "omnidb -session -latest -detail|") or die "$!";
open(OUT, ">$file") or die "$!";
while (<IN>)
{
print OUT $_ ;
}

my $SMTP_SERVER = 'MYMAILSERVER.COM';
my $DEFAULT_SENDER = 'EMAIL@YAHOO.COM';
my $DEFAULT_RECIPIENT = 'EMAIL@YAHOO.COM';
 
MIME::Lite->send('smtp', $SMTP_SERVER, Timeout=>60);
 
my (%o, $msg);
 
# process options
 
getopts('hf:t:s:', \%o);
 
$o{f} ||= $DEFAULT_SENDER;
$o{t} ||= $DEFAULT_RECIPIENT;
$o{s} ||= 'Your binary file, sir';
 
if ($o{h} or !@ARGV) {
    die "usage:\n\t$0 [-h] [-f from] [-t to] [-s subject] file ...\n";
}
 
# construct and send email
 
$msg = new MIME::Lite(
    From => $o{f},
    To   => $o{t},
    Subject => $o{s},
    Data => "Test",
    Type => "multipart/mixed",
);
 
while (@ARGV) {
  $msg->attach('Type' => 'application/octet-stream',
               'Encoding' => 'base64',
               'Path' => shift @ARGV);  #<---HERE MAYBE?
}
 
$msg->send(  );
 
Sorry for the delay, been off the 'net for a few days...
Code:
[red]#[/red]while (@ARGV) {
  $msg->attach('Type' => 'application/octet-stream',
               'Encoding' => 'base64',
               'Path' => [green]$file][/green]);  #<---HERE MAYBE? YES
[red]#[/red]}
I think we can lose the while loop (in red), and just substitute your $file variable for the contents of @ARGV instead (in green).

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]
 
Steve,

I am getting this error when running the script:

Code:
C:\Program Files\OmniBack\bin>sessiontest.pl

Can't locate MIME/Lite.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at C:\Program Files\OmniBa
ck\bin\sessiontest.pl line 6.
BEGIN failed--compilation aborted at C:\Program Files\OmniBack\bin\sessiontest.pl line 6.
 
I'm trying to find where to download the module but its all in tarballs, I need it for activeperl.
 
Nevermind just figured it out, I had to type this command to install it:

ppm install MIME-Lite

But when running the script, nothing seems to happen, here is the output:

Code:
C:\Program Files\OmniBack\bin>sessiontest.pl
usage:
        C:\Program Files\OmniBack\bin\sessiontest.pl [-h] [-f from] [-t to] [-s subject] file ...

C:\Program Files\OmniBack\bin>
 
Alright i've been messing around with this...and I kindof have it working. The email sends now with the attachement, but the text file attached is blank with no data.

Code:
#!C:\Perl\bin\perl.exe
# Take output of Data Protector Session and send in an email
 
use warnings;
use strict;
use MIME::Lite;
use Getopt::Std;

my $file = "output_" . time() . ".txt";

open (IN, "omnidb -session -latest -detail|") or die "$!";
open(OUT, ">$file") or die "$!";
while (<IN>)
{
print OUT $_ ;
}

my $SMTP_SERVER = 'MAILSERVER';
my $DEFAULT_SENDER = 'EMAIL@YAHOO.COM';
my $DEFAULT_RECIPIENT = 'EMAIL@YAHOO.COM';
 
MIME::Lite->send('smtp', $SMTP_SERVER, Timeout=>60);
 
my (%o, $msg);
 
# process options
 
getopts('hf:t:s:', \%o);
 
$o{f} ||= $DEFAULT_SENDER;
$o{t} ||= $DEFAULT_RECIPIENT;
$o{s} ||= 'Data Protector Test';
 
# construct and send email
 
$msg = new MIME::Lite(
    From => $o{f},
    To   => $o{t},
    Subject => $o{s},
    Data => "Test",
    Type => "multipart/mixed",
);
 

$msg->attach('Type' => 'application/octet-stream',
               'Encoding' => 'base64',
               'Path' => $file);

 
$msg->send(  );
 
maybe:

Code:
$msg->attach(  'Type' => 'application/octet-stream',
               'Encoding' => 'base64',
               'Path' => $file,
               'Filename' => 'test',
               'Disposition' => 'attachment' );


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
it could be that 'Path => needs the full working path to the file. Your script creates a file in the same directory as the script, so use the path to that directory:

Path => "path/to/folder/with/$file";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok I put this in:

'Path' => 'C:\Program Files\OmniBack\bin\$file',

But its telling me that its not readable.
 
use forward slashes and double-quotes:

Path' => "C:/Program Files/OmniBack/bin/$file",

windows has no problem with forward slashes. Using forward slashes is safer than back slashes in paths, other wise you run into meta character interpolation problems. Using single-quotes kills the variable $file.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Try closing OUT before you attach it to the mail...

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]
 
Try what Steve suggests. Also, are you sure the file isn't blank?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
close(OUT);

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That worked! Ok I will test this out now and see if the jobs overlap. Thanks for the help.
 
star for Steve [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top