sans-clue has asked for the wisdom of the Perl Monks concerning the following question:

Is there a standard pl script that one uses to redirect a telnet stream to file, ie, I telnet to an ip on port 6666 and it just starts streaming lines and I wish to output each line to a file. If I telnet 1.2.3.4 6666 > /tmp/file , I don't get anything in /tmp/file until I kill the telnet, which is no good for me. I am a bit green in the socket in and out handling space. Thanks

Replies are listed 'Best First'.
Re: Output telnet session to file
by Tanktalus (Canon) on Aug 26, 2009 at 20:11 UTC

    A bit more detail on what you're trying to accomplish might provide us with the ability to point you in a better direction. Absent that, have you looked at Net::Telnet for getting data from a telnet session? You can do whatever you want with the data coming back, including punting it to a file, and even flushing it immediately.

      I have perl script that reads lines from a file and does all kinds of parsing and such. I am not allowed to touch that script. I just need something to connect to the ip port and read in the lines from thqt socket (telnet) session and output each line to a file so this sacred script can read the file. My perl book doesn't cover Net:Telnet but I'll search the web for it. I thought a telnet-> file would be a commonly needed util. Thanks
        Here's a brief example:
        #!/usr/bin/perl use strict; use warnings; use Net::Telnet; my $t = Net::Telnet->new( Timeout => 10, Port => 80 ); $t->open('www.google.com'); $t->print('GET /'); my $line = 0; while (my $o = $t->getline()) { print ++$line . '. ' .$o; }
        Instead of port 80, you'd use port 6666. Instead of www.google.com, you'd use the real server. Instead of printing GET /, you'd do whatever starts the flow of information (which may be nothing - if so, remove that line). Instead of printing the line inside the while loop to STDOUT (the default), you'd print to a filehandle that you opened prior to the while loop, and you'd flush the filehandle after each write. So, something like this, which, unlike the above, is untested:
        #!/usr/bin/perl use strict; use warnings; use Net::Telnet; my $t = Net::Telnet->new( Timeout => 10, Port => 6666 ); $t->open('mydataserver') or die "Can't connect to mydataserver: $!"; open my $fh, '>', '/tmp/file' or die "Can't write to /tmp/file: $!"; $fh->autoflush(1); while (my $o = $t->getline()) { print $fh $o; # or $fh->print($o); } close $fh;