Hi,

What i am trying to achieve using expect is, i have to fetch linux command(command may be simple linux command or shell script or perl script etc..) from database and execute the command based on the results have to perform some operations.

code as below

#!/usr/bin/perl use strict; use warnings; use Getopt::Long qw(GetOptions); use Expect; use Net::OpenSSH; my $user = ''; #linux login user name my $pswd = ''; # Password my $host = ''; #Host name or machine my $fuser = ''; #functional user or sudo user #Connect to host using OpenSSH my $ssh = Net::OpenSSH->new("$user:$pswd\@$host"); if($ssh->error) { print "Unable to connect $host " . $ssh->error; exit; } my $expect; #sudo su 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; $self->send("ls \r"); } ] ) or custom_error( "Expect Failed "); $expect->hard_close(); exit 0; sub custom_error { my $error = shift; print "Error : $error\n"; #some other operations exit 1; }

need below output into a file

Starting EXPECT pattern matching... at /usr/local/share/perl5/Expect.pm line 597 Expect::expect('Expect=GLOB(0x1b086f0)', undef, 'ARRAY(0x1ae5c +80)') called at ./expecct_demo.pl line 40 handle id(5): list of patterns: #1: -re `[\r\n]?[^\r\n]+[%#>$] $' handle id(5): Does `' match: pattern #1: -re `[\r\n]?[^\r\n]+[%#>$] $'? No. handle id(5): Does `\033]0;elkuser1@da3p-gen-imn-aut001:~\007\033[?103 +4h[elkuser1@da3p-gen-imn-aut001 ~]$ ' match: pattern #1: -re `[\r\n]?[^\r\n]+[%#>$] $'? YES!! Before match string: `' Match string: `\033]0;elkuser1@da3p-gen-imn-aut001:~\007\033[?1034 +h[elkuser1@da3p-gen-imn-aut001 ~]$ ' After match string: `' Matchlist: () Calling hook CODE(0x1aa89d0)... Sending 'ls \r' to handle id(5) at /usr/local/share/perl5/Expect.pm line 1421 Expect::print('Expect=GLOB(0x1b086f0)', 'ls \x{d}') called at +./expecct_demo.pl line 38 main::__ANON__('Expect=GLOB(0x1b086f0)') called at /usr/local/ +share/perl5/Expect.pm line 825 Expect::_multi_expect(undef, undef, 'ARRAY(0x1616c90)') called + at /usr/local/share/perl5/Expect.pm line 602 Expect::expect('Expect=GLOB(0x1b086f0)', undef, 'ARRAY(0x1ae5c +80)') called at ./expecct_demo.pl line 40

I can redirect this output to file by running the script in back-end(&) mode. Is there any other way i can achieve this(any expect settings).


In reply to Re^4: Redirect expect screen output to file by ryalagikar
in thread Redirect expect screen output to file by ryalagikar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.