Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

cannot receive the timeout message when telnet fails, WHY?

Posted on 2007-08-01
10
Medium Priority
?
429 Views
Last Modified: 2008-02-01
i use a normal telnet script like that:
sub blabla {
$telnet = new Net::Telnet (
                                Host => "$host",
                                Errmode=> "return",
                                Prompt => "/$node>/");
if (!$telnet) {$alarm .= "Cannot telnet to $node, ";return;}# exit the sub if can not telnet
}

i telnet to 15 unix host, one after onother, but one of them (named A) i cannot telnet to (e.g when it is turned off)
but i can not get the timeout error to escape, the script is hang and when i check on my local host:
netstat -an|grep <ip of host A>
i get:
<IP of my localhost>.40621   <IP of host A>.23         0      0 49640      0 SYN_SENT
which mean the script cannot receive the error message which indicate timeout for me to go further
if i turned of another host, it works fine after 10 seconds trying
what's happening? and what to do to deal with SYN_SENT ??



0
Comment
Question by:nanoterabeta
  • 6
  • 4
10 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 19610447
Not sure why telnet isn't returning.  Are you using a hostname for $host, or an IP address?

You could try something like this to get around the telnet not returning problem:
eval {
    $SIG{ALRM}=sub{die "Timeout\n";};
    alarm(10);
    $telnet = new Net::Telnet(Host => $host, Errmode=>'return', Prompt=>"/$node>/");
};
if (!$telnet) {$alarm .= "Cannot telnet to $node, ";return;}# exit the sub if can not telnet
0
 

Author Comment

by:nanoterabeta
ID: 19610695
i assign an ip address to $host in some commands before hand, but i don't think it matters
where can i put your script to in my script? and what does it mean?
0
 

Author Comment

by:nanoterabeta
ID: 19610888
sorry Adam314, i tried but nothing changes, i need something that is able to trap the error from the telnet
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Expert Comment

by:Adam314
ID: 19611076
I can't seem to replicate the problem so I can't test this.  But you would change your code like so:

sub blabla {
   
    #The eval allows something inside to die without killing your program
    eval {
        #This tells what to do when an alarm is made
        $SIG{ALRM}=sub{die "Timeout\n";};
       
        #This will create an alarm in 10 seconds, running the above subroutine
        alarm(10);
       
        #Try to create your telnet object as normal
        $telnet = new Net::Telnet (
                                    Host => "$host",
                                    Errmode=> "return",
                                    Prompt => "/$node>/");
    };
    #The alarm will have caused the eval block to die even if the telnet doesn't return
    #so you will be here in 10 seconds either way
   
    #if there is no telnet object, the connect failed
    if (!$telnet) {$alarm .= "Cannot telnet to $node, ";return;}# exit the sub if can not telnet
}
0
 

Author Comment

by:nanoterabeta
ID: 19611531
here is what i truss the script, i make it loop to telnet to host A that is unreachable:
the first loop is ok:
.....
time()                                          = 1185992266
write(3, " : : D o   n o t h i n g".., 34)      = 34
time()                                          = 1185992266
alarm(0)                                        = 0
sigaction(SIGALRM, 0xFFBFF990, 0xFFBFFA40)      = 0
sigfillset(0xFF042940)                          = 0
sigprocmask(SIG_BLOCK, 0xFFBFFA30, 0xFFBFFA20)  = 0
alarm(168)                                      = 0            
sigsuspend(0xFFBFFA10)          (sleeping...)                   #here is script sleep 168 second until next loop
    Received signal #14, SIGALRM, in sigsuspend() [caught]
sigsuspend(0xFFBFFA10)                          Err#4 EINTR
setcontext(0xFFBFF6F8)
alarm(0)                                        = 0
sigaction(SIGALRM, 0xFFBFF990, 0x00000000)      = 0
time()                                          = 1185992434
time()                                          = 1185992434
sigaction(SIGALRM, 0xFFBFF8F8, 0xFFBFF978)      = 0
alarm(9)                                        = 0
sigaction(SIGALRM, 0xFFBFF890, 0xFFBFF910)      = 0
sigaction(SIGALRM, 0xFFBFF8F8, 0xFFBFF978)      = 0
alarm(0)                                        = 9
sigaction(SIGALRM, 0xFFBFF898, 0xFFBFF918)      = 0
sigaction(SIGALRM, 0xFFBFF890, 0xFFBFF910)      = 0
sigaction(SIGALRM, 0xFFBFF8F8, 0xFFBFF978)      = 0
alarm(11)                                       = 0
so_socket(PF_INET, SOCK_STREAM, IPPROTO_IP, "", 1) = 4
fcntl(4, F_SETFD, 0x00000001)                   = 0
connect(4, 0x003D6188, 16, 1)   (sleeping...)      #this sleep is very long, approx 4 minutes

#but the second one, it seems like it still connect to host
with the state SYN_SENT
0
 

Author Comment

by:nanoterabeta
ID: 19611943
do i need to use any particular perl module for SIG{ALRM} ?
0
 

Author Comment

by:nanoterabeta
ID: 19612093
i think $telnet is not equal 0 in the first 10 seconds so we cannot trap the error
0
 
LVL 39

Expert Comment

by:Adam314
ID: 19612196
The code i posted would be used in your "blabla" subroutine.
You don't need anything special to use the code I gave.  
I'm not sure what you are showing in your post ID 19611531.
0
 

Author Comment

by:nanoterabeta
ID: 19622376
in my post ID 19611531 is output from truss utility in Unix, when i type the command:
truss -p <Pid>  # Pid is process id of my script
as you can see in the output i extract from above:

...
time()                                          = 1185992434
sigaction(SIGALRM, 0xFFBFF8F8, 0xFFBFF978)      = 0
alarm(9)                                        = 0                            #this is when line alarm (9); in perl script works
sigaction(SIGALRM, 0xFFBFF890, 0xFFBFF910)      = 0
sigaction(SIGALRM, 0xFFBFF8F8, 0xFFBFF978)      = 0
alarm(0)                                        = 9                            # and this is when it runs out of time (9 seconds)
sigaction(SIGALRM, 0xFFBFF898, 0xFFBFF918)      = 0
...

instead of 10 seconds in your suggestion, i type 9 (for it to be different from default timeout value)
so we don't seem to see the following work:
eval {
        #This tells what to do when an alarm is made
        $SIG{ALRM}=sub{die "Timeout\n";};
       #This will create an alarm in 10 seconds, running the above subroutine
        alarm(9);
        #Try to create your telnet object as normal
        $telnet = new Net::Telnet (
                                    Host => "$host",
                                    Errmode=> "return",
                                    Prompt => "/$node>/");
    };
    #The alarm will have caused the eval block to die even if the telnet doesn't return
    #so you will be here in 10 seconds either way


instead of that, the process runs into a long sleep time:
alarm(11)                                       = 0
so_socket(PF_INET, SOCK_STREAM, IPPROTO_IP, "", 1) = 4
fcntl(4, F_SETFD, 0x00000001)                   = 0
connect(4, 0x003D6188, 16, 1)   (sleeping...)      #this sleep is very long, approx 4 minutes
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 19625694
I'm not sure what is causing the problem.  The Net::Telnet module might internally use the alarm to control it's timeouts - which would prevent the alarm before it from working...
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
How to fix a SonicWall Gateway Anti-Virus firewall blocking automatic updates to apps like Windows, Adobe, Symantec, etc.
Six Sigma Control Plans
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question