Problem checking network connection integrity with Net::Telnet 
Author Message
 Problem checking network connection integrity with Net::Telnet

Hi NG,

I've got about 100 Cisco routers each connected to up to 16 serial
devices.
I can talk to these devices via telnetting to the Router port like
telnet 192.168.100.1 2033.
What I would like to do is to run every hour a sanity check of these
connections.




my $conn = new Net::Telnet( Telnetmode => 0, Timeout => 1, );
foreach (0..$#host)
{
  $conn->errmode( sub { $status[$_] = "red"; print
localtime(time)."\t".$conn->errmsg."\n"; } )
  $status[$_]="green";
  $conn->open( Host => $host[$_], Port => $port[$_], );
  $conn->close;

Quote:
}

undef $conn;

results to "problem connecting to "ip address", port port_number:
Unknown error
for each element after a 45 (???) sec timeout. All elements were
unreachable, in fact.




foreach (0..$#host)
{
  my $conn = IO::Socket::INET->new( Proto => "tcp", PeerAddr =>
$host[$_], PeerPort => $port[$_], );
  $conn->errmode( sub { $status[$_] = "red"; print
localtime(time)."\t".$conn->errmsg."\n"; } )
  $status[$_]="green";
  close $conn;

Quote:
}

undef $conn;

aborts with a "can't call method "errmode" on an undefined value" error
message.

I only need to trace the fact of a successful or unsuccessful connection
setup.

What advice would you give?

TIA and best regards,

Zoltan Kandi, M. Sc.



Thu, 12 Aug 2004 23:48:43 GMT  
 Problem checking network connection integrity with Net::Telnet
Just some additions to my previous posting:

Quote:

> Hi NG,

> I've got about 100 Cisco routers each connected to up to 16 serial
> devices.
> I can talk to these devices via telnetting to the Router port like
> telnet 192.168.100.1 2033.
> What I would like to do is to run every hour a sanity check of these
> connections.




> my $conn = new Net::Telnet( Telnetmode => 0, Timeout => 1, );
> foreach (0..$#host)
> {
>   $conn->errmode( sub { $status[$_] = "red"; print
> localtime(time)."\t".$conn->errmsg."\n"; } )
>   $status[$_]="green";
>   $conn->open( Host => $host[$_], Port => $port[$_], );
>   $conn->close;
> }
> undef $conn;

> results to "problem connecting to "ip address", port port_number:
> Unknown error
> for each element after a 45 (???) sec timeout. All elements were
> unreachable, in fact.




> foreach (0..$#host)
> {
>   my $conn = IO::Socket::INET->new( Proto => "tcp", PeerAddr =>
> $host[$_], PeerPort => $port[$_], );
>   $conn->errmode( sub { $status[$_] = "red"; print
> localtime(time)."\t".$conn->errmsg."\n"; } )
>   $status[$_]="green";
>   close $conn;
> }
> undef $conn;

> aborts with a "can't call method "errmode" on an undefined value" error
> message.

> I only need to trace the fact of a successful or unsuccessful connection
> setup.

> What advice would you give?

> TIA and best regards,

> Zoltan Kandi, M. Sc.


The following code segment




 my $conn = new Net::Telnet( Telnetmode => 0, Timeout => 1, );
 local SIG{'__DIE__'} = sub { $status[$_] = "red"; print
gethumantime()."\t".$conn->errmsg."\n"; } ;
 foreach (0..$#host)
 {
   $status[$_]="green";
   $conn->open( Host => $host[$_], Port => $port[$_], ) || die;
   $conn->close;
 }
 undef $conn;

 triggers the following message on my NT box at the very first iteration
of the foreach loop:

2002-02-25 16:44:20   problem connecting to "192.168.13.0", port 2033:
Unknown error
The Unsupported function alarm function is unimplemented at
C:/Perl/site/lib/Net/Telnet.pm line 2024.

Telnet.pm looks like:

2020 sub _have_alarm {
2021     eval {
2022         local $SIG{'__DIE__'} = 'DEFAULT';
2023         local $SIG{ALRM} = sub { die };
2024         alarm 0;
2025     };

My install is ActivePerl build 629, Net::Telnet build 3.02.

I'd appreciate any hints in how to solve this tiny problem.

TIA and best regards,

Zoltan Kandi, M. Sc.
Product & Application Specialist

Tellabs Netherlands BV
Perkinsbaan 17
3439 ND Nieuwegein

Tel:      +31 30 600 40 75
Fax:      +31 30 600 40 90
GSM:      +31 651 194 291

Internet: http://www.tellabs.com



Fri, 13 Aug 2004 16:59:14 GMT  
 Problem checking network connection integrity with Net::Telnet

Quote:

> Just some additions to my previous posting:




> > foreach (0..$#host)
> > {
> >   my $conn = IO::Socket::INET->new( Proto => "tcp", PeerAddr =>
> > $host[$_], PeerPort => $port[$_], );
> >   $conn->errmode( sub { $status[$_] = "red"; print
> > localtime(time)."\t".$conn->errmsg."\n"; } )
> >   $status[$_]="green";
> >   close $conn;
> > }
> > undef $conn;

> > aborts with a "can't call method "errmode" on an undefined value" error
> > message.

> > I only need to trace the fact of a successful or unsuccessful connection
> > setup.

> > What advice would you give?

> > TIA and best regards,

> > Zoltan Kandi, M. Sc.


Problem solved, attached is the solution, maybe someone can profit from
it.
Key to the solution: localize %SIG and wrap the socket creation
statement in an eval block otherwise die will really die.
Comments are more than welcome, spare the newbie, though.

BRGDS,

Zoltan Kandi, M. Sc.
Product & Application Specialist

Tellabs Netherlands BV
Perkinsbaan 17
3439 ND Nieuwegein

Tel:      +31 30 600 40 75
Fax:      +31 30 600 40 90
GSM:      +31 651 194 291

Internet: http://www.tellabs.com

--- code begins ---

#!perl -w

use strict;
use IO::Socket;

local $SIG{'__DIE__'} = sub {print "not reachable!\n";};
my $sock;

foreach (7070..7080)
{
    print "port $_ to be checked, ";
    eval
    {
         $sock = new IO::Socket::INET ( PeerAddr => 'localhost',
                                        PeerPort => $_,
                                        Proto => 'tcp',
                                        Timeout => 1,
                                      ) || die;
    };
    if (defined $sock)
    {
         print "reachable!\n";
         close($sock);
    }

Quote:
}

0;

--- code ends ---



Sun, 15 Aug 2004 17:21:30 GMT  
 Problem checking network connection integrity with Net::Telnet

[snippage]

Quote:
> Problem solved, attached is the solution, maybe someone can profit from
> it.
> Key to the solution: localize %SIG and wrap the socket creation
> statement in an eval block otherwise die will really die.
> Comments are more than welcome, spare the newbie, though.

> BRGDS,

[more snippage]

Quote:
> --- code begins ---

> #!perl -w

> use strict;
> use IO::Socket;

> local $SIG{'__DIE__'} = sub {print "not reachable!\n";};
> my $sock;

> foreach (7070..7080)
> {
>     print "port $_ to be checked, ";
>     eval
>     {
>          $sock = new IO::Socket::INET ( PeerAddr => 'localhost',
>                                         PeerPort => $_,
>                                         Proto => 'tcp',
>                                         Timeout => 1,
>                                       ) || die;
>     };
>     if (defined $sock)
>     {
>          print "reachable!\n";
>          close($sock);
>     }
> }

> 0;

Localizing on file level shouldn't make any difference (and doesn't
in my tests).

Otherwise, if you don't want to die when socket creation fails,
then don't do so.  To me, your code looks like a roundabout way
checking whether port creation succeeds.  To the same effect,
simply do

    for ( 7070..7080 ) {
        my $sock = new IO::Socket::INET ( PeerAddr => 'localhost',
                                          PeerPort => $_,
                                          Proto => 'tcp',
                                          Timeout => 1,
                                         );
        if ( defined $sock ) {
            print "port $_ is reachable\n";
            close $sock;
        } else {
            print "port $_ is not reachable\n"
        }
    }

Anno



Sun, 15 Aug 2004 17:42:31 GMT  
 Problem checking network connection integrity with Net::Telnet

Quote:

[thread snipped]

> Localizing on file level shouldn't make any difference (and doesn't
> in my tests).

> Otherwise, if you don't want to die when socket creation fails,
> then don't do so.  To me, your code looks like a roundabout way
> checking whether port creation succeeds.  To the same effect,
> simply do

>     for ( 7070..7080 ) {
>         my $sock = new IO::Socket::INET ( PeerAddr => 'localhost',
>                                           PeerPort => $_,
>                                           Proto => 'tcp',
>                                           Timeout => 1,
>                                          );
>         if ( defined $sock ) {
>             print "port $_ is reachable\n";
>             close $sock;
>         } else {
>             print "port $_ is not reachable\n"
>         }
>     }

> Anno

Hi Anno,

Thanks for your valuable comments.
Another question I'm facing right now as my script runs: why does it
time out on certain address-port combinations after 1 sec and on others
after 45 secs? Does it really need 45 secs to try to create an IP
socket? I've searched in the IO::Socket pm on 45 (thought maybe it was a
predefined timeout interval for a specific fault scenario) but gone not
too far. The socket manpage did not give any hints either.

Best regards,

Zoltan Kandi, M. Sc.
Product & Application Specialist

Tellabs Netherlands BV
Perkinsbaan 17
3439 ND Nieuwegein

Tel:      +31 30 600 40 75
Fax:      +31 30 600 40 90
GSM:      +31 651 194 291

Internet: http://www.tellabs.com



Mon, 16 Aug 2004 14:40:02 GMT  
 Problem checking network connection integrity with Net::Telnet

Quote:

> [thread snipped]

> > Localizing on file level shouldn't make any difference (and doesn't
> > in my tests).

> > Otherwise, if you don't want to die when socket creation fails,
> > then don't do so.  To me, your code looks like a roundabout way
> > checking whether port creation succeeds.  To the same effect,
> > simply do

> >     for ( 7070..7080 ) {
> >         my $sock = new IO::Socket::INET ( PeerAddr => 'localhost',
> >                                           PeerPort => $_,
> >                                           Proto => 'tcp',
> >                                           Timeout => 1,
> >                                          );
> >         if ( defined $sock ) {
> >             print "port $_ is reachable\n";
> >             close $sock;
> >         } else {
> >             print "port $_ is not reachable\n"
> >         }
> >     }

> > Anno

> Hi Anno,

> Thanks for your valuable comments.
> Another question I'm facing right now as my script runs: why does it
> time out on certain address-port combinations after 1 sec and on others
> after 45 secs? Does it really need 45 secs to try to create an IP
> socket? I've searched in the IO::Socket pm on 45 (thought maybe it was a
> predefined timeout interval for a specific fault scenario) but gone not
> too far. The socket manpage did not give any hints either.

I'm not surprised at all that you get varying behavior between hosts.
An IP connection is complex, and more than one timeout may be involved
on both the local and the remote machine.  Depending on what goes wrong
one or another may have to catch it.

Anno



Mon, 16 Aug 2004 15:21:14 GMT  
 Problem checking network connection integrity with Net::Telnet

Quote:


> [thread snipped]

> > Localizing on file level shouldn't make any difference (and doesn't
> > in my tests).

> > Otherwise, if you don't want to die when socket creation fails,
> > then don't do so.  To me, your code looks like a roundabout way
> > checking whether port creation succeeds.  To the same effect,
> > simply do

> >     for ( 7070..7080 ) {
> >         my $sock = new IO::Socket::INET ( PeerAddr => 'localhost',
> >                                           PeerPort => $_,
> >                                           Proto => 'tcp',
> >                                           Timeout => 1,
> >                                          );
> >         if ( defined $sock ) {
> >             print "port $_ is reachable\n";
> >             close $sock;
> >         } else {
> >             print "port $_ is not reachable\n"
> >         }
> >     }

> > Anno

> Hi Anno,

> Thanks for your valuable comments.
> Another question I'm facing right now as my script runs: why does it
> time out on certain address-port combinations after 1 sec and on
> others after 45 secs?

Because the Timeout item is either not supported or incorrectly
implemented in your Perl.

Quote:
> Does it really need 45 secs to try to create an IP socket?

Creating the socket is near instantaneous, it's the attempt to connect
which takes so long.

As a workaround for the Timeout parameter not being supported, try:

    my $localhost = inet_aton("localhost");
    for ( 7070..7080 ) {
        my $sock = IO::Socket::INET->new(Proto=>'tcp')
            or die "socket: $!";
        defined($sock->blocking(0)) or die "nonblocking: $!";
        $sock->connect(sockaddr_in($_,$localhost)
            or $!{EWOULDBLOCK} || $!{EINPROGRESS}
            or die "connect: $!";
        if( () = IO::Select->new($sock)->can_write(1) ) {
            print "port $_ is reachable\n";
        } else {
            print "port $_ is unreachable\n";
        }
    }

Sadly, ->blocking doesn't work quite right on windows.  As a workaround
for *that*, you would change 'defined(...->blocking(0)) or die...' to:

   defined($server->blocking(0)) or
      $^O eq 'MSWin32' and do {
         my $set_it = "1";
         my $ioctl_val = 0x80000000 | (4 << 16) | (ord('f') << 8) | 126;
         ioctl ($socket, $ioctl_val, $set_it);
      } or die "Couldn't set nonblocking: $!";

You can speed up your testing quite a bit by doing your connects in
parallel:

    my $localhost = inet_aton("localhost");
    my $sockets = IO::Select->new;
    my (%fail, %sockets);
    for ( 7070..7080 ) {
        my $sock = IO::Socket::INET->new(Proto=>'tcp')
            or die "socket: $!";
        defined($sock->blocking(0)) or die "nonblocking: $!";
        (my $ok = $sock->connect(sockaddr_in($_,$localhost))
            or $!{EWOULDBLOCK} || $!{EINPROGRESS}
            or ($fail{$_} = $!, next);
        if( $ok ) {
            $fail{$_} = 0;
        } else {
            $sel->add($sock); $sockets{$sock} = $_;
        }
    }




    }
    undef $sel; %sockets = ();
    for ( 7070..7080 ) {
        if(( $! = $fail{$_} )) {
            print "port $_ is unreachable: $!\n";
        } elsif( !exists $fail{$_} ) {
            print "port $_ is unreachable [timed out]\n";
        } else {
            print "port $_ is reachable\n";
        }
    }

[If you're on Win32, add to the ->blocking(0) the workaround I
suggested.  I only ommited it here for improved readability.]

Caveat Lector: All this code is off the top of my head, and untested.
Well, except the Win32 workaround for nonblocking, which I've used to
good effect in another program and I cut&pasted to here.

--
print reverse( ",rekcah", " lreP", " rehtona", " tsuJ" )."\n";



Tue, 17 Aug 2004 04:58:43 GMT  
 Problem checking network connection integrity with Net::Telnet
Olena Kandi scripsit:

Quote:
> Hi NG,

> I've got about 100 Cisco routers each connected to up to 16 serial
> devices.
> I can talk to these devices via telnetting to the Router port like
> telnet 192.168.100.1 2033.
> What I would like to do is to run every hour a sanity check of these
> connections.
>...
> I only need to trace the fact of a successful or unsuccessful connection
> setup.

> What advice would you give?

> TIA and best regards,

hi, you can try this (untested)

use IO::Socket::INET;
use strict;
use warnings;

             {
               ip => <ipnumber>,
               port => <portnumber>,
               status => 'green'
             },
             ...
            );


   if (my $sock = IO::Socket::INET->new(Proto => "tcp",
                                        PeerAddr =>$host->{ip},
                                        PeerPort => $host->{port})) {
         $sock->close();
         $host->{status} = 'green';
    }
    else {
      $host->{status} = 'red';
    }

Quote:
}

--
Leo "TheHobbit"
remove nospamme from mail address to reply


Fri, 20 Aug 2004 18:10:21 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Net::Telnet connection timed out error

2. Net::SMTP : check connection

3. Net::Telnet-ing problem to Solaris telnet server

4. Resource Fork Integrity Checks

5. checking if file exists in Net::Telnet

6. Vax username/passwd check via Net::Telnet

7. "Vax login check using Net::Telnet"

8. Telnet.pm and Bay Networks environment problem

9. Problem reading a telnet connection with sysread/read

10. Telnet.pm Connection Problems???

11. Can't Net::FTP - 425 Can't build data connection: Connection refused

12. Net::IRC connection problems: possible fix

 

 
Powered by phpBB® Forum Software