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

Kevin LaTona lists at studiosola.com
Thu Jun 13 10:42:07 EDT 2013


On Jun 12, 2013, at 5:59 PM, rice.cruft at gmail.com wrote:


> 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:
>  Lastly, a version of this regex as a non-VERBOSE expression works as expected.. Something about re.VERBOSE... ????
Snip


With the following code tweaks in Python 2.7.2, I find it works with VERBOSE for me, but not without.

I would say the regex could still use some more adjustments yet.

-Kevin





import re

inp ="""
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
"""

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]*?
      # Session state iSCSI
      iSCSI\s+Session\s+State:\s+(?P<sessionState>\w+)\s*
      # Session state Internal
      Internal\s+iscsid\s+Session\s+State:.*\s+(?P<ss2>\w+\s\w+)
       ''', re.VERBOSE|re.MULTILINE)

myDetails = [ m.groupdict() for m in regex.finditer(inp)][0]
for k,v in myDetails.iteritems():
   print k,v




#*************

If you want just the values back in the order parsed this will work for now.


for match in regex.findall(inp):
      for item in range(len(match)):
            print match[item]






More information about the Python-list mailing list