#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use IO::Select; foreach my $host (qw/rasto@172.16.0.102 rasto@172.16.0.100/) { print "doing $host\n"; 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'); my( $in, $out, $err, $pid ) = $ssh->open3("sudo ls"); my $sel = new IO::Select( $in, $err ); my( @readready, @writeready ); foreach my $fh ( $sel->can_write(5) ) { if( $fh == $in && fileno $in ) { syswrite( $in, $pass2 . "\n\n\n\n\n\n" ); } } my @output; foreach my $fh ( $sel->can_read(5) ) { print "inside foreach\n"; @output = <$err>; } if( grep { $_ =~ /Sorry/gsm } @output ) { print "fail\n"; } else { print "success!\n"; } close $in; close $out; close $err; }