my $timeout = 50; use Expect; $Expect::Exp_Internal = 1; $Expect::Debug = 1; my $conn = Expect->new; $conn->raw_pty(1); $conn->log_user(0); $conn->spawn('/usr/bin/ssh', -l => 'USERNAME', 'IPADDRESS', -s => 'sftp') or die "Cannot spawn : $!"; $conn->expect($timeout, "Password:") or die "Password not requested as expected"; $conn->send("PASSWORD\n"); $conn->expect($timeout, "\n"); my $sftp = Net::SFTP::Foreign->new(transport => $conn); die "unable to stablish SSH connection: ". $sftp->error # The rest is the same as above