in reply to Modifying STDOUT and keeping control

From the code you posted, it looks like what you really need is IPC::Open2.
use IPC::Open2; my($rdh, $wrh); my $pid = open2($rdh, $wrh, "telnet", "192.168.1.1");
Now you can print $wrh "some command"; and read back results with $var = <$rdh>; etc. Make sure to read Suffering from Buffering before you embark on that route.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Modifying STDOUT and keeping control
by Anonymous Monk on Oct 14, 2002 at 21:43 UTC
    Tell me, what am I doing wring here...
    open(PIPE, "telnet 192.168.1.1 |") or die $!; while (<PIPE>) { $bah = $_; $bah =~ s/192.168.1.1//; print $bah; } close(PIPE); bash~$ perl test.pl Trying ... Connected to . Escape character is '^]'.
    Locks up before it gets to the login prompt....
    use IPC::Open2; print open2(\*IN, \*OUT, "telnet 10.65.2.252"); print OUT "Trying\n"; while (<IN>) { $_ =~ s/10.65.2.252//; print; } close IN; close OUT; bash~$ perl test1.pl 26210Trying ... Connected to . Escape character is '^]'.
    Does the same thing in the same place, with the exception of printing the pid.

    Am I misunderstanding what can and can't be done here?

    All I need to do is just strip off that ip address, or the whole "trying" message, during execution.( these snips do that, but hang up in the same place ).

      What you are likely running into is that telnet is an interactive application. The buffering that Perl does to read input by lines destroys the interactivity. When the user hits the first key of their username, they expect to see it echoed immediately. But Perl is still sitting there waiting for the end of the line. Luckily, you only need to process the header. Once you finish reading the header, you can stop reading line by line. Either enter a loop that reads and writes a single character. Or find some way to reconnect the telnet stdout to the Perl stdout.

      I'm not sure what you mean by hanging. Do you mean it's not terminating? If so, do you know where it's stuck? Most likely, since the remote host hasn't closed the connection and therefor telnet is still running and hasn't closed the IN handle, while(<IN>) is blocked waiting for data. I also don't see any attempt at actually sending a command to the remote host. Is the user supposed to type that in? You'd have to do that yourself in that case. Are you printing a command but nothing happens? In that case you probably haven't read Suffering from Buffering.

      Also, it seems you haven't quote understood what the file handles do - or is Trying a command on the remote machine? That is the only reason you should be printing it to OUT, but it doesn't seem likely to me.

      Makeshifts last the longest.