Previous section   Next section

Recipe 17.10 Using SNMP to Perform Mass Configuration Changes

17.10.1 Problem

You want to automate the distribution of a set of configuration commands to a large number of routers.

17.10.2 Solution

The Perl script in Example 17-3 will distribute configuration commands to a large number of routers. It works using SNMP to trigger TFTP file transfers into the routers. In effect, this script lets you automatically distribute a series of configuration commands to a list of routers. Automating routine changes like this saves time and effort but more importantly, it virtually eliminates typing mistakes.

Here's some example output:

Freebsd% ./snmpcfg.pl
= = = = = = = = = = = = = = = = = = = = = = = = = = = = =
toronto - Update Successful
toronto - Wr Mem Successful
= = = = = = = = = = = = = = = = = = = = = = = = = = = = =
boston  - Update Successful
boston  - Wr Mem Successful
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
denver  - Update Successful
denver  - Wr Mem Successful
= = = = = = = = = = = = = = = = = = = = = = = = = = = = =
newyork - Update Successful
newyork - Wr Mem Successful
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
detroit - Update Failed
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
chicago - Update Successful
chicago - Wr Mem Successful
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
sanfran - Update Successful
sanfran - Wr Mem Successful
= = = = = = = = = = = = = = = = = = = = = = = = = = = = =
seattle - Update Successful
seattle - Wr Mem Successful
= = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Freebsd%

Example 17-3 contains the Perl code.

Example 17-3. snmpcfg.pl
#!/usr/bin/perl -w
#
#    snmpcfg.pl -- a script to perform mass configuration changes to
#                  a list of routers using SNMP.
#
#
# Set behaviour
$workingdir="/home/nms";
$snmprw="ORARW";
$tftpsrv="172.25.1.1";
#
#
$rtrlist="$workingdir/RTR_LIST";
open (RTR, "$rtrlist") || die "Can't open $rtrlist file";
open (LOG, ">$workingdir/RESULT") || die "Can't open $workingdir/RESULT file";
#
while (<RTR>) {
  chomp($rtr="$_");
  print LOG "= = = = = = = = = = = = = = = = = = = = = = = = = \n";
  print "= = = = = = = = = = = = = = = = = = = = = = = = \n";
  $snmpset="/usr/local/bin/snmpset -t 20 -r 2 -v1 -c $snmprw $rtr ";
  chomp($result=`$snmpset .1.3.6.1.4.1.9.2.1.50.$tftpsrv s SNMPCFG`);
  if ($result=~/.+ = "(.+)"$/ ) {
            if( $1 eq SNMPCFG ) {
                print LOG "$rtr - Update Successful\n";
                print "$rtr - Update Successful\n";
                chomp($result=`$snmpset .1.3.6.1.4.1.9.2.1.54.0 i 1`);
                if ($result=~/.+ = (.+)$/ ) {
                          if( $1 = = 1 ) {
                            print LOG "$rtr - Wr Mem Successful\n";
                            print "$rtr - Wr Mem Successful\n";
                          }
                          else {
                            print LOG "$rtr - Wr Mem Failed\n";
                            print "$rtr - Wr Mem Failed\n";
                          }
                }
                else {
                  print LOG "$rtr - Wr Mem Failed\n";
                  print "$rtr - Wr Mem Failed\n";
                }
            }
            else {
              print LOG "$rtr - Update Failed\n";
              print "$rtr - Update Failed\n";
            }
  }
  else {
    print LOG "$rtr - Update Failed\n";
    print "$rtr - Update Failed\n";
  }
}

17.10.3 Discussion

This script distributes a set of configuration commands to a list of routers using SNMP to trigger TFTP transfers, as we did manually in Recipe 17.8. The script goes through a list of routers in sequence, performing an snmpset command on each one to force the router to upload a predefined configuration file. If the file transfer completes successfully, the script will issue another snmpset command that permanently saves the running configuration file to NVRAM. The script displays a status report to the terminal screen and sends the same messages to a flat log file.

This script requires the NET-SNMP toolset. The script looks for the executable snmpset in the default location, /usr/local/bin. If your system has snmpset in another location, change the variable $snmpset.

Before running the script, change the variable $workingdir to point to the directory where the script resides. Also set the variable $snmprw to your organization's SNMP read-write community string. This script will not work with a read-only community string. You will need to set the value of $tftpsrv to the IP address of the TFTP server where the configuration file resides.

The script expects to find the router list located in the working directory in a file called RTR_LIST. This file should have a single router name per line. You can change the default name and location of this file by modifying the variable $rtrlist.

By default, the script will copy the configuration file SNMPCFG (which is located in the /tftpboot directory) to every router in the list. The configuration file must be world readable. This file should include a list of Cisco configuration commands as you would type them from a command prompt on the router. As we discussed in Chapter 1, we recommend inserting the keyword end at the end of the configuration file to prevent spurious error messages. If you want to change the filename, you will need to change both occurrences of the default filename SNMPCFG to the name of the new file.

The script creates a status report in a file called RESULT in the working directory. The script will automatically create this file the first time you execute it and will clear its contents each time the script is run. The status file allows you to run the script unattended and check for failures later. The easiest way to check for failures is to use the Unix grep utility to search the status report file for the keyword Fail.

17.10.4 See Also

Recipe 17.2; Recipe 17.5; Recipe 17.7; Recipe 17.8; Chapter 1; Appendix A


  Previous section   Next section
Top