dihewidd has asked for the wisdom of the Perl Monks concerning the following question:

...and then the telnet connection just remains open until I type control-] and then control-D. We need this script to run via crontab, so how can we automate the closing of the connection? Thanks!
open TELNET, "telnet 192.168.1.88 < /dev/tty |"; while (<TELNET>){ # writes data to mySQL DB }
ALTERNATIVELY how do we get the output line by line from Net::Telnet and work with it? Every example I see has it logging in and getting a single line of text. My telnet server has no login. My server just starts spitting data at you. ----------
use Net::Telnet; $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die'); $telnet->open('192.168.1.88');

Replies are listed 'Best First'.
Re: Safely close TELNET connection?
by Mr. Muskrat (Canon) on Jan 18, 2011 at 18:58 UTC

    You should be able to use Net::Telnet. The following should connect, grab all available lines of data and print them to STDOUT before it exits. I say should because I have no way of testing it. You may need to uncomment and modify the regex in one of the commented out lines. Be sure to change the path in the Dump_log line and use tail -f to see what's going on.

    #!/usr/bin/env perl use Net::Telnet (); my $telnet = new Net::Telnet( Timeout => 10, # Prompt => '/bash\$ $/', # Change me Errmode => 'die', Dump_log => '/path/to/dump.log', # change the path to a valid one ); $telnet->open('192.168.1.88'); #$telnet->waitfor('/bash\$ $/'); # Change me my @lines = $telnet->getlines(); $telnet->close(); print @lines; exit;