Gossamer Forum
Home : General : Perl Programming :

Grab output from a Shell/Cron script?

Quote Reply
Grab output from a Shell/Cron script?
Hi. Just wondering, if I have a Perl scipt that outputs;

Quote:
testing 1
test 2

test3

test 4

test 5


... what/how could I store the output so far in a script, into a variable? I checked here;

http://www.perldoc.com/...8.0/pod/perlvar.html

.. and also searched Google, but couldn't find anything that helps :( I'm pretty sure its gotta be possible. For example, LSQL prints the output of commands into the log file.. so there must be a way to grab it, right? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
Are you using print statements for this output? If so, have you tried using variable and the concanate the variable and return it?

Example:

$output = "statement 1";
$output .= "statement2";
$output .= "statement3";
return $output;
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] Grab output from a Shell/Cron script? In reply to
Yeah, that works to an extent, but certain stuff, like below will not get logged, as it seems to only be passed into the client (my SSH program);

Quote:
Net::FTP: Net::FTP(2.64)
Net::FTP: Exporter(5.562)
Net::FTP: Net::Cmd(2.21)
Net::FTP: IO::Socket::INET(1.25)
Net::FTP: IO::Socket(1.26)
Net::FTP: IO::Handle(1.21)

Net::FTP=GLOB(0x83fdde8)<<< 220-=(<*>)=-.:. (( Welcome to PureFTPd 1.0.12 )) .:.-=(<*>)=-
Net::FTP=GLOB(0x83fdde8)<<< 220-You are user number 3 of 50 allowed.
Net::FTP=GLOB(0x83fdde8)<<< 220-Local time is now 17:37 and the load is 0.18. Server port: 21.
Net::FTP=GLOB(0x83fdde8)<<< 220 You will be disconnected after 15 minutes of inactivity.
Net::FTP=GLOB(0x83fdde8)>>> user ...
Net::FTP=GLOB(0x83fdde8)<<< 331 User ...
OK. Password required
Net::FTP=GLOB(0x83fdde8)>>> PASS ....
Net::FTP=GLOB(0x83fdde8)<<< 230-User ... has group access to: ...
Net::FTP=GLOB(0x83fdde8)<<< 230 OK. Current restricted directory is /
Net::FTP=GLOB(0x83fdde8)>>> PORT 64,69,64,30,185,31
Net::FTP=GLOB(0x83fdde8)<<< 200 PORT command successful
Net::FTP=GLOB(0x83fdde8)>>> STOR defs_backup.def
Net::FTP=GLOB(0x83fdde8)<<< 150 Connecting to port 47391
Net::FTP=GLOB(0x83fdde8)<<< 226-34769.3 Mbytes free disk space
Net::FTP=GLOB(0x83fdde8)<<< 226-File successfully transferred
Net::FTP=GLOB(0x83fdde8)<<< 226 0.325 seconds (measured here), 114.04 Kbytes per second
Net::FTP=GLOB(0x83fdde8)>>> QUIT
Net::FTP=GLOB(0x83fdde8)<<< 221-Goodbye. You uploaded 38 and downloaded 0 kbytes.
Net::FTP=GLOB(0x83fdde8)<<< 221 Logout - CPU time spent: 0.000 seconds.

I was hoping there would be a way to grab the output of the script so far Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
You need to use tie to read from STDER, similar to what I did in my "capturing ftp responces" thread a few days ago. do a search on google groups and you'll churn up a few good examples on the first couple pages.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
Hi. STDER or STDERR OR STDIN ? Laugh

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
Net::FTP logs messages to STDERR when you turn Debug on.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
Great....I have managed to grab all the bebugging info, except the debug output from Net::FTP. I was about to give up on that one. Thanks Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
Mmm.. 'Debug' is set to 1, but I can't seem to access STDERR (probably doing it wrong). How would I go about it? I've tried;

my $ftp_details = $STDERR;

my $ftp_details = <STDERR>;

my $ftp_details = \*STDERR;

Have you actually managed to do something like this before? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
hm.... just noticed this myself. I started a second thread to handle file uploads/downloads so I could have a progress bar with speed statistics. I have logs for everything except the success message for the transfer (the transfer begins in the main process so I do get the request message). Or maybe it's because I'm using retr/stor instead of get/put?

Andy, I'd post my code for my FTP client so you could take a peek but it's well over 500 lines long at this point...

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
>>>Andy, I'd post my code for my FTP client so you could take a peek but it's well over 500 lines long at this point... <<<

Attachment/PM ? Angelic

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
um... okay I uploaded the (unfinished) script to my web site.

http://www.perlmad.com/scripts/ftperl/ftp.cgi.txt
http://www.perlmad.com/scripts/ftperl/threads.pm
http://www.perlmad.com/scripts/ftperl/threads/shared.pm

You may need to install a some Tk modules. Note also, that I haven't spent any time being efficient, tidy, or making comments, and that the program is sort of unstable at this point. Oh yeah, and I haven't coded in all the functions, so clicking on some things won't do anything.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
LOL.... that code means nothing to me :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
Quote:
LOL.... that code means nothing to me :(

yeah, a lot of it is Perl/Tk, of which I knew none of until last week. Thank God for O'Reilly's "Mastering Perl/Tk" and Google Groups. I bought the book a year ago and hadn't touched it untill last week.

Did you try running it?

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
>>>Did you try running it? <<<

Nah, the first one gave me a 500 IS error,so I couldn't get the code for that one :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
how did you get a 500 ISE? did you run it from a browser or from the command line?

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
Just copied and pasted it to a browser window. The other 2 files showed the code fine :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Grab output from a Shell/Cron script? In reply to
After a little trial and error today, I ended up doing something similar to the following to read STDERR output on child threads on the fly. Basicly, after I start a thread, I open STDERR and all output gets printed to a log file. While the thread is executing, a while loop opens the log for reading, comparing it's contents against what has already been printed to the screen, prints any new messages, and exits the loop if the thread signaled it was finished.

STDOUT works also...

Code:
use Net::FTP;
use threads;
use threads::shared;
use strict;

my $done : shared = 0;

my $ftp_log = "";

my $thread = threads->create(\&ftp);

while (1) {
open (LOG, "log");
my $response;

while (my $line = <LOG>) {
$response .= $line;
}

my $ftp_length = length $ftp_log;
my $log_length = length $response;

if ($log_length > $ftp_length) {
my $new = substr($response, $ftp_length);

$ftp_log .= $new;

print $new;
}
close (LOG);
last if $done;
}

sub ftp {
my $server = "domain.com";
my $user = "user";
my $pass = "pass";

open (STDERR, ">log");
my $ftp = Net::FTP->new($server, Debug => 1);

$ftp->login($user, $pass);

$ftp->dir;

$ftp->quit;
close STDERR;

$done++;
}

in my client I also tie()ed STDERR for automatic logging on (usually quick) basic commands that execute in the main program, and needed to untie() to free up the filehandle for writing in my ftp thread. when the thread was through I then tied()ed again.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Grab output from a Shell/Cron script? In reply to
Interesting. I'll have a look into that. Thanks for sharing this code :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!