#!/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();