Solved

Error running perl script to backup cisco devices

Posted on 2008-06-16
5
343 Views
Last Modified: 2010-05-18
I am attempting to use the perl script found in the Cisco IOS Cookbook.  i used to run this a few years ago but can't seem to get it to work now.  Error I get when I run the script "backup.pl" is :

syntax error at backup.pl line 44, near "$status=~/.+ = "(.+)" .*$"
  (Might be a runaway multi-line $$ string starting on line 43)
Execution of backup.pl aborted due to compilation errors.

please find attached the script as well.  it didn't used to be this hard :-)

This script looks for both perl and net-snmp in /usr/local/bin and they are both there.
#!/usr/local/bin/perl

#

#       backup.pl -- a script to automatically backup a list of 

#                    router configuraton files on a nightly basis.

#

#

# Set behavior

$workingdir="/home/cisco/bkup"; 

$snmprw="bugme"; 

$ipaddress="10.255.254.13";

$days="30";

#

#

$rtrlist="$workingdir/RTR_LIST";

$storage="$workingdir/storage";

$latest="$storage/LATEST";

$prev="$storage/PREV";

if (! -d $storage) {mkdir ($storage, 0755)};

if (! -d $prev) {mkdir ($prev, 0755)};

if (! -d $latest) {mkdir ($latest, 0755)};

($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time);

$mon++; $year=$year+1900;

$today1=sprintf("%.4d_%.2d_%.2d", $year, $mon, $mday);

$today="$storage/$today1";

system("cp -p $latest/* $prev/");

unlink <$latest/*>;

mkdir ($today, 0755);

 

open (RTR, "$rtrlist") || die "Can't open $rtrlist file";

open (LOG, ">$workingdir/RESULT") || die "Can't open $workingdir/RESULT file";

print LOG "Router Configuration Backup Report for $year/$mon/$mday\n";

print LOG "======================================================\n";

print LOG "Device Name                        Status\n";

print LOG "======================================================\n";

while (<RTR>) {

  chomp($rtr="$_");

  $oid=".1.3.6.1.4.1.9.2.1.55.$ipaddress";

  $snmpset ="/usr/local/bin/snmpset -v1 -c $snmprw -t60 -r2 $rtr";

  $rtrfile="/tftpboot/$rtr.cfg";

  unlink $rtrfile;

  open (CFG, ">$rtrfile"); print CFG " ";close CFG;

  chmod 0666, $rtrfile;

  chop ($status=\Q$snmpset $oid s $rtr.cfg\Q);

  $status=~/.+ = "(.+)" .*$;

  if($1 eq "$rtr.cfg") {

     if( -z "$rtrfile" ) {

        $result="not ok (File empty)";

        unlink $rtrfile; 

     }

     else {

        $result="ok";

        chmod 0444, $rtrfile;

        system("mv $rtrfile $latest");

     }

  }

  else {

     $result="not ok";

     unlink $rtrfile;

  }

 

printf LOG ("%-28s       %-28s\n", $rtr,$result); 

 

}

system ("cp -p $latest/*cfg $today");

$time=$days*86400;

print "$time\n";

($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time-$time);

$mon++; $year=$year+1900;

$rmdir=sprintf("%s/%.4d_%.2d_%.2d",$configs, $year, $mon, $mday);

system ("rm -r -f $storage/$rmdir");

Open in new window

0
Comment
Question by:raven76051
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 21798252
That error message is correct. Line 44 needs a final /
$status =~ /.+ = "(.+)" .*$/;
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 250 total points
ID: 21798276
Are you sure that script is exactly as it was?

On line 43, it looks like there is some missing double quotes.  Then line 44 looks it is missing the ending of the RE.

43:    chop ($status="\Q$snmpset $oid s $rtr.cfg\E");
44:    $status=~/.+ = "(.+)" .*$/;
0
 

Author Comment

by:raven76051
ID: 21798326
My apologies, you are both correct here is a correct copy of my script : (note the missing slash is back in but I'm afraid the error is the same) :

#!/usr/local/bin/perl
#
#       backup.pl -- a script to automatically backup a list of
#                    router configuraton files on a nightly basis.
#
#
# Set behavior
$workingdir="/home/cisco/bkup";
$snmprw="ORARW";
$ipaddress="172.25.1.1";
$days="30";
#
#
$rtrlist="$workingdir/RTR_LIST";
$storage="$workingdir/storage";
$latest="$storage/LATEST";
$prev="$storage/PREV";
if (! -d $storage) {mkdir ($storage, 0755)};
if (! -d $prev) {mkdir ($prev, 0755)};
if (! -d $latest) {mkdir ($latest, 0755)};
($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time);
$mon++; $year=$year+1900;
$today1=sprintf("%.4d_%.2d_%.2d", $year, $mon, $mday);
$today="$storage/$today1";
system("cp -p $latest/* $prev/");
unlink <$latest/*>;
mkdir ($today, 0755);

open (RTR, "$rtrlist") || die "Can't open $rtrlist file";
open (LOG, ">$workingdir/RESULT") || die "Can't open $workingdir/RESULT file";
print LOG "Router Configuration Backup Report for $year/$mon/$mday\n";
print LOG "======================================================\n";
print LOG "Device Name                        Status\n";
print LOG "======================================================\n";
while (<RTR>) {
  chomp($rtr="$_");
  $oid=".1.3.6.1.4.1.9.2.1.55.$ipaddress";
  $snmpset ="/usr/local/bin/snmpset -v1 -c $snmprw -t60 -r2 $rtr";
  $rtrfile="/tftpboot/$rtr.cfg";
  unlink $rtrfile;
  open (CFG, ">$rtrfile"); print CFG " ";close CFG;
  chmod 0666, $rtrfile;
  chop ($status=\Q$snmpset $oid s $rtr.cfg\Q);
  $status=~/.+ = "(.+)".*$/;
  if($1 eq "$rtr.cfg") {
     if( -z "$rtrfile" ) {
        $result="not ok (File empty)";
        unlink $rtrfile;
     }
     else {
        $result="ok";
        chmod 0444, $rtrfile;
        system("mv $rtrfile $latest");
     }
  }
  else {
     $result="not ok";
     unlink $rtrfile;
  }

printf LOG ("%-28s       %-28s\n", $rtr,$result);

}
system ("cp -p $latest/*cfg $today");
$time=$days*86400;
print "$time\n";
($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time-$time);
$mon++; $year=$year+1900;
$rmdir=sprintf("%s/%.4d_%.2d_%.2d",$configs, $year, $mon, $mday);
system ("rm -r -f $storage/$rmdir");
0
 
LVL 19

Assisted Solution

by:Kim Ryan
Kim Ryan earned 250 total points
ID: 21798386
You could try rewriting line 43
chop ($status=\Q$snmpset $oid s $rtr.cfg\Q);
as
$status="$snmpset $oid s $rtr\.cfg";
chomp($status);

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
This tutorial will go through the steps required to write a script that will back up the configuration settings of a HP-ProCurve switch. You will need to get the following things to follow this tutorial: Telnet Scripting Tool e.g. TST10.exe …
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now