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.