#!/usr/bin/perl -w # # vyatta - SCP and SNMP Backup script for vyatta OFR routers # to be used by the wrancid rancid wrapper # # WARNING: This is only PROOF OF CONCEPT code and will screw up your data # and eat babies!!! # # 2008 Sam Noble # based on vpn3k by: # Copyright 2005 Michael Stefaniuc for Red Hat # # This script is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ####################################################### # Modules ####################################################### # Load any modules needed use strict; use Getopt::Std; use Net::SCP::Expect; use File::Temp; ####################################################### # Variables ####################################################### # Initialize variables used in this script my $backup_user = "rancid"; my $backup_pass = "rancid"; my $snmp_community = 'public'; my %options = (); getopts('f:', \%options); my $file = $options{'f'}; my $fh; my $host = $ARGV[0]; (my $tempfh, my $tempfile) = mkstemp( "/tmp/tmpfileXXXXX" ); #close($tempfh); # Open the output file. open($fh, ">", $file) or die "Cannot open output file\n"; print($fh "#RANCID-CONTENT-TYPE: wrapper.vyatta\n#\n"); # Get some infos from snmp print($fh "#SNMP description and location:\n"); my $snmp_description = "snmpget -v2c -c $snmp_community -On $host .1.3.6.1.2.1.1.1.0"; my $description = `$snmp_description`; print($fh "#Description $description"); my $snmp_location = "snmpget -v2c -c $snmp_community -On $host .1.3.6.1.2.1.1.6.0"; my $location = `$snmp_location`; print($fh "#Location $location\n"); #chomp($result); #if ($result =~ /VPN 3000 Concentrator Version (\S+) built by (\S+) on (.+)$/i) { #if ($result =~ /Vyatta (\S+) (\S+) (.+)$/i) { # my $version = $1; # my $compiled = "$3 by $2"; # print($fh "#Chassis Type: Vyatta V514\n#\n"); # #$snmp_command = "snmpget -v2c -c $snmp_community -On $host .1.3.6.1.2.1.47.1.1.1.1.11.1"; # $snmp_command = "snmpget -v2c -c $snmp_community -On $host .1.3.6.1.2.1.1.6.0"; # $result = `$snmp_command`; # chomp($result); # if ($result =~ /"([^"]+)"/) { # print($fh "#Location: $1\n#\n"); # } # print($fh "#Image: Version: $version\n"); # print($fh "#Image: Compiled: $compiled\n#\n"); #} # Call scp and download the running config. my $scp_session = Net::SCP::Expect->new(user=>"$backup_user",password=>"$backup_pass"); # the connection sometimes terminates incorrectly but we fully transfered # the file eval { $scp_session->scp("$host:/opt/vyatta/etc/config/config.boot", $tempfile); }; # Copy the config file over removing the comment at the beginning open($tempfh, "<", $tempfile) or die "Scp seems to have failed\n"; my $line; while ($line = <$tempfh>) { if ($line =~ /^#/) { next; } print($fh $line); } ####### # End # ####### close($fh); close($tempfh); unlink($tempfile);