I had no trouble getting the timeout to work properly. The connection does not get dropped. Either get() or getline() will work but I settled on get().

#!/usr/bin/perl use strict; use warnings; use diagnostics; use IO::Handle; use Net::Telnet; my ($host, $username, $passwd) = ("localhost", "issac", "YYYYY"); autoflush STDOUT 1; my $t = Net::Telnet->new(); # Log into unix box print localtime().": Connect to $host..."; my $ok = $t->open(Host => $host, Timeout => 20, Errmode => "return"); die ("Cannot connect to $host telnet server") if !$ok; print "OK\n"; print localtime().": Login to $host..."; $ok = $t->login(Name => $username, Password => $passwd, Timeout => 20, Errmode => "return"); die ("Cannot login to $host telnet server") if !$ok; print "OK\n"; # Read from server continually. # Periodically send text to server. while (1) { my $output = $t->get(Timeout => 10, Errmode => "return"); if (defined $output) { # Recieved text from server print localtime().": get() returned definite output=\"$output\ +"\n"; } elsif ($t->eof()) { # End of file - server has hung up. print localtime().": get() returned EOF\n"; last; } else { # Timeout occured print localtime().": get() returned TIMEOUT\n"; print localtime().": put() sending string\n"; # Send some crap to the server my $ok = $t->put(String => "echo HELLO\n", Timeout => 10, Errmode => "return"); if (!$ok) { if ($t->eof()) { print localtime().": put() returned EOF\n"; last; } else { print localtime().": put() returned TIMEOUT\n"; } } } }

Here is some sample output:

gmargo@tesla 1753$ perl telnet_test.pl Thu Nov 5 19:41:00 2009: Connect to localhost...OK Thu Nov 5 19:41:00 2009: Login to localhost...OK Thu Nov 5 19:41:11 2009: get() returned TIMEOUT Thu Nov 5 19:41:11 2009: put() sending string Thu Nov 5 19:41:11 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:11 2009: get() returned definite output="HELLO issac@tesla 79$ " Thu Nov 5 19:41:22 2009: get() returned TIMEOUT Thu Nov 5 19:41:22 2009: put() sending string Thu Nov 5 19:41:22 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:22 2009: get() returned definite output="HELLO issac@tesla 80$ " Thu Nov 5 19:41:33 2009: get() returned TIMEOUT Thu Nov 5 19:41:33 2009: put() sending string Thu Nov 5 19:41:33 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:33 2009: get() returned definite output="HELLO issac@tesla 81$ " Thu Nov 5 19:41:44 2009: get() returned TIMEOUT Thu Nov 5 19:41:44 2009: put() sending string Thu Nov 5 19:41:44 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:44 2009: get() returned definite output="HELLO issac@tesla 82$ "


In reply to Re^3: Advice on perl socket activity by gmargo
in thread Advice on perl socket activity by sans-clue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.