Home > front end >  How to extract the result of the snmpwalk and snmpgetnext,snmpget command in NET-SNMP using java?
How to extract the result of the snmpwalk and snmpgetnext,snmpget command in NET-SNMP using java?

Time:08-08

When using snmpgetnext, snmpget command, I can get the total result only one row. The result is

SNMPv2-SMI::enterprises.232.6.2.17.2.1.5.0.1 = INTEGER: 1

or

SNMPv2-SMI::enterprises.232.6.2.17.2.1.5.0 = No Such Instance currently exists at this OID.

How can I get the real result like 1 or No Such Instance currently exists at this OID? Also, Why people have told me that using regex isn't good for this?

When using snmpwalk, it returns many rows. I want the map<oid,reuslt> like <1.3.6.1.4.1.2352.2.4.1.4.1.3.18.1,36>. I think the question may be the same with snmpgetnext.

snmpwalk -v 2c -c testro 172.0.0.1 -On .1.3.6.1.4.1.2352.2.4.1.4.1.3
.1.3.6.1.4.1.2352.2.4.1.4.1.3.18.1 = Gauge32: 36
.1.3.6.1.4.1.2352.2.4.1.4.1.3.31.1 = Gauge32: 35
.1.3.6.1.4.1.2352.2.4.1.4.1.3.36.1 = Gauge32: 35

CodePudding user response:

You could use a regex employing capturing groups to extract the bits where to an OID is associated a value or no value at all.

The first capturing group extracts the OID (just to use it later when printing the results). The second capturing group identifies either the No Such Instance... case or the corresponding value. Finally, the third and fourth capturing groups respectively grab the value associated and the no-such-instance string.

The presence of a fourth capturing group can be used to determine whether an OID has no instance associated or not. In case the fourth capturing group is null, then a value is associated and this can be read by referencing the third group.

Here is an implementation of the regex:

SNMPv2-SMI::enterprises.([\d*.] )\s =\s (.*?:(.*)|(.*))

Here is a link to test the regex:

https://regex101.com/r/y4FX6a/1

Here is a possible implementation:

String test = "SNMPv2-SMI::enterprises.232.6.2.17.2.1.5.0.1 = INTEGER: 1\n"  
        "SNMPv2-SMI::enterprises.232.6.2.17.2.1.5.0 = No Such Instance currently exists at this OID\n"  
        "SNMPv2-SMI::enterprises.1.3.6.1.2.1.1.3.0 = Timeticks: (157736000) 18 days, 6:09:20.00";

Pattern pattern = Pattern.compile("SNMPv2-SMI::enterprises.([\\d*.] )\\s =\\s (.*?:(.*)|(.*))");
Matcher matcher = pattern.matcher(test);

while (matcher.find()) {
    //Checking if the fourth capturing group exists (no value associated)
    if (matcher.group(4) != null) {
        System.out.println(matcher.group(1)   " => "   matcher.group(4).trim());
    } else {
        //Otherwise if a match with pattern 'something:value' is found, then the third capturing group with only the value is returned
        System.out.println(matcher.group(1)   " => "   matcher.group(3).trim());
    }
}

Here is a link to test the code above:

https://www.jdoodle.com/iembed/v0/tLd

Output

232.6.2.17.2.1.5.0.1 => 1
232.6.2.17.2.1.5.0 => No Such Instance currently exists at this OID
1.3.6.1.2.1.1.3.0 => (157736000) 18 days, 6:09:20.00
  • Related