#!/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; }