cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post
Level 7

Backup cisco ios router/switch config

Here's a quick and dirty perl script that backs up your cisco ios router and switch configs via tftp when ever someone makes a config change. You need to have activestate perl install and Net::Telnet::Cisco and Timestamp::Simple perl mods.

  1. You need to make sure your devices are sending syslog message to orion.
  2. Create an alert/filter rule in syslog view that looks for messages containing *CONFIG*
  3. Add an alert action that executes an external app.  ie "C:\Perl\bin\perl.exe" D:\scripts\save_router_config.pl ${Hostname}
  4. Copy perl script below. Make necessary changes.  Our routers require usernames and passwords to login.  If you routers only require password, the username variable is ignored.
#!/usr/bin/perl

use Net::Telnet::Cisco;
use Timestamp::Simple qw(stamp);


my $device = "$ARGV[0]";
my $user = "username";
my $pass = "password";
my $enable_pass = "enable_password";
my $backup_host = 'tftp_server';
my $numArgs = $#ARGV + 1;
my $dt = stamp;


if ($numArgs != 1 )
{
    print "usage: save_router_config.pl device name\n";
    exit;
}

my $session = Net::Telnet::Cisco->new(Host => $device );
$session->login($user, $pass);

my @output = "";

if ($session->enable($enable_pass) )
{
    @output = $session->cmd('show privilege');
    print "My privileges: @output";
}
else
{
    warn "Can't enable: " . $session->errmsg;
}

$session->cmd("copy run tftp://$backup_host/configs/$device/$dt-$device.cfg\n\n\n");

$session->close;

0 Kudos
4 Replies
Level 13

For the sysadmins that do not like to code (me), here's another option: https://www.edge-cloud.net/2013/05/rancidtrac-on-ubuntu-12-04-lts/

0 Kudos

can rancid finally snmpv3?

0 Kudos
Level 10

Hi,

here is an old script of mine in which I backup our devices via snmpv3 and perl:

#!/usr/bin/perl -w

#loading modules
use strict;
use Net::SNMP;
use Net::Ping;
use Getopt::Std;
use Data::Dumper;
use Switch;


sub snmpset_copyconfig($$$);
sub string_to_hex($);
sub check_date();


#global variables
#snmp variable to set copy_config
my $oid_ConfigCopyProtocol = "1.3.6.1.4.1.9.9.96.1.1.1.1.2.111";
my $oid_SourceFileType = "1.3.6.1.4.1.9.9.96.1.1.1.1.3.111";
my $oid_DestinationFileType = "1.3.6.1.4.1.9.9.96.1.1.1.1.4.111";
my $oid_ServerAddress = "1.3.6.1.4.1.9.9.96.1.1.1.1.5.111";
my $oid_CopyFilename = "1.3.6.1.4.1.9.9.96.1.1.1.1.6.111";
my $oid_CopyStatus = "1.3.6.1.4.1.9.9.96.1.1.1.1.14.111";
my $oid_CopyStatus_DeleteInfo = "1.3.6.1.4.1.9.9.96.1.1.1.1.14.111";

my $session;
my $error;
our $hostname;
my $tftp_server = '<ip-of-tftpserver>';


#host_array aus nagios_db abfrage mittels hostgroup
my @network_hosts = ("Fill your array");

#Fehler beim Sammeln der Configs
my @unreachable_hosts = "";

my @error_hosts = "";

my $count_host = 0;
#ping aufbauen
my $ping = Net::Ping->new("icmp");

$ping->bind($tftp_server);

foreach $hostname (sort @network_hosts){
       
  unless ($ping->ping($hostname, 2)){
        print "Not reachable $hostname\n";
        push (@unreachable_hosts, $hostname);
        next;
  }
  $count_host++;
  chomp($hostname);
  #print "Hostname: $hostname\n";
# Create the SNMP session - with Write Permission
        ($session, $error) = Net::SNMP->session(
                   -hostname => $hostname,
                   -authprotocol =>  '<md5/sha>',
                   -authpassword =>  '<authpassword>',
                   -username     =>  '<snmp-username>',
                   -version      =>  '3',
                   -privprotocol =>  '<des/aes-128>',
                   -privpassword =>  '<privpassword>'
        );
        #TFTP Sicherung mittels SNMP
        my $ref_date = check_date();
        my $dest_config = "backup/$hostname$ref_date.conf";
        #my $dest_config = "$hostname$ref_date.conf";
        my $snmp_result7 = snmpset_copyconfig($oid_CopyStatus_DeleteInfo, 'i', 6 );
        #print "Line7: $snmp_result7\n";
        my $snmp_result1 = snmpset_copyconfig($oid_ConfigCopyProtocol, 'i', 1 );
        #print "Line1: $snmp_result1\n";
        my $snmp_result2 = snmpset_copyconfig($oid_SourceFileType, 'i', 4 );
        #print "Line2: $snmp_result2\n";
        my $snmp_result3 = snmpset_copyconfig($oid_DestinationFileType, 'i', 1 );
        #print "Line3: $snmp_result3\n";
        my $snmp_result4 = snmpset_copyconfig($oid_ServerAddress, 'a', $tftp_server);
        #print "Line4: $snmp_result4\n";
        my $snmp_result5 = snmpset_copyconfig($oid_CopyFilename, 's', $dest_config);
        #print "Line5: $snmp_result5\n";
        my $snmp_result6 = snmpset_copyconfig($oid_CopyStatus, 'i', 1 );
        #print "Line6: $snmp_result6\n";
       
        # Close the snmp-read session
        $session->close();
        $dest_config = "";
       
}
$ping->close();
print "Config saved for $count_host hosts\n";

foreach my $failed_hosts (sort @unreachable_hosts){
if ($failed_hosts eq ""){
next;
} else {
  print "Config failed to save for $failed_hosts\n";
}
}


sub snmpset_copyconfig($$$){   
        my $oid = shift;
        my $datatype = shift;
        my $parameter = shift;
        chomp($parameter);
        #my $context = shift;
        print "oid $oid , Para: $parameter\n";
        my $result = '';
    if ($datatype eq 'i'){
            $result = eval { $session->set_request($oid,INTEGER,$parameter); };
        } elsif ($datatype eq 'a'){
            $result = eval { $session->set_request($oid,IPADDRESS,$parameter);};
        } elsif ($datatype eq 's'){
            $result = eval { $session->set_request($oid,OCTET_STRING,$parameter); };
        }
   
    if (!defined $result){
        #printf "ERROR_Set by: %s\n",$@;
        #$session->close();
        push(@error_hosts, $hostname);
                next;
    }
        my $val = $result->{$oid};
        #printf "Result: %s\n", $val;          
        return $val;
}

sub check_date(){
        my($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)=localtime();
  my $jahr=$year;
  my $monat=$mon+1;
  my $tag=$mday;
       
        $jahr=$year +1900;
       
        if (length($monat) == 1)
        {
            $monat="0$monat";
        }
        if(length($tag) == 1)
        {
           $tag="0$tag";
        }
       
        #my $ref_date = "$Xdatum $Xzeit";
        my $ref_date = "_$jahr$monat$tag";
        return $ref_date ;
}

Have fun!

Kind regards,

Flo

0 Kudos
Level 14

This is handy for those that have Perl installed on their Orion box.  You should post this in the 'Tips and Tidbits' forum so it doesn't get lost in the sea of Orion questions.

http://thwack.com/forums/75.aspx 

0 Kudos