#!/bin/perl

use Net::Telnet::Cisco;

$wait="";
open RTR, "<router_list.txt";
while (<RTR>) {
   chomp;
   ($ip,$user,$pass,$enable,$wait)=$_=~/^(.*),(.*),(.*),(.*),(.*)$/i;
   $wait=5 if ($wait eq "");
   @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;
mkdir $date;

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]") if ($$router[3] ne "");

   print "$$router[0] sh version\n";
   @output = $session->cmd('show version');

   open OUT, ">$date\\$$router[0]_version.txt";
   print OUT @output;
   close OUT;

   sleep($$router[4]);
}







#$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
# ");