Sign in to follow this  
Followers 0
DeltaRocked

regex for SNMP MIB

6 posts in this topic

Hello Friends,

I am stuck with some regexs , which are used to extract data from .MIB files. these files are used by SNMP trap notifications.

Correct Regexs:

StringRegExp($Value, '(?i)(?:traps\s)(\d*)(?: })',3)
StringRegExp($Value, '(?i)(.*)(?:NOTIFICATION-TYPE\s)',3)

Incorrect Regex:

$array=StringRegExp($Value, '(?i)description*[\S\s]*?(?:")?.*(?:".*\S\s.*)', 3)

From the below mentioned Sample Data, I am interested in extracting the values for Description belonging to traps. The above regex will extract all the descriptions instead of the desired "traps".

Sample Data:

Prod_Name MODULE-IDENTITY
    LAST-UPDATED "201101151711Z"
    ORGANIZATION 
        "Name"
    CONTACT-INFO 
        "contact
        me
        "
    DESCRIPTION 
        "MIB file for ProductName"
::= { CompName 2011 }

FoundTrp1  NOTIFICATION-TYPE
    OBJECTS { eventDate, 
        eventTime, 
        NFname }
    STATUS     current
    DESCRIPTION 
        "Found_Trp"
    ::= { traps 1 }
    
FoundTrp2  NOTIFICATION-TYPE
    OBJECTS { eventDate, 
        eventTime, 
        NFname }
    STATUS     current
    DESCRIPTION 
        "Found_Trp2"
    ::= { traps 2 }

NFname  OBJECT-TYPE
    SYNTAX     DisplayString
    MAX-ACCESS accessible-for-notify
    STATUS     current
    DESCRIPTION 
        "Description of NFname"
    ::= { eventVars 11 }

Appreciate your help in advance.

Regards

Deltarocked

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

Maybe this :

#include <Array.au3>
$s = FileRead("t.txt")
 
$a = StringRegExp($s, '(?s)DESCRIPTION\s\r\n\s{8}"(.*?)"', 3)
_ArrayDisplay($a)
If not, please provide an example of the desired output.

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

have a look

#include <Array.au3>

$s = FileRead("file.txt")

$a = StringRegExp($s, '(?sm)DESCRIPTION.*?$(.+?$)(.+?$)', 4)

;0th Index : Full Match
;1st Index : Description
;2nd Index : Traps.
For $i = 0 To UBound($a) -1
    $Temp = $a[$i]
    _ArrayDisplay($Temp)
Next

Logic : Capture the next two lines after the word DESCRIPTION

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Gentlemen - going through your replies . will revert back in a few moments.

[uPDATE]

Firefox: For some strange reasons - your regex works with the provided Sample Data-set - most probably got to do with the "{8}". I presume the additional non-printable chars got converted while pasting the O/P into the Sample-DataSet.

Phoenix: I have been trying to understand your regex (Failed miserably) - but it works . However there is one small thing I need from that --- The regex should grab only those results whose second-line contains the word "traps". Please let me know on how its done - will incorporate this knowledge into future regexs.

I have been facing problems with multi-line regexs, due to my lack of knowledge.

Regards

Deltarocked.

[uPDATE]

Added the check for "{ traps" within the code provided by Phoenix.

$array = StringRegExp($Value, '(?ism)description.*?$(.+?$)(.+?$)', 4)
For $i = 0 To UBound($array) - 1
    $Temp = $array[$i]
    If StringInStr($Temp[2], '{ traps ') Then
        $Temp[1] = StringStripWS($Temp[1], 7)
        ConsoleWrite(StringReplace($Temp[1],'_',' ') & @CRLF)
    EndIf
Next

tried a few other things --- but the above regex is the only one which works.

Edited by DeltaRocked

Share this post


Link to post
Share on other sites

I presume the additional non-printable chars got converted while pasting the O/P into the Sample-DataSet.

$a = StringRegExp($s, '(?s)DESCRIPTION\s\r\n(?:.*?)"(.*?)"', 3)

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This would do better.

#include <Array.au3>

$s = FileRead("file.txt")

$a = StringRegExp($s, '(?m)DESCRIPTION(.*\n){2}(.*?traps.*)', 4)

;0th Index : Full Match
;1st Index : Description
;2nd Index : Traps.
For $i = 0 To UBound($a) -1
    $Temp = $a[$i]
    _ArrayDisplay($Temp)
Next 

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0