Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Alarm and Telnet issues

by pubscout (Initiate)
on Dec 27, 2006 at 16:38 UTC ( [id://591881]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Monks,
I am attempting to script an automated telnet session that has an overall timeout and will also handle reconnect attempts for dropped sessions.

I'm using an alarm for the global timeout control, which by itself works as expected, but as soon as I add a few Net::Telnet object calls the alarm exception does not seem to propagate.

eval { local $SIG{ALRM} = sub (die "timeout\n"); alarm 5; telnet_connect: #label for later handling of dropped sessions my $t = new Net::Telnet(); # Even with the new() line above the alarm fires # Adding the lines below stop the alarm from working $t->open($host) $t->login($user, $pass); while (1) { # Interact w/telnet session until we're happy # This loop is always reached in testing } $t->close() alarm 0; }; if ($@) { if ($@ =~ /timeout/) { print "Timed out!"; } else { print "Received exception: $@\n"; alarm 0; die; } }
My next intention is to handle dropped connections by receiving Net::Telnet specific exceptions and returning to the telnet_connect label to reconnect and retry, all within the same timeout eval scope.

The first question is why does the alarm fail to trigger with the addition of the Net::Telnet object, even when setting PERL_SIGNALS=unsafe ?

Secondly, is this the best way to handle reconnecting to dropped telnet sessions while being governed by a global timeout?

Thanks in advance for any glimpse of enlightenment!

Replies are listed 'Best First'.
Re: Alarm and Telnet issues
by themage (Friar) on Dec 27, 2006 at 16:59 UTC
    Hi pubscout,

    Your code probably is not working because Net::Telnet uses internly alarm and $SIG{ALRM} to handler timeouts itself.

    You should use the Net::Telnet timeout instead.

      Bummer. I guess that means that there is no trivial way to have a global timeout when using Net::Telnet.

      Thanks for the help!

        Well, no lightweight trivial way.
        use POSIX (); $SIG{USR1} = sub { ... }; unless(fork) { sleep(4); kill(POSIX::SIGUSR1(), getppid()); exit; }
Re: Alarm and Telnet issues
by EvanK (Chaplain) on Dec 27, 2006 at 16:48 UTC
    anonymous subroutines use curly braces, so the sub you're assigning to $SIG{ALRM} should be:
    #curly braces instead of parentheses local $SIG{ALRM} = sub {die "timeout\n"};
    not sure if thats causing all the problems, but i hope it helps

    __________
    The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
    - Terry Pratchett

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://591881]
Approved by chargrill
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found