1
mount_list = ['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']

mount_list是一个列表li[1] = '/dev/vg10/lv10:cp:99'.

仅当 vg10 或 lv10 包含卷(,是卷)时,如何获取数字后跟vg10lv10?这有点令人困惑,所以我将完全解释。cpsp

#!/usr/bin/python

import subprocess
import sys

def volumeCheck(volume_name):
    """This function will check volume name is mounted or not.
    """
cmd  = ['cat', '/etc/auto_mount_list']
cmd1 = ['cat', '/etc/mdadm.conf']
cmd2 = ['cat', '/proc/mdstat']

vname = sys.argv[1]

p = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1, err = p.communicate()
pattern = p1

p2 = subprocess.Popen(cmd,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p3, err = p2.communicate()
mount_list = p3.split()
#if mount_list.find(vname) != -1 here i need the number of vg or lv only when it conatin sys.argv[1]


p4 = subprocess.Popen(cmd1,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p5, err = p4.communicate()
mdadm_list = p5.split()

p6 = subprocess.Popen(cmd2,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p7, err = p6.communicate()


new_vol = '/VolumeData/' + sys.argv[1] 

#if pattern.find(new_vol) != -1 : this also give the proper output but below code is more efficiant.

if new_vol in p1:
    print 'volume mounted'
else:
    print 'volume not mounted'


if __name__ == '__main__':
   volumeCheck(sys.argv[1])

这是我的 Python 程序的输出:

root@sr-query:/# python volume_check.py cp
['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']
volume mounted
root@sr-query:/# df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                938M  473M  418M  54% /
/dev/md0              938M  473M  418M  54% /
none                  250M  4.9M  245M   2% /dev
/dev/md2              9.7M  1.2M  8.0M  13% /usr/config
/dev/md7              961M   18M  895M   2% /downloads
tmpfs                 250M  8.2M  242M   4% /var/volatile
tmpfs                 250M     0  250M   0% /dev/shm
tmpfs                 250M     0  250M   0% /media/ram
/dev/mapper/vg10-lv10
                     1016M   65M  901M   7% /VolumeData/cp
root@sr-query:/# python volume_check.py new_volume
['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']
volume not mounted
root@sr-query:/# 

现在很明显python volume_check.py new_volume(它的参数)没有安装,也没有在自动安装列表中。但是python volume_check.py cp已安装并在自动安装列表中找到...在第二种情况下,我需要从自动安装列表中获取 vg 和 lv 编号

如何更改我的代码以获得此解决方案?提前致谢。

4

1 回答 1

1

不确定我是否正确理解了您的问题,但我认为您想从li[1]字符串中提取数字,对吗?如果是这样,只需使用正则表达式:

>>> import re
>>> s = '/dev/vg10/lv11:cp:99'
>>> m = re.search('/vg(?P<vg>\d+)/lv(?P<lv>\d+):\w+:(?P<number>\d+)', s)
>>> print m.groupdict()
{'lv': '11', 'number': '99', 'vg': '10'}
于 2012-10-19T07:52:21.450 回答