Greetings, Brothers!

I have no doubt my lack of Linux knowledge is seriously hampering me in my attempts to automate checking for sudo access on remote machines--any help would be greatly appreciated!

Basically, the idea is that if I have the current user password, that is success, and if not I want to drop into interactive mode to set it correctly (I won't bore you with the grisly why's and wherefores)

I have a simple method that attempts to do this:

sub checksudo { my ($ssh, $ip, $pass) = @_; #$ssh is the successfully created openssh +object, $pass is the user's sudo password my $system = $ssh->system ('sudo -K') or return "failed to sudo -K: $! +\n"; #ensure we are prompted for a password my ( $pty, $pid ) = $ssh->open2pty( "sudo -p 'configtest:' bash 2>&1") or return "failed to attempt sudo bash: $!\n"; my $expect = Expect->init($pty); #sleep 1; #$expect->send ($pass . "\n"); $expect->expect(2, [ qr/configtest:/ => sub { my $expect =shift; $expect->send($p +ass . "\n"); exp_continue;} ], [ qr/Sorry/ => sub { my $expect=shift; print "Failed\n"; e +xp_continue; } ], ); #$expect->interact(); return; }
The above code gives me:
tcsetattr: Input/output error
If I comment out the first array ref in the $expect->expect() statement, I get:
tcsetattr: Input/output error channel 0: chan_read_failed for istate 1
However, if I remove the $expect->expect() statement entirely and uncomment the $expect->send() statement which just sends the password, I get successful sudo's on the remote machine. Likewise if I only have the $expect->interact() statement in place, that works just fine, too.

I have to think I'm fundamentally misunderstanding something--any input would be greatly appreciated!

updateEven when things seem to go fine, it seems that I still get the I/O errors when I try to execute another remote ssh command. The command succeeds but when that error at the top. I can't seem in the docs for Net::OpenSSH how to kill a particular child connection, which I'm guessing is what I need to do?


In reply to Losing my mind with Net::OpenSSH and Expect 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.