Extract IP addresses from system report
<p>I traverse the address entries, using a Python regex to extract addresses to add to my list.  Here is a sample input string and desired output.  How do I do this?</p>

<pre><code>var = """

sw1:FID256:root&gt; ipaddrshow 

CHASSIS
Ethernet IP Address: 10.17.11.10
Ethernet Subnetmask: 255.255.210.0

CP0
Ethernet IP Address: 10.17.11.11
Ethernet Subnetmask: 255.255.210.0
Host Name: cp0
Gateway IP Address: 10.17.48.1

CP1
Ethernet IP Address: 10.17.11.12
Ethernet Subnetmask: 255.255.210.0
Host Name: cp1
Gateway IP Address: 10.17.18.1

sw1:FID256:root&gt;
"""
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>List Index 0 âchassis,ip 10.17.11.10 chassis,mask 255.255.210.0â

List Index 1 âcp0,ip 10.17.11.11 cp0,mask 255.255.210.0 cp0,gw 10.17.18.1â

List Index 2 âcp1,ip 10.17.11.12 cp1,mask 255.255.240.0 cp1,gw 10.17.18.1â
</code></pre>

<p>See <a href="https://regex101.com/r/eC0nV7/1" rel="nofollow">this regex</a> that extracts all the data:</p>

<pre><code>(?m)^([a-zA-Z0-9]+)(?:\r?\n|\r)Ethernet IP Address: ([\d.]+)(?:\r?\n|\r)Ethernet Subnetmask: ([\d.]+)(?:(?:\r?\n|\r)Host Name: ([a-z\d]+)(?:\r?\n|\r)Gateway IP Address: ([\d.]+))?
</code></pre>

<p><em>Details</em>:</p>

<ul>
<li><code>(?m)</code> - multiline mode to let <code>^</code> match a line start</li>
<li><code>^</code> - lime start</li>
<li><code>([a-zA-Z0-9]+)</code> - Group 1, one or more alphanumerics</li>
<li><code>(?:\r?\n|\r)</code> - a linebreak</li>
<li><code>Ethernet IP Address:</code> - literal string</li>
<li><code>([\d.]+)</code> - Group 2, 1+ digits and dots</li>
<li><code>(?:\r?\n|\r)Ethernet Subnetmask: ([\d.]+)</code> - similar pattern to above with Group 3 containing subnetmask</li>
<li><code>(?:(?:\r?\n|\r)Host Name: ([a-z\d]+)</code>  - similar pattern to above  with Group 4 containing host name</li>
<li><code>(?:\r?\n|\r)Gateway IP Address: ([\d.]+))?</code> - similar pattern to above  with Group 5 containing gateway.</li>
</ul>

<p>Now, all you need is to use <code>re.finditer</code> and build the resulting string:</p>

<pre><code>import re
p = re.compile(r'^([a-zA-Z0-9]+)(?:\r?\n|\r)Ethernet IP Address: ([\d.]+)(?:\r?\n|\r)Ethernet Subnetmask: ([\d.]+)(?:(?:\r?\n|\r)Host Name: ([a-z\d]+)(?:\r?\n|\r)Gateway IP Address: ([\d.]+))?', re.MULTILINE)
s = "sw1:FID256:root&gt; ipaddrshow \n\nCHASSIS\nEthernet IP Address: 10.17.11.10\nEthernet Subnetmask: 255.255.210.0\n\nCP0\nEthernet IP Address: 10.17.11.11\nEthernet Subnetmask: 255.255.210.0\nHost Name: cp0\nGateway IP Address: 10.17.48.1\n\nCP1\nEthernet IP Address: 10.17.11.12\nEthernet Subnetmask: 255.255.210.0\nHost Name: cp1\nGateway IP Address: 10.17.18.1\n\nsw1:FID256:root&gt;"
result = ["{0},ip {1} {0},mask {2} {3},gw {4}".format(z.group(1).lower(),z.group(2),z.group(3).lower(),z.group(4),z.group(5)) if z.group(4) else "{0},ip {1} {0},mask {2}".format(z.group(1).lower(),z.group(2),z.group(3)) for z in p.finditer(s)]
print(result)
</code></pre>

<p>See <a href="https://ideone.com/JebjLp" rel="nofollow">Python demo</a>.</p>

python regex