I have tried Super Search, CPAN, Google and the Perl Cookbook but perhaps the Monks can save me from thinking I am trying to do the impossible.

What I would like to do is start up an external program (talk or telnet for example), send it some data via STDIN, and then return STDIN of the program to the controlling console. I would like to avoid having the Perl process sitting around redirecting output the whole time.

Here is the code I have so far:

#!/usr/bin/perl use strict; my $cmd = "telnet"; # or talk my $args = "testhost"; # or user my $data = "my_name\nmy_pass\n\nstartup_cmd\n"; # or "hi user\n" print "Running '$cmd' with '$args', and sending '$data'\n"; local $^F = 1000; # set max system file descriptor high! pipe(READ,WRITE); select((select(READ), $| = 1)[0]); if (my $pid = fork) { # PARENT - launches the program #close(WRITE); # don't close the pipe, and '$cmd' doesn't die! :-) # One way, reopen stdin to the pipe: open(STDIN, "<&READ"); exec "$cmd $args"; # Another way, mess with file descriptors # and shell redirection: #my $fd = fileno(READ); #print "fd = $fd\n"; #exec "$cmd $args <&$fd"; # similarly, try to grab STDIN as well: #exec "$cmd $args <&$fd &0<&$fd"; # (however, seems to separate '$cmd' from the console) die "exec of '$cmd' failed. $!"; } else { # CHILD - sends the data close(STDIN); close(STDOUT); close(STDERR); close(READ); print WRITE "$data\n"; # Almost there, but instead of exiting, I need # to set WRITE back to the controlling console exit; }
I need something like the shell command "fg" to perform the missing piece. Other modules that I have looked at, but don't seem to _quite_ do what I need include Proc::Spawn, IPC::Run, and IO::Handle.

Is there any deep perl magic that can help me, or am I just wishing for something that can't be done?

Thanks, Nathan


In reply to Send data to STDIN of a exec'ed program by ndwg

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.