Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

modify the part of the OID returned in "oid" and "oid_index" by Python easysnmp module

Python easysnmp module returns the SNMP OID in two parts: oid and oid_index. For example, if I walk the ipAddressIfIndex.ipv4(.1.3.6.1.2.1.4.34.1.3.1 in numerical form) OID:

>>> from easysnmp import Session
>>> session = Session(hostname="r1", community='public', version=2, use_long_names=True, use_sprint_value=True)
>>> session.bulkwalk("ipAddressIfIndex.1")
[<SNMPVariable value='9' (oid='.iso.org.dod.internet.mgmt.mib-2.ip.ipAddressTable.ipAddressEntry.ipAddressIfIndex', oid_index='1.4.192.0.2.1', snmp_type='INTEGER')>,

/* rest of the output is removed for brevity */

>>>

.. then the oid is .iso.org.dod.internet.mgmt.mib-2.ip.ipAddressTable.ipAddressEntry.ipAddressIfIndex(or .1.3.6.1.2.1.4.34.1.3 numerically) and oid_id is 1.4.192.0.2.1. I would like to shift the returned oid to right by two so that the oid_id would be 192.0.2.1. Is this possible? I have tried with session.bulkwalk("ipAddressIfIndex.1.4"), session.bulkwalk(("ipAddressIfIndex", "1.4")), session.bulkwalk(".1.3.6.1.2.1.4.34.1.3.1.4") and used various session parameters like use_long_names or use_enums, but I'm not able to modify this behavior.

This is obviously not a big issue as I can read the last four fields(for example something like oid_index.split('.')[-4:]), but maybe there is a trick to shift the "line" between the oid and oid_index returned by easysnmp.

question from:https://stackoverflow.com/questions/65930043/modify-the-part-of-the-oid-returned-in-oid-and-oid-index-by-python-easysnmp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can use regex to extract you information

import re
regex = re.compile(r'(?:d+.){3}d+$')
tuple('{}/{}'.format(regex.search(item.oid).group(0), item.value)
      for item in system_items)

or you can use PySNMP

class SNMPVariable(object):
    def __init__(self, value, oid):
       self.value = value 
       self.oid = oid  
#example      
s1 = SNMPVariable('255.255.255.0', 'iso.3.6.1.2.1.4.21.1.11.10.10.2.0')
prtin('{}/{}'.format(regex.search(s1.oid).group(0), s1.value)
  

check this answer


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...