Greetings Brothers,

I'm having a surprisingly agonizing time trapping sudo failures via the awesome Net::OpenSSH module.

I've been trying it by using Expect, which I posted about here:
http://perlmonks.org/?node_id=890815

No matter what I do, success works great--successful sudos are not a problem--and I can even trap failures--but once I've failed to sudo I can't do any more Net::OpenSSH commands, and in fact the whole program comes to a screaching halt, unless I ctrl-c or interact in some other way, since it is prompting for another password try.

What I would like to do (I think) is simply attempt to sudo, close that connection, and then try again, this time capturing the output. If I get the expected output, then I know the sudo attempt worked. If I don't, then I need to do something else.

Here is what I'm trying currently:

#!/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->err +or; my @capture = $ssh2->capture('sudo ls 2>&1'); print @capture; }
The first machine has a different sudo password, and thus will fail. But when I try this I get:
rasto@frodo:~/cc$ ./test.pl channel 2: bad ext data channel 2: bad ext data channel 2: bad ext data channel 2: bad ext data channel 2: bad ext data channel 2: bad ext data
And if I hit ctrl-c I'll get the password prompt:
Password:
What I'm kinda thinking is that one of the very cool features of Net::OpenSSH, which I've made heavy use of (thank you Salva!), is that it automagically prompts you if a remote machine puts up a sudo password prompt during a session, and it Just Works. So it's like attaching/re-attaching STDIN, STDOUT intelligently (I presume). But now that functionality is getting in my way?

In reply to 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.