Python-正则表达式从snmp walk输出中提取IP和掩码 [英] Python - regex to extract IP and Mask from snmp walk output
问题描述
我正努力使用正则表达式从snmp walk输出中提取IP和子网掩码信息.这是输出的样子.
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')>,
因此,在每个块之间,我们都有子网掩码(value ='255.255.255.0')和ip地址(oid ='iso.3.6.1.2.1.4.21.1.11. 10.10.2.0 ')
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
它给了我这个输出:
['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 ...]
我想第一步是要走出去,那只会给我面具和IP,而中间没有#.
I guess the first step would be to get out that only gives me mask and IP and not there #s in between.
任何帮助将不胜感激.
谢谢达蒙
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
推荐答案
要获得问题中的输出,请按以下步骤操作:
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)
我没有安装 PySNMP
,但这是一个测试:
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')
这篇关于Python-正则表达式从snmp walk输出中提取IP和掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!