Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47977

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

Subject Re: Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?)
From Kevin LaTona <lists@studiosola.com>
Date 2013-06-13 07:42 -0700
References <e682e1eb-1f7b-4776-82f0-11a0147947ec@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3206.1371136676.3114.python-list@python.org> (permalink)

Show all headers | View raw


On Jun 12, 2013, at 5:59 PM, rice.cruft@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]


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?) rice.cruft@gmail.com - 2013-06-12 17:59 -0700
  Re: Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?) Andreas Perstinger <andipersti@gmail.com> - 2013-06-13 09:17 +0200
    Re: Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?) rice.stew@gmail.com - 2013-06-14 12:09 -0700
  Re: Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?) Kevin LaTona <lists@studiosola.com> - 2013-06-13 07:42 -0700
  Re: Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?) Kevin LaTona <lists@studiosola.com> - 2013-06-13 14:07 -0700
    Re: Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?) rice.stew@gmail.com - 2013-06-14 12:14 -0700

csiph-web