Python - regex to extract IP and Mask from snmp walk output

不羁的心 提交于 2021-02-19 07:48:22

问题


I am struggling with regex to extract IP and subnet mask info from a snmp walk output. Here is how output looks like.

[<SNMPVariable value='255.255.255.0' (oid='iso.3.6.1.2.1.4.21.1.11.10.10.2.0', oid_index='', snmp_type='IPADDR')>, <SNMPVariable value='255.255.255.192' (oid='iso.3.6.1.2.1.4.21.1.11.10.0.0.0', oid_index='', snmp_type='IPADDR')>, <SNMPVariable value='255.255.255.0' (oid='iso.3.6.1.2.1.4.21.1.11.10.10.10.0', oid_index='', snmp_type='IPADDR')>, <SNMPVariable value='255.255.255.252' (oid='iso.3.6.1.2.1.4.21.1.11.10.11.0.0', oid_index='', snmp_type='IPADDR')>,

I have organized the output in different lines just so it is easy to understand (actual output in my code is the above one without lines):

[<SNMPVariable value='255.255.255.0' (oid='iso.3.6.1.2.1.4.21.1.11.10.10.2.0', oid_index='', snmp_type='IPADDR')>, 
<SNMPVariable value='255.255.255.192' (oid='iso.3.6.1.2.1.4.21.1.11.10.0.0.0', oid_index='', snmp_type='IPADDR')>, 
<SNMPVariable value='255.255.255.0' (oid='iso.3.6.1.2.1.4.21.1.11.10.10.10.0', oid_index='', snmp_type='IPADDR')>, 
<SNMPVariable value='255.255.255.252' (oid='iso.3.6.1.2.1.4.21.1.11.10.11.0.0', oid_index='', snmp_type='IPADDR')>,

So between each block, we have subnet mask (value='255.255.255.0') and ip address (oid='iso.3.6.1.2.1.4.21.1.11.10.10.2.0')

I need to to extract that info and save it in an array/list so it will look like this:

(10.10.2.0/255.255.255.0, 10.0.0.0/255.255.255.192, and so on ...)

I believe regex would be the best solution but despite many hours of research and trying I can't seem to find a solution.

Here is what I have so far:

<some code omitted ..>
# contains SNMP output displayed above
print snmp_output
str_result = ''.join(str(snmp_output))
regex = re.findall(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", str_result)
print regex

It gives me this output:

['255.255.255.0', '3.6.1.2', '1.4.21.1', '11.10.10.2', '255.255.255.192', '3.6.1.2', '1.4.21.1', '11.10.0.0', '255.255.255.0', and so on ...]

I guess the first step would be to get out that only gives me mask and IP and not there #s in between.

Any help will be appreciated.

Thanks Damon

Edit:

for item in system_items:
    print '{oid}.{oid_index} {snmp_type} = {value}'.format(
        oid=item.oid,
        oid_index=item.oid_index,
        snmp_type=item.snmp_type,
        value=item.value

回答1:


To get the output as you have it in your question:

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

I don't have PySNMP installed, but here's a test:

>>> class SNMPVariable(object):
...     def __init__(self, value, oid):
...         self.value = value
...         self.oid = oid
...         

>>> s1 = SNMPVariable('255.255.255.0', 'iso.3.6.1.2.1.4.21.1.11.10.10.2.0')

>>> s2 = SNMPVariable('255.255.255.252', 'iso.3.6.1.2.1.4.21.1.11.10.11.0.0')

>>> system_items = [s1, s2]

>>> tuple('{}/{}'.format(regex.search(item.oid).group(0), item.value)
...       for item in system_items)
...       
('10.10.2.0/255.255.255.0', '10.11.0.0/255.255.255.252')



回答2:


You can use following regexp:

value='([\d.]+)'\s*\(oid='iso.*?\.(\d+\.\d+.\d+.\d+)'

Demo

And in python:

>>> import re
>>> str = r"[<SNMPVariable value='255.255.255.0' (oid='iso.3.6.1.2.1.4.21.1.11.10.10.2.0', oid_index='', snmp_type='IPADDR')>, <SNMPVariable value='255.255.255.192' (oid='iso.3.6.1.2.1.4.21.1.11.10.0.0.0', oid_index='', snmp_type='IPADDR')>, <SNMPVariable value='255.255.255.0' (oid='iso.3.6.1.2.1.4.21.1.11.10.10.10.0', oid_index='', snmp_type='IPADDR')>, <SNMPVariable value='255.255.255.252' (oid='iso.3.6.1.2.1.4.21.1.11.10.11.0.0', oid_index='', snmp_type='IPADDR')>,"
>>> re.findall(r"value='([\d.]+)'\s*\(oid='iso.*?\.(\d+\.\d+.\d+.\d+)'", str)
# => [('255.255.255.0', '10.10.2.0'), ('255.255.255.192', '10.0.0.0'), ('255.255.255.0', '10.10.10.0'), ('255.255.255.252', '10.11.0.0')]

Then you can take tuples and format them in strings as you need.



来源:https://stackoverflow.com/questions/49417978/python-regex-to-extract-ip-and-mask-from-snmp-walk-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!