#!/usr/bin/perl use strict; use warnings; use Expect; my @connection=qw(telnet localhost); my $user='xxxxx'; my $pass='*****'; my $timeout = 1; my $data=''; my $exp = Expect->new(); $exp->log_stdout(0); $exp->spawn(@command) or die "Cannot spawn @connection: $!\n"; $exp->expect($timeout, [ qr/login:/i, sub{ my $exp = shift; $exp->send("$user\n"); exp_continue; }], [ qr/password:/i, sub{ my $exp = shift; $exp->send("$pass\n"); exp_continue; }], '-re', qr/[#>:\$] $/, ); $exp->log_stdout(1); $exp->print(qq{cat /etc/fstab\n}); $exp->expect($timeout,'-re',qr/.+[#>:\$] $/s); $data=$exp->match(); $exp->print("exit\n"); $exp->soft_close(); print "OUTPUT:"; print $data;