ndwg has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.#!/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; }
Is there any deep perl magic that can help me, or am I just wishing for something that can't be done?
Thanks, Nathan
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Send data to STDIN of a exec'ed program
by iburrell (Chaplain) on Nov 14, 2002 at 17:52 UTC | |
by ndwg (Beadle) on Nov 14, 2002 at 19:38 UTC | |
by jjdraco (Scribe) on Nov 15, 2002 at 04:25 UTC | |
|
Re: Send data to STDIN of a exec'ed program
by dws (Chancellor) on Nov 14, 2002 at 19:39 UTC | |
by ndwg (Beadle) on Nov 14, 2002 at 20:28 UTC | |
|
Re: Send data to STDIN of a exec'ed program
by zentara (Cardinal) on Nov 15, 2002 at 16:23 UTC |