use strict; use warnings; use Net::SSH::Perl; use Net::SSH::Perl::Constants qw( :msg ); my $ssh = Net::SSH::Perl->new($user); $ssh->login($username, $password); $ssh->register_handler( SSH_SMSG_STDERR_DATA, sub{ my ( $ssh, $packet ) = @_; my $str = $packet->get_str; if ( $str =~ /Password/ ) { my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str($password); $packet->send; } } ); $ssh->cmd('ssh second_host'); $ssh->cmd('run cmd on second host');