Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?)

rice.cruft at gmail.com rice.cruft at gmail.com
Wed Jun 12 20:59:43 EDT 2013


I am parsing the output of an open-iscsi command that contains several blocks of data for each data set. Each block has the format:

Target: iqn.1992-04.com.emc:vplex-000000008460319f-0000000000000007
        Current Portal: 221.128.52.224:3260,7
        Persistent Portal: 221.128.52.224:3260,7
                **********
                Interface:
                **********
                Iface Name: default
                Iface Transport: tcp
                Iface Initiatorname: iqn.1996-04.de.suse:01:7c9741b545b5
                Iface IPaddress: 221.128.52.214
                Iface HWaddress: <empty>
                Iface Netdev: <empty>
                SID: 154
                iSCSI Connection State: LOGGED IN
                iSCSI Session State: LOGGED_IN
                Internal iscsid Session State: NO CHANGE


I have worked out the regex to grab the values I am interested with the exception of the 'iSCSI Connection State' and 'iSCSI Session State'. My regex is

regex = re.compile( r'''
        # Target name, iqn
        Target:\s+(?P<iqn>\S+)\s*
        # Target portal
        \s+Current\sPortal:\s*
        (?P<ipaddr>\w+\.\w+\.\w+\.\w+):(?P<port>\d+),(?P<tag>\d+)
        # skip lines...
        [\s\S]*?
        # Initiator name, iqn
        Iface\s+Initiatorname:\s+(?P<initiatorName>\S+)\s*
        # Initiator port, IP address
        Iface\s+IPaddress:\s+(?P<initiatorIP>\S+)
        # skip lines...
        [\s\S]*?
        # Session ID
        SID:\s+(?P<SID>\d+)\s*
        # Connection state
        iSCSI\ +Connection\ +State:\s+(?P<connState>\w+\s*\w*)
        [\s\S]*?    <<<<<< without this the regex fails
        # Session state
        iSCSI\ +Session\ +State:\s+(?P<sessionState>\w+)
        ''', re.VERBOSE|re.MULTILINE)


I tried using \s* to swallow the whitespace between the to iSCSI lines. No joy... However [\s\S]*? allows the regex to succeed. But that seems to me to be overkill (I am not trying to skip lines of text here.) Also note that I am using \ +   to catch spaces between the words. On the two problem lines, using \s+ between the label words fails.

The regex is compiled and fed to a finditer() call... With debug prints:

for m in regex.finditer(inp):
    print 'SSSSSS %d' % len(m.groups())
    for i in range(len(m.groups())):
        print ' SSS--> %s' % (m.group(i+1))

myDetails = [ m.groupdict() for m in regex.finditer(inp)]
print 'ZZZZ myDetails %s' % myDetails


Any help would be appreciated.   Lastly, a version of this regex as a non-VERBOSE expression works as expected.. Something about re.VERBOSE... ????

Thanks.

--Eric




More information about the Python-list mailing list