Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Redirect expect screen output to file

by ryalagikar (Novice)
on Oct 08, 2015 at 08:05 UTC ( [id://1144149]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,

Am using Expect.pm in my script, and am setting 'exp_internal', 'log_file' values. Still output is not redirecting to he file.

$expect->exp_internal(1); $expect->log_file("/tmp/expect.log", "w");

I want below lines into file.

Starting EXPECT pattern matching... at /usr/local/share/perl5/Expect.pm line 597 Expect::expect('Expect=GLOB(0x2ae3d80)', undef, 'ARRAY(0x2ae42 +18)') called at /products/backend/***/expect3.pl line 86 handle id(9): list of patterns: #1: -re `[\r\n]?[^\r\n]+[%#>$] $' handle id(9): Does `' match: pattern #1: -re `[\r\n]?[^\r\n]+[%#>$] $'? No. handle id(9): Does `\033]0;user1@da3p-host-**-aut001:~\007\033[?1034h[ +user1@da3p-host-**-aut001 ~]$ ' match: pattern #1: -re `[\r\n]?[^\r\n]+[%#>$] $'? YES!! Before match string: `' Match string: `\033]0;user1@da3p-host-**-aut001:~\007\033[?1034h[u +ser1@da3p-host-**-aut001 ~]$ ' After match string: `'

Thanks in advance

Replies are listed 'Best First'.
Re: Redirect expect screen output to file
by u65 (Chaplain) on Oct 08, 2015 at 12:46 UTC

    Can you show a working script in code blocks? Also it would help to put the output in separate code blocks. See Writeup Formatting Tips.

      Working Code

      #!/usr/bin/perl use strict; use warnings; use Getopt::Long qw(GetOptions); use Expect; use Net::OpenSSH; use Log::Log4perl qw(:easy); #Initializing the log file.. my $log_file = "/tmp/linux_cmd_execute_".$id.".log"; Log::Log4perl->easy_init( { level => $INFO, file => "> $log_file", } ); INFO( "expect usage user - $user host - $host , fuser - $fuser , cmd - $cmd "); my $start_time = time(); INFO("ssh to the host $host"); my $ssh = Net::OpenSSH->new("$user:$pswd\@$host"); if($ssh->error) { print "Unable to connect $host " . $ssh->error; exit; } my $expect; my ( $pty, $pid ) = $ssh->open2pty( { stderr_to_stdout => 1 }, "su +do su - $fuser" ) or die "Failed to attempt sudo su - $fuser"; $expect = Expect->init($pty); $expect->exp_internal(1); $expect->log_file("/tmp/expect.log", "w"); my $before; my $exit1; my $output; my $result = $expect->expect( undef, [ "\[\r\n]?\[^\r\n]+\[%#>\$] \$", sub { my $self = shift; INFO("Sending command $cmd"); $self->send("ls \r"); } ] ) or custom_error( "Expect Failed ",$id); $expect->hard_close(); exit 0;
      -----this output i need in a file------ Starting EXPECT pattern matching... at /usr/local/share/perl5/Expect.pm line 597 Expect::expect('Expect=GLOB(0x2b70d80)', undef, 'ARRAY(0x2b712 +18)') called at /products/backend/mopgenie/expect3.pl line 86 handle id(9): list of patterns: #1: -re `[\r\n]?[^\r\n]+[%#>$] $' handle id(9): Does `' match: pattern #1: -re `[\r\n]?[^\r\n]+[%#>$] $'? No. handle id(9): Does `\033]0;user@host:~\007\033[?1034h[user@host ~]$ ' match: pattern #1: -re `[\r\n]?[^\r\n]+[%#>$] $'? YES!! Before match string: `' Match string: `\033]0;user@host:~\007\033[?1034h[user@host ~]$ ' After match string: `' Matchlist: () Calling hook CODE(0x2b70cd8)... Sending 'ls1;date \r' to handle id(9) at /usr/local/share/perl5/Expect.pm line 1421 Expect::print('Expect=GLOB(0x2b70d80)', 'ls1;date \x{d}') call +ed at /products/backend/mopgenie/expect3.pl line 84 main::__ANON__('Expect=GLOB(0x2b70d80)') called at /usr/local/ +share/perl5/Expect.pm line 825 Expect::_multi_expect(undef, undef, 'ARRAY(0x1f52480)') called + at /usr/local/share/perl5/Expect.pm line 602 Expect::expect('Expect=GLOB(0x2b70d80)', undef, 'ARRAY(0x2b712 +18)') called at /products/backend/mopgenie/expect3.pl line 86 Starting EXPECT pattern matching... at /usr/local/share/perl5/Expect.pm line 597 Expect::expect('Expect=GLOB(0x2b70d80)', undef, 'ARRAY(0x2b72a +40)') called at /products/backend/mopgenie/expect3.pl line 122 handle id(9): list of patterns: #1: -re `[\r\n]?[^\r\n]+[%#>$] $'

        It looks like you are using two log files. Does either show anything?

        I've downloaded and am trying to execute the code. Can you please define ALL missing undefined vars (use bogus names if necessary to disguise sensitive data)?. Thanks.

        -Tom

        Update: Please also include your "custom_error" subroutine.

        Update2: I'm making a bit of progress, but I believe it would help more if you could describe plainly what you are trying to accomplish with your script. Using Expect is a last resort IMHO--to be used ONLY when human interaction is needed. There may be a better way to handle your problem. For one thing you might check out auditd.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1144149]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found