#!/opt/vdops/bin/perl # This script automates the process of IOS running configs # V Who When What # --------------------------------------------------------------------------- # 2.0.0 skendric 02-21-2011 Upgrade to Netops 1.4.0 # 1.9.1 skendric 06-25-2010 Allow $error{$target} to be undef # 1.9.0 skendric 02-01-2010 Upgrade to perl 5.10.1 # 1.8.3 skendric 04-29-2009 Remove CatOS support # 1.8.2 skendric 02-16-2009 Retire eval wrapping upload_config # 1.8.1 skendric 01-03-2009 Move check_tftp_copy to upload_config # 1.8.0 skendric 05-23-2008 Log contents of config file # 1.7.9 skendric 08-17-2007 Cosmetic changes to output # 1.7.8 skendric 03-21-2007 Stylistic mods # 1.7.7 skendric 11-14-2006 Replace Object Values with OIDs # 1.7.6 skendric 09-25-2006 Change variable names # 1.7.5 skendric 11-05-2005 Upgrade to new WI::VDOPS module structure # 1.7.4 skendric 07-14-2005 Fixed bug in use of touch_file # 1.7.3 skendric 06-26-2005 Forgot to hand path arg to upload_config # 1.7.2 skendric 06-05-2005 Forgot to hand args to several subroutines # 1.7.1 skendric 05-31-2005 Support new which_string routine # 1.7.0 skendric 05-09-2005 Support Netops.pm-1.2 # 1.6.1 skendric 07-21-2004 Prettified error msg printing # 1.6.0 skendric 05-09-2004 Migrate common functions to Netops.pm # 1.5.0 skendric 04-30-2004 Enhance command-line options # 1.4.3 skendric 12-01-2003 Autodetect local IP address # 1.4.2 skendric 11-16-2003 Use Net::Ping::External # 1.4.1 skendric 08-10-2003 Minor bug fixes # 1.4.0 skendric 03-28-2003 Numerous minor updates # 1.3.4 skendric 02-18-2003 Added more $config_file validation # 1.3.3 skendric 01-26-2003 Tightened scoping, changed $debug to $debug # 1.3.2 skendric 01-05-2003 Check IOS version for SNMP/TFTP support # 1.3.1 skendric 12-18-2002 Added support for Aironet # 1.3.0 skendric 11-14-2002 Added @version, improved build_target(), # . improved error handling # 1.2.5 skendric 10-20-2002 Fixed bug in use of $grab_hosts # 1.2.4 skendric 10-14-2002 Handle changing SNMP strings # 1.2.3 skendric 10-11-2002 Improve error handling in check_copy # 1.2.2 skendric 08-24-2002 Check for white space in snippet # 1.2.1 skendric 08-18-2002 Streamlined error handling # 1.2.0 skendric 08-16-2002 Added 'all' as command-line option # 1.1.4 skendric 08-16-2002 Fixed bug in CatOS 'begin/end' check # 1.1.3 skendric 08-05-2002 Additional error handling # 1.1.2 skendric 08-05-2002 Check for CatOS 'begin' and 'end' lines # 1.1.1 skendric 06-24-2002 Fixed bug in Catalyst 3500 detection # 1.1.0 skendric 05-23-2002 Auto-detect running fron cron # 1.0.5 skendric 05-10-2002 Logs username of operator # 1.0.4 skendric 05-05-2002 Added more debug code # 1.0.3 skendric 04-30-2002 Added more debug code # 1.0.2 skendric 04-29-2002 Streamlined error handling # 1.0.1 skendric 02-24-2002 Fixed bug in use of @error # 1.0.0 skendric 02-10-2002 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 # # # This script takes the following approach: # -Copies the named file from the local tftp directory to the named # devices, merging it with the running configuration of those # devices. # # # Requirements: # -The target(s) must be pingable # # -The script must have file system access to the tftp directory # # -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 # -Create the config file which will be uploaded # -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 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 my $config_file; # Name of config file to upload my $snmp_change; # 'yes' if we are changing SNMP strings my $wait; # Seconds to wait for device to complete upload # Define global variables $program_name = 'mod-config'; $usage = 'Usage: mod-config -s {yes|no} [-d {integer}] -c {filename} [-a | -e {expr} | -f {filename} | target1 target2 target3 ...]'; $version = '2.0.0'; # TFTP Stuff $tftp_server = $EMPTY_STR; $tftp_server = get_my_ipaddr() if $tftp_server eq $EMPTY_STR; $tftp_server_name = get_nodename($tftp_server); # The number of seconds I'm willing to wait for various operations, like # flash file system formats and file uploads, before giving up $wait = 360; # Grab arguments getopts('ad:c:e:f:s:', \%option); @target = @ARGV; # Check arguments die $usage unless $option{c}; $config_file = $option{c}; # 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 information sanity_check(); # Check for major errors print_before(); # Tell operator what I will do do_the_work(); # Do it print_after(); # Tell the operator what I did } ##### End Main Program ################################################# ######################################################################## # Do the work: upload config file ######################################################################## sub do_the_work { # Debug trace trace_location('begin') if $debug; # Log the config file log_file_contents("$tftp_dir/$config_file"); # Make things look pretty say '--------------------------------------------------------' if $mode eq 'interactive'; # Loop through the list of targets TARGET: for my $target (@target) { my $result; # Announce start say('') unless $target eq $target[0]; print_it("Beginning to process $target"); # Upload config file $result = upload_config($target, "$tftp_path/$config_file"); $error{$target} = 'Failed to upload config file' unless $result; # If I am changing SNMP strings, figure out what the new ones are snmp_char($target) if $snmp_change eq 'yes'; # Announce completion print_it("Done processing $target"); } # Make things look pretty say "--------------------------------------------------------\n\n" if $mode eq 'interactive'; # Debug trace trace_location('end') if $debug; return 1; } ######################################################################## # Tell operator what I did ######################################################################## sub print_after { my $shit_happens = 0; # Did any errors occur? # Debug trace trace_location('begin') if $debug; # If running from cron, don't print report return 1 if $mode eq 'batch'; # Print report if ($dome) { say "\n# Here is what I did\n"; } else { say "\n# Here is what I would have done, had you been serious"; } print < 0) { print "\n\n\n\n"; print <