#!/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; }

In reply to cisco-insert by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.