use Expect; $Expect::Debug = 1; # verbose debug output $Expect::Log_Stdout = 1; # show chatter for debugging # stolen from Net::Telnet::Cisco I think. my $prompt = '[\w-]+\s?(?:\(config[^\)]*\))?\s?[\$#>]\s?(?:\(enable\))?\s*$'; my $exp = Expect->spawn( @command ); # or my $exp = Expect->exp_init($filehandle); sub cmd { my ($exp, $cmd) = @_; $exp->print($cmd, "\n"); $exp->expect($timeout, -re => $prompt) or return; my $output = $exp->before; $output =~ s/\r//g; # silly telnet line endings $output =~ s/^$cmd\n//; # remove the sent cmd return $output; } sub login { my ($exp, $user, $pass) = @_; my $rc = $exp->expect($timeout, [qr/[Pp]assword:\s*/, sub { my $exp = shift; $exp->print($pass,"\n"); exp_continue; }], [qr/[Uu]sername:\s*/, sub { my $exp = shift; $exp->print($user,"\n"); exp_continue; }], -re => $prompt, ); return defined($rc) ? $rc : 0; } sub enable { my ($exp, $pass) = @_; $exp->print("enable","\n"); $exp->login('',$pass); } # generic chat once you have your $exp from a spawn or filehandle login($exp, "username", "password"); cmd($exp,"terminal length 0"); enable($exp, "enablepass"); my $config = cmd($exp, "sho mem"); cmd($exp, "exit"); # enable cmd($exp, "exit"); $exp->soft_close;