Category: | Networking Code |
Author/Contact Info | Neil Watson watson-wilson.ca |
Description: | Cisco-insert will parse a commented text file of Cisco IOS rules and upload them to a router. A backup of the running config is made prior to the upload. |
#!/usr/bin/perl use strict; use warnings; use Term::ReadKey; use Net::Telnet::Cisco; use Getopt::Std; my $host = "default"; #default backup filename my ($sec,$min,$hour,$mday,$mon,$year,$rest) = localtime(time); $mon += 1; $year += 1900; my $backup = $host."_run_".$year.$mon.$mday; my %opt = ( #set default options f => "HELP", b => "$backup", h => "$host" ); getopts("f:b:h:", \%opt); #print usage if ($opt{f} eq "HELP"){ print <<"*END*"; Usage: cisco-insert -f <command file> -b <backup file> -h <hostname> command file: this file contains a list of cisco IOS commands. You m +ay comment this file using "#" as the script will ignore them as well + as blank lines. backup file: cisco-insert will backup the running rules (show run) to + the given filename prior to issuing the commands from the command fi +le. The default filename is $backup, note the date is today. hostname: the hostname of the cisco device (dotted quad allowed). cisco-inert will print the output of the IOS commands to STDOUT. *END* exit; } my $rules_file = $opt{f}; $backup = $opt{b}; my (@rules, @output, $x); #get password and enable password ReadMode('noecho'); print "Enter Initial Password:"; my $passwd = <STDIN>; chomp ($passwd); print "\nEnter Enable Password:"; my $ena_passwd = <STDIN>; chomp ($ena_passwd); ReadMode('restore'); #connect my $session = Net::Telnet::Cisco->new(Host=>$host); @output = (@output, $session->login('', $passwd)); @output = (@output, $session->enable($ena_passwd)); #make backup of rules open BACK, ">$backup" || die "Could not open file $backup"; @output = $session->cmd('show run'); print BACK @output; close BACK; #open an parse rules file open RULES, $rules_file || die "Could not open file $rules_file"; while (<RULES>){ push @rules, $_ unless m/(^#|^$)/; } close RULES; #process rules foreach $x (@rules){ @output = (@output, $session->cmd($x)); } #show command output foreach $x (@output){ print $x; } |
|
---|