Or, is there any way in Expect to get rid of the PTY once the interactive phase is over, using a regular pipe for the rest of the IPC?

Yes, there is, I have found it!

This code spawns a new process with its STDIN and STDOUT redirected to regular pipes/sockets and also attaches a PTY to the child to simulate manual interaction that is handled by Expect.

my $pty = IO::Pty->new; my $expect = Expect->init($pty); my $child = open2($in, $out, '-'); if (defined $child and !$child) { $pty->make_slave_controlling_terminal; exec @cmd; exit -1; } unless (defined $child) { die "unable to spawn new process"; } $expect->expect($timeout, "..."); ... # and once the authentication phase is over, $in and $out are used for + the rest of the IPC.

Also, I have to retain the PTY open until the child program finishes (not just after the authentication is over), otherwise, it gets a SIGPIPE and exits.

Passing a dash (-) to open2 (from IPC::Open2 or open3 from IPC::Open3), makes it fork the current Perl process instead of running an external program.


In reply to Re: Expect and PTY dropping data - solved by salva
in thread Expect and PTY dropping data by salva

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.