r0658 iteraction: 0 lines in output(show isdn active): 9 Open Calls: 2 of 3 r0658 iteraction: 1 - rit0658;Cmd2;Error r0037 iteraction: 0 lines in output(show isdn active): 7 Open Calls: 0 of 3 r0037 iteraction: 1 - rit0037;Cmd2;Error r1057 iteraction: 0 lines in output(show isdn active): 7 Open Calls: 0 of 3 r1057 iteraction: 1 lines in output(show isdn active): 10 Open Calls: 3 of 3 + r1057;3;3 [3] sites... r0658 iteraction: 0 lines in output(show isdn active): 9 Open Calls: 2 of 3 r0658 iteraction: 1 - rit0658;Cmd2;Error r0037 iteraction: 0 lines in output(show isdn active): 7 Open Calls: 0 of 3 r0037 iteraction: 1 lines in output(show isdn active): 10 Open Calls: 3 of 3 + r0037;3;3 r1057 iteraction: 0 lines in output(show isdn active): 7 Open Calls: 0 of 3 r1057 iteraction: 1 lines in output(show isdn active): 10 Open Calls: 3 of 3 + r1057;3;3 [3] sites... #### 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 Correa 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;$numCalls") 14 15 #file format "$routerIp;$routerName;$dialerIp;$dialerNumber;$numCalls" 16 #open(FILE,"hosts.with.dialer.csv") or die "> unable to open $_ file: $!\n"; 17 open(FILE,"hosts.with.dialer.tmp.csv") or die "> unable to open $_ file: $!\n"; 18 my @hosts = ; # 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 $hostserrors; 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 connections are up 65 print $name . "\titeraction: ".$i."\t" if(DEBUG); 66 my ($test1, $test2) = ""; 67 68 $test1 = $session->cmd( String => "isdn test call interface BRI 0/2/0 $phone number-of-calls 2", 69 Timeout => "15"); 70 $test2 = $session->cmd( String => "isdn test call interface BRI 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 contains data 81 82 print "lines in output(show isdn active): ".$#connets."\t" if(DEBUG); 83 84 foreach my $myline (@connets) { # count number of connections 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, $numcalls); 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