#!/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(0); # do not show terminal output to STDOUT $exp->log_file("/dev/null"); # log all output to /dev/null $exp->spawn("/bin/sh"); $exp->expect(2, ['such file or directory', sub { print "ERROR: no shell\n"; die; } ], [timeout => sub { $exp->send("/usr/bin/last -5 $username\n"); $exp->log_file(undef); # should clear the log file location $exp->clear_accum(); # should tell it to clear its buffer $exp->log_file(\&formatoutput); # log to function formatoutput $exp->send("/bin/cat /etc/passwd\n"); } ], ); $exp->soft_close(); sub formatoutput { my $input = shift; chomp($input); $input =~ tr/\r//; print"$input \n"; }