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>I had something similar laying around and covers any attributes using a dictionary:</p>

<pre><code>(?P&lt;name&gt;^[A-Z0-9]+)\n|(?P&lt;attr&gt;^[\w]+[^:]+):\s(?P&lt;val&gt;[\d\w\.]+)\n
</code></pre>

<p>In python is not possible to recover intermediate captures from groups that match more than once per regex match (as far as I know), but some of mine horrible python should do the work.</p>

<p>I also made a random dictionary because to me is not clear what you are asking.</p>

<pre><code>import re

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;
"""

rgx = re.compile(r'(?P&lt;name&gt;^[A-Z0-9]+)\n|(?P&lt;attr&gt;^[\w]+[^:]+):\s(?P&lt;val&gt;[\d\w\.]+)\n', re.MULTILINE)

dict = {
    "Ethernet IP Address": "ip",
    "Ethernet Subnetmask": "mask",
    "Gateway IP Address": "gw",
    "Host Name": ""
}

def translate(attr):
    return dict[attr]

def build_list(r):
    entry = ""
    name = ""

    for l in rgx.finditer(var):

        if(l.group("name")):
            if(entry):
                r.append(entry[:-1])
                entry = ""

            name = l.group("name").lower()

        elif(l.group("attr")):
            varname = translate(l.group("attr"))
            value = l.group("val")

            if(varname != ""):
                entry += "{0},{1} {2} ".format(name, varname, value)

    # add last entry
    r.append(entry[:-1])
    entry = ""

def build_dict(d):
    entry = ""
    name = ""

    for l in rgx.finditer(var):

        if(l.group("name")):
            name = l.group("name").lower()
            d[name] = {}
        elif(l.group("attr")):
            varname = translate(l.group("attr"))
            value = l.group("val")

            if(varname != ""):
                d[name][varname] = value

r = []

build_list(r)

print r

d = {}

build_dict(d)

print d
</code></pre>

<p><a href="https://ideone.com/DBkR2E" rel="nofollow">Demo</a></p>

python regex