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

I have a piece of hardware that I need to connect to on port tcp 23. That device sends one line messages randomly. In order for the device to not break the connection, I must send a 'HELLO' every 60 seconds. I have tried the telnet module and socket comms, but inevitably, I can't seem to 'break out' to send 'HELLO' while I am blocking waiting for random output. If you close the handle on timeout or leave the handle on subroutine you lose the handle and can't send anything. This problem is kicking my azz. Any ideas ? Thanks

Replies are listed 'Best First'.
Re: Advice on perl socket activity
by gmargo (Hermit) on Nov 06, 2009 at 01:25 UTC

    This is the exact same question you asked before in thread While loop - telnet module question. What did you do with the advice I gave you then? Where is the code that you've written since?

      I worked with getline timeout parameters for about 4 hours. Once the timeout occurs the handle is dropped. You can reestablish the handle to send the 'hello' but then you can miss output (verified) while the handle is 'bounced'. I appreciate your input but it isn't behaving the way I need it. Also you have to set errmode in order for the getline timeout not to make the whole script die. The doc on how to that is not very clear.

        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$ "

Re: Advice on perl socket activity
by zwon (Abbot) on Nov 05, 2009 at 23:50 UTC
Re: Advice on perl socket activity
by Illuminatus (Curate) on Nov 06, 2009 at 00:27 UTC
    Just to expand. Just open the socket using IO::Socket in non-blocking mode, and use select. select can take a timeout, so you can ensure you write your HELLO message at the necessary interval