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\ ${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.

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: device name\n";

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";
    warn "Can't enable: " . $session->errmsg;

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


0 Kudos
4 Replies
Level 13

For the sysadmins that do not like to code (me), here's another option:

0 Kudos

can rancid finally snmpv3?

0 Kudos
Level 10


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 = "";
my $oid_SourceFileType = "";
my $oid_DestinationFileType = "";
my $oid_ServerAddress = "";
my $oid_CopyFilename = "";
my $oid_CopyStatus = "";
my $oid_CopyStatus_DeleteInfo = "";

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");


foreach $hostname (sort @network_hosts){
  unless ($ping->ping($hostname, 2)){
        print "Not reachable $hostname\n";
        push (@unreachable_hosts, $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
        $dest_config = "";
print "Config saved for $count_host hosts\n";

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

sub snmpset_copyconfig($$$){   
        my $oid = shift;
        my $datatype = shift;
        my $parameter = shift;
        #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",$@;
        push(@error_hosts, $hostname);
        my $val = $result->{$oid};
        #printf "Result: %s\n", $val;          
        return $val;

sub check_date(){
  my $jahr=$year;
  my $monat=$mon+1;
  my $tag=$mday;
        $jahr=$year +1900;
        if (length($monat) == 1)
        if(length($tag) == 1)
        #my $ref_date = "$Xdatum $Xzeit";
        my $ref_date = "_$jahr$monat$tag";
        return $ref_date ;

Have fun!

Kind regards,


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. 

0 Kudos