As posted, your code didn't have a declaration or assignment for "$username", but I think I got the idea...

Having tried this out myself (putting in a suitable string for $username), I saw that the output from this instruction:

$exp->send("/usr/bin/last -5 graff\n");
showed up on STDOUT, and I gather from your post that you don't want this. Meanwhile, the output from this instruction:
$exp->send("/bin/cat /etc/passwd\n");
also showed up on STDOUT, and I gather you do want this. I realize that this is just some trimmed-down, debugging code to demonstrate the problem you want to solve, which is to control what does and does not end up on STDOUT.

Thank you for a very clear and concise presentation -- it has led me to understand Expect.pm much better. Your problem is that you need to use the "expect()" method every time you send something to the process that will cause it to produce output; that's the only way to properly control where that output goes.

Look closely at the part of the manpage that describes the "expect()" method, and note the difference between calling in scalar context vs. calling in array context. Structure your code to use the value(s) returned by this method.

Maybe the following version will be closer to what you want for this example:

#!/usr/bin/perl -w use strict; use Expect; $| = 1; my $exp = new Expect; $exp->raw_pty(1); # treat this terminal as a raw file $exp->log_stdout(undef); # do not show terminal output to STDOUT $exp->spawn("/bin/sh"); # typical regex pattern for end of /bin/sh prompt: my $shell_prompt = qr/[\$\#]\s*$/; my $result = $exp->expect(2, -re => $shell_prompt # pattern #1 ); die "Didn't get a shell prompt -- go figure" unless ( $result == 1 ); $exp->send("/usr/bin/last -5 nobody\n"); my @result = $exp->expect(2, -re => $shell_prompt); my $last_output = $result[3]; # element 3 is "before_match" text $exp->send("/bin/cat /etc/passwd\n"); @result = $exp->expect(2, -re => $shell_prompt); my $passwd_file = $result[3]; print "/etc/passwd contains:\n$passwd_file\n"; $exp->soft_close();

In reply to Re: Problems with Expect.pm's logging? by graff
in thread Problems with Expect.pm's logging? by jcpunk

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.