in reply to Problems with Expect.pm's logging?
Having tried this out myself (putting in a suitable string for $username), I saw that the output from this instruction:
showed up on STDOUT, and I gather from your post that you don't want this. Meanwhile, the output from this instruction:$exp->send("/usr/bin/last -5 graff\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.$exp->send("/bin/cat /etc/passwd\n");
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();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Problems with Expect.pm's logging?
by jcpunk (Friar) on Jun 20, 2003 at 14:10 UTC |