[NCLUG] regex for snmp

Eric Brunson brunson at level3.net
Wed Nov 21 09:33:22 MST 2001


Though I'm always up for a good regexp puzzler, sometimes it's not the
right tool for the job.

Since snmp is going to return a variable number of attribute value
pairs you're going to end up doing something to the effect of:

while there's string left:
   attr, val, rest = /return the attribute, value and the rest of the string/ 

You can do the match by telling your regexp to include newlines
(tmtowtdi, i.e. including '\n' in your [], setting your record
separator to something other than '\n', etc. You did say you're using
perl, didn't you?) then using a look-ahead to keep from consuming your
next attribute name, these are left as an exercise.

Personally, I would just split the string on whitespace and iterate
over the whole thing myself.

And finally, have you looked at any of the SNMP modules available for
perl (or preferrably python)?  You are re-inventing the wheel, why not
just bop down to Goodyear?

e.

* mike cullerton (michaelc at cullerton.com) [011121 08:58]:
> hey folks, 
> 
>  hope everyone is having a great week. thanksgiving is my favorite holiday.
> there's no avoiding it, this one is definitely about the food!
> 
>  anyway, i'm reinventing the wheel here, writing some little functions to
> help with snmp calls and i'm having some regex trouble. i'm trying to parse
> the results i get from an snmpwalk and break it up into the individual MIB
> elements. the output i receive is just one big long string.
> 
> 
> so, i would like to break up
> 
>   system.sysName.0 = gw system.sysLocation.0 = fort collins
> 
> into
> 
>  system.sysName.0 = gw
>  system.sysLocation.0 = fort collins
>  
> 
> these are easy, and my regex works for them, but here's the tough one...
> 
> system.sysDescr.0 = Cisco Internetwork Operating System Software
> IOS (tm) 2500 Software (C2500-I-L), Version 11.2(23), RELEASE SOFTWARE (fc1)
> Copyright (c) 1986-2000 by cisco Systems, Inc.
> Compiled Thu 06-Jul-00 14:24 by torowe
> 
> with this one, my regex only grabs "Cisco Internetwork Operating System
> Software".
> 
> my regex is pretty lame, i think, but it's a start. anyone with some
> suggestions for fixing it. (this is a perl regex, but i would take a posix
> answer as well)
> 
>  /[\S]+ = .*\s/
> 
> the left side (before the ' = ') is always a bunch of letters and digits
> separated by periods. it appears there is always a space before the 'next
> element' so, what i'd like to have, is something like
> 
>  /[[a-zA-Z0-9]+\.]+[a-zA-Z0-9]+ = .*\s/
> 
> can you even nest brackets like that? it doesn't appear so :)
> 
> thanks folks,
> happy thanksgiving,
> mike
> 
>  -- mike cullerton   michaelc at cullerton dot com
> 
> _______________________________________________
> NCLUG mailing list
> NCLUG at nclug.org
> http://www.nclug.org/mailman/listinfo/nclug


-- 
 Eric Brunson   brunson at level3.net   page-eric at level3.net  
tcA thgirypoC muinelliM latigiD eht detaloiv tsuj evah uoY



More information about the NCLUG mailing list