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

In my quest for wisdom I’am looking for some advice regarding this script.
I intend to use this script to connect around 1200 routers (once a month) and test if the backup lines are operational.
The problem is within the for loop, where the connection just dies.
I need the loop because there is a delay between the command to activate the bri’s and the connection to be established.
It’s totally random; sometimes I’am able to go in the loop twice or three times, sometimes it dies on the first iteration.
I tried to change the timeouts in the commands without success.
Here are samples with debug turned on
r0658 iteraction: 0 lines in output(show isdn active): 9 Open Cal +ls: 2 of 3 r0658 iteraction: 1 - rit0658;Cmd2;Error r0037 iteraction: 0 lines in output(show isdn active): 7 Open Cal +ls: 0 of 3 r0037 iteraction: 1 - rit0037;Cmd2;Error r1057 iteraction: 0 lines in output(show isdn active): 7 Open Cal +ls: 0 of 3 r1057 iteraction: 1 lines in output(show isdn active): 10 Open Cal +ls: 3 of 3 + r1057;3;3 [3] sites... r0658 iteraction: 0 lines in output(show isdn active): 9 Open Cal +ls: 2 of 3 r0658 iteraction: 1 - rit0658;Cmd2;Error r0037 iteraction: 0 lines in output(show isdn active): 7 Open Cal +ls: 0 of 3 r0037 iteraction: 1 lines in output(show isdn active): 10 Open Cal +ls: 3 of 3 + r0037;3;3 r1057 iteraction: 0 lines in output(show isdn active): 7 Open Cal +ls: 0 of 3 r1057 iteraction: 1 lines in output(show isdn active): 10 Open Cal +ls: 3 of 3 + r1057;3;3 [3] sites...
Please ANY comment is welcomed :)
1 #!/usr/bin/perl 2 # perl v5.6.1 3 4 use strict; 5 use warnings; 6 use IO::File; 7 use Net::Telnet::Cisco; #v1.10 8 use constant DEBUG => 1; 9 10 # testBris.pl - test isdn backups on cisco routers - Marcelo Corr +ea <mvcorrea[at]gmail[dot]com> 11 # input: csv file with routers info "see line format above" 12 # output: csv file with the input format with failed routers (to +re-run) 13 # csv file with full output ("$routerName;$callsOK;$numCa +lls") 14 15 #file format "$routerIp;$routerName;$dialerIp;$dialerNumber;$numC +alls" 16 #open(FILE,"hosts.with.dialer.csv") or die "> unable to open $_ f +ile: $!\n"; 17 open(FILE,"hosts.with.dialer.tmp.csv") or die "> unable to open $ +_ file: $!\n"; 18 my @hosts = <FILE>; # put the file content in the array 19 close(FILE); 20 21 my $hostserrors = new IO::File "hosts.errors.csv", "w"; 22 die "Unable to create File \"$hostserrors\": $!\n" unless $hostse +rrors; 23 24 my $outfile = new IO::File "test.rdis.full.csv", "w"; 25 die "Unable to create File \"$outfile\": $!\n" unless $outfile; 26 27 my $output = ""; # returned cvs line 28 my (@fullarray,@errorarray) = (); # arrays to file 29 my $sitecount = 0; # site counter 30 push(@fullarray, "Site;#;Tot\n"); # cvs header 31 32 sub routerConnect { 33 $sitecount++; 34 my ($ip, $name, $dialer, $phone, $numCalls) = @_; 35 my $log = new IO::File "logBK.log", "a"; # full log 36 37 my $session = Net::Telnet::Cisco->new( Host => "$ip", 38 Input_log => $log, 39 Timeout => "20", 40 Errmode => "return"); 41 42 unless($session) { 43 $output = $name.";Session;Error\n"; 44 return $output; 45 } 46 47 unless($session->login(Name =>"Superuser", Password => "secret" +, Timeout => "10")) { 48 $output = $name.";Login;Error\n"; 49 return $output; 50 } 51 52 unless($session->enable("SuperSecret")) { 53 $output = $name.";Enable;Error\n"; 54 return $output; 55 } 56 57 $session->waitfor_pause(0.6); 58 $session->always_waitfor_prompt; 59 $session->cmd("terminal length 0"); 60 $session->cmd("clear interface dialer0"); 61 62 my $iterations= 6; 63 64 for (my $i=0; $i <= $iterations; $i++) { # wait until all conn +ections are up 65 print $name . "\titeraction: ".$i."\t" if(DEBUG); 66 my ($test1, $test2) = ""; 67 68 $test1 = $session->cmd( String => "isdn test call interface B +RI 0/2/0 $phone number-of-calls 2", 69 Timeout => "15"); 70 $test2 = $session->cmd( String => "isdn test call interface B +RI 0/3/0 $phone number-of-calls 2", 71 Timeout => "15"); 72 73 if ($test1 && $test2){ # if the above commands + return OK 74 sleep 1; 75 my $numOpenCalls = 0; 76 my @connets = (); 77 @connets = $session->cmd( String => "show isdn active", 78 Timeout => "15"); 79 80 if($#connets ne "-1"){ # if the output array c +ontains data 81 82 print "lines in output(show isdn active): ".$#connets."\t +" if(DEBUG); 83 84 foreach my $myline (@connets) { # count number of conne +ctions 85 if($myline=~ /^Out/){ $numOpenCalls++; } 86 } 87 88 print "Open Calls: ".$numOpenCalls." of ".$numCalls."\n" +if(DEBUG); 89 90 $output = $name.";".$numOpenCalls.";".$numCalls."\n"; 91 92 if($numOpenCalls != $numCalls || $numCalls == 0){ 93 # print "."; 94 if ($i == $iterations){ 95 $session->cmd("clear interface dialer0"); 96 last; 97 } 98 } else { 99 $session->cmd("clear interface dialer0"); 100 last; 101 } 102 } else { $output= $name.";Cmd2;Error\n"; return $output; } 103 } else { $output= $name.";Cmd1;Error\n"; return $output; } 104 } 105 $session->cmd("clear interface dialer0"); 106 $session->cmd("show isdn active"); 107 $session->close; 108 return $output; 109 }# END routerConnect 110 111 112 foreach my $host (@hosts){ 113 chomp($host); 114 next if($host =~ /^#/); # skip comments lines 115 next if($host =~ /^$/); # skip empty lines 116 my @items = split (/;/,$host); 117 118 my($ip, $name, $dialer, $phone, $numcalls)= ($items[0], $items[ +1], $items[2], $items[3], $items[4]); 119 120 my $rtoutput = routerConnect($ip, $name, $dialer, $phone, $numc +alls); 121 122 chomp($rtoutput); 123 my @parts = split(/;/,$rtoutput); 124 if (($parts[1] ne $parts[2]) || ($parts[2] eq "Error")) { 125 print "- ".$rtoutput."\n";# unless(DEBUG); 126 push(@errorarray, $host."\n"); 127 } else { 128 print "+ ".$rtoutput."\n";# unless(DEBUG); 129 } 130 push(@fullarray,$rtoutput."\n"); 131 } 132 133 print $outfile @fullarray; 134 print $hostserrors @errorarray; 135 print "[" . $sitecount . "] sites...\n"; 136 exit(0); 137
ps. I am not able to bypass the 80 col line length :(

Replies are listed 'Best First'.
Re: timeout in net::telnet::cisco in a test bri script
by apl (Monsignor) on Mar 26, 2008 at 15:00 UTC
    Do you plan on modifying your code to reconnect if the connection is dropped? Sometimes the cause of the problem is outside your control.
      I don't use NET:telnet to see the active ISDN Connections. because it takes al Lot of time and you don't have problems with the Authorisation snmpwalk -v 1 -c Community YOURHost .1.3.6.1.4.1.9.9.27.1.1.3 if you have Cisco Routers you can use the Cisco OID Browser http://tools.cisco.com/Support/SNMP/do/BrowseOID.do?local=en&translate=Translate&objectInput=.1.3.6.1.4.1.9.9.27.1.1.3