use strict; use warnings; use Expect; my $timeout = 5; my $command = '/usr/bin/ssh'; my $user='johndoe'; my $host='192.168.47.11'; my $password = 'godsexpower'; my $exp = Expect->spawn($command, "$user\@$host") or die "Cannot spawn $command: $!\n"; $exp->expect($timeout, [ qr{password:} , sub {$exp->send("$password\n")} ]); $exp->expect($timeout, [ qr{\$ } , sub {$exp->send("ls\n")} ]); $exp->expect($timeout, [ qr{\n*?\$ } , sub {print "got the output: '". $exp->before()."'\n"} ]); $exp->soft_close();