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
In reply to Send data to STDIN of a exec'ed program by ndwg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |