#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; foreach my $host (qw/rasto@172.16.0.100 rasto@172.16.0.101/) { my $pass2 = 'orange!'; my $ssh = Net::OpenSSH->new($host); $ssh->error and die "unable to connect to remote host: " . $ssh->error; $ssh->system('sudo -K'); #just ensuring that sudo prompts for password my ($in, $out, $err, $pid) = $ssh->open3('sudo ls'); sleep 1; print $in $pass2 . "\n"; close $in; close $out; close $err; waitpid ($pid, 0); my $ssh2 = Net::OpenSSH->new($host); $ssh2->error and die "unable to connect to remote host: " . $ssh2->error; my @capture = $ssh2->capture('sudo ls 2>&1'); print @capture; }