With Net::OpenSSH, I used to be able to connect without any problems using password-interactive authentication (our system administrators having conveniently blocked every other authentication method). Connecting code looks like:

my $ssh = Net::OpenSSH->new( host => $hostname, user => $Username, password => $Password, master_opts => \@ssh_opts, timeout => 10, ) || die("Cannot connect to $hostname");

However, the system administrators have recenctly decided to send me on another adventure by adding the following 2nd prompt after the password prompt:

Your password will expire in XXX days Press any key to continue.

Now Net::OpenSSH is timing out while it waits for the Enter key (CR-NL) to be sent to the terminal. I reviewed the module documentation and source code, and it appears that the code to send the password to the remote terminal is hard-coded. It looks for the colon (:) character, and then it stuffs the password onto the pty.

It was relatively easy to modify the module to deal with my specific situation, by trapping the additional prompt and sending the CR-NL sequence:

elsif ( $$bout =~ s/(.*)Press Enter to Continue//is ) { print STDERR "$1"; print $mpty "\r\n"; }

However, I was wondering, is there any way to use Expect to handle the various messages and prompts that are sent by the server before the user gets logged on?

I noticed that Net::OpenSSH has a way to get an Expect object, but I think it only works after the user is already logged on. Is there is a way to use Expect to deal with additional prompts and system-generated messages before the user gets logged on?

Is there a way to do this, or could the module be enhanced to allow it?

Thanks, Scott


In reply to Using Expect to handle Net::OpenSSH 2nd password prompt? by scotchie

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.