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'.
如果数字包含一个卷(cp
,sp
是卷),我怎么能得到 vg10 或 lv10 后面的数字?这有点令人困惑,所以我会完全解释。
#!/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编号。
如何更改代码才能获得此解决方案?提前谢谢。
答案 0 :(得分: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'}