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

Hi. I'm trying to use Net::Telnet for multiple telnet sessions one after another. When the open($host) method fails (timeout) at the first time it will work ok. But the second or third time doesnt work!! If I run this with IP-range containing nonallocated addresses, after one timeout the next timeout will not happen at all. Is this related to SIGALARM if yes, how? Here is a little snipp:
#!/usr/bin/perl -w use strict; use Net::Telnet(); # ... # ... sub checkVersion { my $host = shift; my $telnet = new Net::Telnet(Timeout => 10, Prompt => '/-> $/', Errmode => "return"); if ( not $telnet->open($host) ){ # Telnet timeout for destination print "Telnet failed for address $host.\n"; $telnet->close; return 0; } # ... } for ( my $i = $ip_range_start; $i <= $ip_range_stop; $i++) { $host = "$ip_c_base.$i"; checkVersion($host); }

Replies are listed 'Best First'.
Re: Net::Telnet timeout fails
by Roger (Parson) on Oct 27, 2003 at 00:36 UTC
    Have you checked the return error message from Net::Telnet? You can check the error message with the following code:

    printf "%s\n", $telnet->errmsg();
    Which version of Net::Telnet are you using? The version I am using is 3.03. The following is an extract from the module -

    ## Connect to server, timing out if it takes too long. eval { ## Turn on timer. local $SIG{"__DIE__"} = "DEFAULT"; local $SIG{ALRM} = sub { die "timed-out\n" }; alarm $timeout; ## Lookup server's IP address. $ip_addr = inet_aton $host or die "unknown remote host: $host\n"; ## Create a socket and attach the filehandle to it. socket $self, AF_INET, SOCK_STREAM, 0 or die "problem creating socket: $!\n"; ## Open connection to server. connect $self, sockaddr_in($port, $ip_addr) or die "problem connecting to \"$host\", port $port: $!\n"; }; alarm 0;
    The module code will try to resolve the host IP before making a connection with inet_aton, create socket, and connect to the specified server.

    Now that explains why the call to open does not wait for 10 seconds on invalid addressed, because as soon as the host resolution fails, the evalutation block will terminate (with alarm trigger reset) at the line die "unknown remote host: $host\n";.

    I suspect this is the cause for your problem. Check the $telnet->errmsg() nevertheless to see what's the reason for the failure.

Re: Net::Telnet timeout fails
by Anonymous Monk on Oct 27, 2003 at 08:09 UTC
    I found out that when I'm using ActiveState Perl v5.6.1 for Solaris and Net::Telnet v.3.03, the timeout only works once. The IP-address is OK, but there is no host connected. First time it timeouts OK, but the second address hangs much longer than 10s timeout I've set.

    If the first address gives me the errmsg:

    "problem connecting to "xxx.xxx.xxx.xxx", port 23: connect timed-out"

    and the second address should be similar, the open()-method will hang about 3 minutes and 44 seconds! I think this is some system level timeout for socket connections...

    But when I'm using ActiveState Perl v5.8.0 and Net::Telnet v.3.03, it WORKS!!! Is this a known issue with 5.6.1?