#!/opt/vdops/bin/perl # This script automates the process of copying running-config to startup-config # # V Who When What # --------------------------------------------------------------------------- # 1.1.0 skendric 2011-02-21 Upgrade to Netops 1.4.0 # 1.0.1 skendric 2010-06-25 Allow $error{$target} to be undef # 1.0.0 skendric 2008-01-12 First Version # # # Author: Stuart Kendrick, sbk {put at sign here} skendric {put dot here} com # # Source: http://www.skendric.com/device # # This software is available under the GNU GENERAL PUBLIC LICENSE, see # http://www.fsf.org/licenses/gpl.html # # I borrowed heavily from the following URLs in order to write this script: # http://www.cisco.com/warp/public/477/SNMP/copy_configs_snmp.shtml # # # # # Requirements: # -The target(s) must be pingable # # -The script must have file system access to the tftp directory # # -The following MIB modules stashed in /opt/vdops/share/snmp/mibs, # or wherever it is that you store MIB modules: # CISCO-PRODUCTS-MIB.my # # -PERL modules: the WI::Netops collection # # # Assumptions: # # # Tested on: # -perl-5.12.2 # -net-snmp-5.6 # # # Instructions: # -Customize the script for your site: find the 'user-configurable # variables' section and modify as appropriate # -Run this script # # # # Caveats: # # # Known Bugs: # # # To do: # -Add support for SNMPv3 # # Begin script # Load modules use strict; use warnings; use feature 'say'; use feature 'switch'; use Carp qw(carp cluck croak confess); use Cwd; use Data::Dumper; use English qw( -no_match_vars ); use Getopt::Std; use WI::Netops::CiscoTools 1.4.3; use WI::Netops::HostTools 1.0.4; use WI::Netops::NetopsTools 2.2.3; use WI::Netops::NetopsData 1.4.0; use WI::Netops::PingTools 1.1.7; use WI::Netops::SNMPTools 1.5.3; use WI::Netops::Utilities 1.4.4; # Declare global variables # Define global variables # Debug stuff $program_name = 'write-mem'; $usage = 'Usage: write-mem -s {yes|no} [-d {integer}] [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '1.1.0'; # Grab arguments getopts('ad:e:f:rs:', \%option); @target = @ARGV; # Set mode if ($option{r}) { $mode = 'report' } elsif (-t STDIN) { $mode = 'interactive' } else { $mode = 'batch' } ### Begin Main Program ############################################### { check_args(); # Check arguments read_config(); # Read Netops config file compile_mibs(); # Compile MIB files build_target(); # Populate @target target_check(); # Look for errors in @target basic_info(); # Gather basic information sanity_check(); # Exclude unusual cases print_before(); # Tell operator what I will do do_the_work(); # Do it print_after(); # Tell the operator what I did } ##### End Main Program ################################################# ######################################################################## # Copy old config files to the change tree, instruct the devices to save # their current config files to the tftp server, copy these new config # files to the changetree ######################################################################## sub do_the_work { my $result; # Debug trace trace_location('begin') if $debug; # Loop through the list of targets TARGET: for my $target (@target) { # Announce start say '--------------------------------------------------------' if $mode eq 'interactive'; print_it("Beginning to process $target"); if ($dome) { if (write_mem($target)) { say 'Success: saved running-config to startup-config' if $mode eq 'interactive'; log_it("Saved running-config to startup-config on $target"); } else { say 'Failure: did not save running-config to startup-config' if $mode eq 'interactive'; log_it("Failed to save running-config to startup-config on $target"); } } else { sleep $short; } # Announce completion print_it("Done processing $target"); say "-------------------------------------------------------\n\n" if $mode eq 'interactive'; } # Make things look pretty say('') if $mode eq 'interactive'; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Tell operator what I did ######################################################################## sub print_after { my $dir = getcwd(); my $shit_happens = 0; # Did errors occur? # Debug trace trace_location('begin') if $debug; # If running from cron, don't print report return 1 if $mode eq 'batch'; say "\n# Here is what I did\n"; print < 0) { print <