#!/bin/perl

use Net::Telnet::Cisco;

open RTR, "<router_list.txt";
while (<RTR>) {
   if ($_ =~ /^#/) {
      print $_;
      next;
   }
   chomp;
   ($ip,$user,$pass,$enable,$wait)=$_=~/^(.*),(.*),(.*),(.*),(.*)$/i;
   $wait=60 if ($wait eq "");
#   print "$ip,$user,$pass,$enable,$wait\n";
   @routers=(@routers,[$ip,$user,$pass,$enable,$wait]);
}
close RTR;

my $session;
my @output;

my ($ss,$mm,$hh,$dd,$mon,$yy,$we,$doy,$st)=localtime;
$mon++;
$yy+=1900;
$dd  = '0' . $dd  if ($dd  < 10);
$mon = '0' . $mon if ($mon < 10);
my $date=$yy . "-" . $mon . "-" . $dd;

my @commands;
open CMD, "<router_config.txt";
foreach (<CMD>) {
   if ($_ =~ /^#/) {
      print $_;
      next;
   }
   chomp unless ($_ =~ /^\n$/);
   @commands=(@commands,$_);
}
close CMD;

foreach my $router (@routers) {
   print "$$router[0] connect\n";

   next unless ($session = Net::Telnet::Cisco->new(Host => "$$router[0]", Errmode=>'return'));

   if ($$router[1] eq '') {
      print "$$router[0] send pw\n";
      $session->login(Password => "$$router[2]");
   }
   else {
      print "$$router[0] send user, pw\n";
      $session->login(Name => "$$router[1]", Password => "$$router[2]");
   }

   print "$$router[0] enable\n";
   $session->cmd("enable\n$$router[3]");



   foreach (@commands) {
      print "$$router[0] $_\n";
      @output = $session->cmd("$_");
   }

   $session->cmd("end");
   $session->cmd("wr");
   sleep ($$router[4]);


   print "\n\n";

}




#$ok = $obj->login($username, $password);#
#
#    $ok = $obj->login([Name     => $username,]
#                      [Password => $password,]
#                      [Passcode => $passcode,] # for Secur-ID/XTACACS
#                      [Prompt   => $match,]
#                      [Timeout  => $secs,]);



#$session = Net::Telnet::Cisco->new(
#        [Autopage                 => $boolean,] # 1
#        [More_prompt              => $matchop,] # '/(?m:^\s*--More--)/',
#        [Always_waitfor_prompt    => $boolean,] # 1
#        [Waitfor_pause            => $milliseconds,] # 0.1
#        [Normalize_cmd            => $boolean,] # 1
#        [Send_wakeup              => $when,] # 0
#        [Ignore_warnings          => $boolean,] # 0
#        [Warnings                 => $matchop,] # see docs
#        
#        # Net::Telnet arguments
#        [Binmode                  => $mode,]
#        [Cmd_remove_mode          => $mode,]
#        [Dump_Log                 => $filename,]
#        [Errmode                  => $errmode,]
#        [Fhopen                   => $filehandle,]
#        [Host                     => $host,]
#        [Input_log                => $file,]
#        [Input_record_separator   => $char,]
#        [Option_log               => $file,]
#        [Output_log               => $file,]
#        [Output_record_separator  => $char,]
#        [Port                     => $port,]
#        [Prompt                   => $matchop,] # see docs
#        [Telnetmode               => $mode,]
#        [Timeout                  => $secs,]
#    );



#if ($session->enable("enable_password"))
#$session->errmsg;



# my $protocol  = ''; # default value
#  my $ip       = '10.0.0.1';
#  my $repeat    = 10;
#  my $datagram  = 1500;
#  my $timeout   = ''; # default value
#  my $extended  = ''; # default value
#  my $sweep     = ''; # default value

#  $session->cmd(
#  "ping
#  $protocol
#  $ip
#  $repeat
#  $datagram
#  $timeout
#  $extended
#  $sweep
# ");