in reply to Re: Output telnet session to file
in thread Output telnet session to file

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

Replies are listed 'Best First'.
Re^3: Output telnet session to file
by Tanktalus (Canon) on Aug 26, 2009 at 20:52 UTC
    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;