Using re to perform grep functionality in Python

robert at forzasilicon.com robert at forzasilicon.com
Wed Mar 1 16:55:04 EST 2017


Hi All,

I'm relatively new to Python, and I am having some trouble with one of my scripts. Basically, this script connects to a server via ssh, runs Dell's omreport output, and then externally pipes it to a mail script in cron. The script uses an external call to grep via subprocess, but I would like to internalize everything to run in Python. I've read that re can accomplish the filtering, but I am having trouble getting it to duplicate what I already have.

##### USING EXTERNAL GREP #####

# display RAID controller info

cmd = ['ssh root at server.ip -C \"/opt/dell/srvadmin/sbin/omreport storage vdisk\"']

print '########## Array Status ##########'

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
if call(["grep", "-i", "ID"], stdin=p.stdout) != 0:
   print final
   sys.stdout.write(p)
p.wait()

###### END #######

This gives me:

####### Array Status ##########
ID                                : 0
Layout                            : RAID-1
Associated Fluid Cache State      : Not Applicable
----

vs the full output of:

########## Array Status ##########
List of Virtual Disks in the System

Controller PERC H310 Mini (Embedded)
ID                                : 0
Status                            : Ok
Name                              : Main
State                             : Ready
Hot Spare Policy violated         : Not Assigned
Encrypted                         : Not Applicable
Layout                            : RAID-1
Size                              : 2,794.00 GB (3000034656256 bytes)
T10 Protection Information Status : No
Associated Fluid Cache State      : Not Applicable
Device Name                       : /dev/sda
Bus Protocol                      : SATA
Media                             : HDD
Read Policy                       : No Read Ahead
Write Policy                      : Write Through
Cache Policy                      : Not Applicable
Stripe Element Size               : 64 KB
Disk Cache Policy                 : Enabled

##### END ####

I've been tinkering around with re, and the only code that I've come up with that doesn't give me a type error is this:

cmd = ['ssh root at server.ip -C \"/opt/dell/srvadmin/sbin/omreport storage vdisk\"']

print '########## Array Status ##########'

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

for line in p.stdout:
        final = re.findall('ID', line, re.DOTALL)
        print final
p.wait()


Which returns this:

########## Array Status ##########
[]
[]
[]
['ID']
[]
[]
[]
[]
[]
['ID']
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]

Obviously, not what I want. Can anyone feed some input?



More information about the Python-list mailing list