Pages

1/25/2012

Script to push config changes to switches and routers.

Perl::Cisco::Script to push config changes to switches and routers.


#reconfig.pl
#
#############
use Net::Telnet;
#
my $list = "./list.txt";
my $cmd = "./cmds.txt";
my $login = "config";
my $password = "XXXXXXXXXX";
open HOSTS, "<", $list or die "$list not found!";
    my @all = ;
close HOSTS;
open CMDS, "<", $cmd or die "$cmd not found!";
    my @todo = ;
close CMDS;
###############
foreach $host (@all) {
$host =~ s/^\s+//;
$host =~ s/\s+$//;
my $outfile = "./$host-log.txt";
open OUT, ">", $outfile or die "Unable to create $outfile!";
print $host;
 if ($host =~ m/#/i) { #skip
  next;
 }
 print OUT "HOST=$host\n";
 $telnet = new Net::Telnet ( 
     Timeout=>15,
  Errmode=> sub{&ConnErr},
  Prompt => '/.*#$/');
 $telnet->open($host);
 $telnet->login($login, $password);
 foreach $task (@todo) {
  if ($task =~ m/#/i) { #skip
   next;
   }
  print OUT " -> $task  <-\t";
  @result = $telnet->cmd(String =>$task, Prompt => '/.*#$/');
  $telnet->waitfor('/#/');
  print OUT @result;
  print OUT "\r\n";
  foreach $line (@result) {
   if (($line =~ m/%/i)or($line =~ m/Translating/i)) { #Woah there!
    print OUT "ERROR:  Terminating\r\n";
    print "ERROR:  Check Log\r\n";
    exit;
   }
  }
  $telnet->waitfor('/#/');
 } #foreach task
print OUT "----------------------------------------\r\n";
#$telnet->waitfor('/#/');
close OUT;
} #foreach host

#END MAIN

################
sub ConnErr {
    print OUT "ERROR:  Connection Failed to $host\r\n";
    print OUT "----------------------------------------\r\n";
    next;
}
################

No comments: