#!/usr/bin/perl -w use Expect; #use diagnostics; $\ = "\n"; my $user = 'administrator'; my $password = 'password'; my $timeout = undef; my $exp; my $ip = '192.168.5.106'; my $result; my @command_op; sub Login { $exp = Expect->new(); $exp->debug(2); $exp->spawn ('ssh', '-l', $user, $ip); $exp->log_file("Results.log"); $exp->expect($timeout, '-re', "$user\@$ip\'s password: \$"); $exp->send ("$password\n"); } sub CountArray { $exp->expect($timeout, 're', "$user\@cli>"); @command_op = expect_execute('phydrv'); } ## This subroutine was taken from [Expect command output parser sub] sub expect_execute($) { $exp->clear_accum(); my $command = shift; my $x = ''; my $temp = ''; ##Removed 3 lines frm original because command was being passed without a ; at the end. my @temp; print $exp "$command\n"; ##Just printing $command."\n", as opposed to what was done in the original subroutine. $exp->expect($timeout, 're', "$user\@cli>"); ##Didnt really understand what was being done in the original, so this is what I thought should have been here. my $result = $exp->exp_before(); ##This line just dosen't work, Ive tried it as a direct call in the outer code, but get the same results:( ( my @result ) = split( /\n/, $result ); $result = ''; foreach $x ( @result ) { $temp = $x; if ( chop( $temp ) eq "\r" ) { chop( $x ); } push (@temp, $x); } return @temp; } sub Logout { $exp->expect($timeout, 're', "$user\@cli>"); $exp->send("logout\n"); $exp->soft_close(); } Login; CountArray; Logout; #print $result; print join ("\n", @result);