Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Router Backup.pl failing (operator error no doubt)

by rmagin (Initiate)
on Feb 14, 2013 at 21:27 UTC ( #1018799=perlquestion: print w/ replies, xml ) Need Help??
rmagin has asked for the wisdom of the Perl Monks concerning the following question:

Hello oh powerful Monks I need your help, I keep getting an error on my backup script but everything looks perfect.

I am launching a test by typing: perl -c backup.pl

The error message is:

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

I am using a script found here: http://www.fengnet.com/book/Cisco.IOS.Cookbook.2nd/I_0596527225_CHP_1_SECT_19.html#I9617__ExampleLabel__Example_1_4

Is it a typo from this script or should i attempt retyping entire script by hand in case there is a parse somewhere else?

Here is the code from the page

#!/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/RESUL +T 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");

Thank you for being here Guru's...Rmagin

Comment on Router Backup.pl failing (operator error no doubt)
Download Code
Re: Router Backup.pl failing (operator error no doubt)
by toolic (Chancellor) on Feb 14, 2013 at 21:40 UTC
    I think the syntax error may actually be on line 42:
    chop ($status=\Q$snmpset $oid s $rtr.cfg\Q);

    If I comment out that line, I no longer get the syntax error. Maybe the \Q should really be backticks (qx)?

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

      Thanks toolic, I thought it was a tick issue also. I tried the commenting out and saw that it ran clean on check but failed on execution

      I then tried placing the ticks so that the old progression should be calling the variable it still started spitting errors but this time from line 28. Too frustrating for what I don't actually want anyway.

      I have decided this is not what I want anyway. I used to have a super simple perl script that just called an EXPECT script and the router-file so that the EXPECT can telnet from a router-file into all the various equipment we have and send via tftp the configs down and then the perl moved it to its various sub directory dated and named so that if someone did an edit they could just run the expect script to their own router file to do repeated sequential downloads of one router if they are doing many edits...or they could run the script calling the entire router-file...

      I think I will just have to find my old harddrives and see if I can find it. It is not something I would expect someone to already have ready made, after all it took me a good bit to get the scripts right and sanity checked and I hate reinventing the wheel. Thank you again for taking a look, rmagin

Re: Router Backup.pl failing (operator error no doubt)
by kcott (Abbot) on Feb 15, 2013 at 04:14 UTC

    G'day rmagin,

    Welcome to the monastery.

    The message indicating a potential "runaway multi-line" is due to the chop() statement:

    $ perl -ce 'chop ($status=\Q$snmpset $oid s $rtr.cfg\Q);' Substitution pattern not terminated at -e line 1.

    \Q escape sequences are terminated with \E (not another \Q) and are applied to strings (i.e. they're not standalone operators):

    $ perl -ce 'chop ($status="\Q$snmpset $oid s $rtr.cfg\E");' -e syntax OK

    Take a look at quotemeta for a more complete description.

    toolic may well be correct in suggesting you want `...` instead of \Q...\Q; however, you may also want to escape the ASCII non-"word" characters in the return value - something like this (untested) might do what you want:

    $ perl -ce 'chop ($status = quotemeta(`$snmpset $oid s $rtr.cfg`));' -e syntax OK

    -- Ken

      Thank you Ken! that looks like it worked. Now just getting cp: cannot stat `/home/cisco/bkup/storage/LATEST/*': No such file or directory Can't open /home/cisco/bkup/RTR_LIST file at backup.pl line 29.

      This is no doubt because I have no file for it to run across so I can bang away at this...your explanation on the /Q and /E was right on the money and I tried your alt format and it runs clean on checks...thanks again for looking at it.

      After consideration I still think this is wrong way for me to go. I have stuff that won't download from snmp or mibs so I am going to search for a good perl/expect combo that I can use to telnet in get the prompts and send the downloads to a tftp server that i kick on and off

      Thank you both for the assist

      Rich

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018799]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-07-22 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (108 votes), past polls