title: Python - regex to extract IP and Mask from snmp walk output
body: <p>I am struggling with regex to extract IP and subnet mask info from a snmp walk output.  Here is how output looks like.</p>
  
  <pre><code>[&lt;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')&gt;, &lt;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')&gt;, &lt;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')&gt;, &lt;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')&gt;,
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>[&lt;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')&gt;, 
  &lt;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')&gt;, 
  &lt;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')&gt;, 
  &lt;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')&gt;,
  </code></pre>
  
  <p>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.<strong>10.10.2.0</strong>') </p>
  
  <p>I need to to extract that info and save it in an array/list so it will look like this:</p>
  
  <pre><code>(10.10.2.0/255.255.255.0, 10.0.0.0/255.255.255.192, and so on ...)
  </code></pre>
  
  <p>I believe regex would be the best solution but despite many hours of research and trying I can't seem to find a solution.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>&lt;some code omitted ..&gt;
  # 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
  </code></pre>
  
  <p>It gives me this output:</p>
  
  <pre><code>['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 ...]
  </code></pre>
  
  <p>I guess the first step would be to get out that only gives me mask and IP and not there #s in between.</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>Thanks
  Damon</p>
  
  <p>Edit:</p>
  
  <pre><code>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
  </code></pre>