rastoboy has asked for the wisdom of the Perl Monks concerning the following question:
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:
The first machine has a different sudo password, and thus will fail. But when I try this I get:#!/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; }
And if I hit ctrl-c I'll get the password prompt: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
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?Password:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trapping sudo failures via Net::OpenSSH
by rastoboy (Monk) on Mar 06, 2011 at 18:50 UTC |