Got it! After a process of elimination, I discovered that the weird errors likely had to do with reading/writing to filehandles that weren't ready to be written to/read from, and I recalled Zentara's post to me in a very similar situation:

http://perlmonks.org/?node_id=884178

And indeed, I found that utilizing IO::Select to only read/write to known ready filehandles that got rid of the bizarre errors. Then it was just a matter of finding the correct voodoo to keep the program from prompting me (the user) for a password, ever, which was basically a matter of appending a bunch of \n's to the password writing process. Lame, yes--but it works :-)

If anyone knows of more elegant ways to do this, that would be great, but this works:

#!/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->e +rror; $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; }

In reply to Re: Trapping sudo failures via Net::OpenSSH by rastoboy
in thread Trapping sudo failures via Net::OpenSSH by rastoboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.