Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks.
I want to capture output of upload script locally
($output) = `/home/jhon/upload -yes `; iam getting directly out put in the screen not in $output
Even i tried with IPC::Cmd it is not show out put in the screen.
But before yes option what ever the out put it is capturing $buffer
if i run using Net::TelNet through remotely i am able to capture the entire out put. @output = TelnetObject->cmd(String => "/home/jhon/upload -yes ");
  • Comment on Get the out put when run script through remotely and locally

Replies are listed 'Best First'.
Re: Get the out put when run script through remotely and locally
by rdfield (Priest) on Sep 26, 2007 at 09:40 UTC
    Perhaps the output is from STDERR. Try
    ($output) = `/home/jhon/upload -yes 2>&1`;

    rdfield

Re: Get the out put when run script through remotely and locally
by sago (Scribe) on Sep 27, 2007 at 06:35 UTC

    This is a sample program, to log the output and notify if required.

    # Modules used
    use Net::FTP;
    use File::Copy;
    use Net::FTP;
    use Net::SMTP;

    sub notify($);
    my $error,$ftp;

    my @t = localtime(time);
    my ($rf, $td);
    $t4++;
    $td = (1900 + $t5) . ((length($t4)==1)?(0 . $t4):$t4) . ((length($t3)>1)?$t3:(0 . $t3)) . ((length($t2)==1)?(0 . $t2):$t2) . ((length($t1)>1)?$t1:(0 . $t1)) . ((length($t[0])>1)?$t[0]:(0 . $t[0]));
    $tds = (1900 + $t5) . ((length($t4)==1)?(0 . $t4):$t4) . ((length($t3)>1)?$t3:(0 . $t3));

    open(LOG,">>/usr/perl_scripts/get_filenames.log");

    mark_time("$tds");
    mark_time("1.Connection to the test server");
    $ftp = Net::FTP->new("testserver.vago.com", Timeout => 75, Debug => 0, BlockSize => 1024);

    if (!$ftp) {
    return(notify("Could not connect to testserver.vago.com"));
    }
    else
    {
    print LOG "Successfull Connection to the server\n";
    }


    mark_time("2.Login to the testserver.vago.com");
    $ftp->login("testftp","123qwe") || (return(notify("Could not login to testserver.vago.com")));
    print LOG "Successfull login to the server\n";


    mark_time("3.Get files from the server and then delete them");
    $ftp->cwd("/home/testftp/cat/rat");
    @dirlist = $ftp->ls();
    foreach $file (@dirlist) {
    $ftp->get($file,"/home/testftp/cat/rat/$file") || (return(notify("Could not get the files")));
    notify("Got the file $file from server to the local machine\n");
    print LOG "$tds $file\n";
    $ftp->delete($file) || (return(notify("Could not delete $file in server")));
    }

    $a= "--------------------------------------------------------";
    open(PLOG, ">>//usr/perl_scripts/process.log");
    printf PLOG ("%-20s%20s\n", $a);
    close(LOG);

    ######################################################################################
    # Send emails
    sub notify ($) {
    my $error = shift;

    my $smtp = Net::SMTP->new('mail.vago.com');
    if (!$smtp) {
    my @t = localtime(time);
    my $td;
    $t4++;
    $td = 1900 + $t5 . "/" . ((length($t4)==1)?(0 . $t4):$t4) . "/" . ((length($t3)>1)?$t3:(0 . $t3)) . " " . ((length($t2)==1)?(0 . $t2):$t2) . ":" . ((length($t1)>1)?$t1:(0 . $t1)) . ":" . ((length($t[0])>1)?$t[0]:(0 . $t[0]));
    print OUT "test.PL Could not connect to SMTP server mail.vago.com at $td\n";
    close(OUT);
    return(undef);
    }
    my $email = "test.pl: $error\n";
    my $MailFrom = "test_test\@vago.com";
    $smtp->mail( $MailFrom );

    $smtp->recipient("sago.gara\@vago.com", { SkipBad => 1 });
    $smtp->data();
    $smtp->datasend("Subject: Test: FTP \n");
    $smtp->datasend("\n");
    $smtp->datasend( $email );
    $smtp->datasend("\n");
    $smtp->dataend();
    $smtp->quit;

    return($error)
    }
    ####################################################################################################
    # print out time stamp and message
    sub mark_time ($) {
    my $message = shift;
    my @t = localtime(time);

    $t4++;
    my $runtime = 1900 + $t5 . "-" . ((length($t4)==1)?(0 . $t4):$t4) . "-" . ((length($t3)>1)?$t3:(0 . $t3)) . " " . ((length($t2)==1)?(0 . $t2):$t2) . ":" . ((length($t1)>1)?$t1:(0 . $t1)) . ":" . ((length($t[0])>1)?$t[0]:(0 . $t[0]));
    open(PLOG, ">>/usr/perl_scripts/process.log") or warn("can't open log file: $!");
    printf PLOG ("%-20s%20s\n", $message, $runtime);
    close(PLOG);
    }