Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

cisco-insert

by neilwatson (Priest)
on Oct 28, 2002 at 17:45 UTC ( [id://208560]=sourcecode: print w/replies, xml ) Need Help??
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;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://208560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found