[英]Getting hard disk serial number (USB stick / ATA based) with pyudev
Been working on the below sample code to retrieve the connected HDD detail using pyudev. 正在使用以下示例代码来使用pyudev检索连接的HDD详细信息。
Devices that I am trying to probe: 我尝试探查的设备:
With the below script, I have been able to retrieve the USB sticks information. 使用以下脚本,我已经能够检索到USB记忆棒信息。 But, when I connect the USB to SATA converter I get the serial number of the converter not the HDD.
但是,当我将USB连接到SATA转换器时,我得到的是转换器的序列号而不是HDD。 Also, for my regular SATA hard drives I get exception on "ID_VENDOR".
另外,对于我的常规SATA硬盘驱动器,我在“ ID_VENDOR”上遇到异常。
sample output: 样本输出:
Device Connected Info:
[Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0/host9/target9:0:0/9:0:0:0/block/sdc'),
Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host7/target7:0:0/7:0:0:0/block/sdb'),
Device(u'/sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda')]
Device Information:
SonyPendrive:
{'model': u'Storage Media', 'vendor': u'Sony', 'serial': u'CB071031B7E215C294'}
SATA IDE Converter
{'model': u'DE SATA Device', 'vendor': u'USB TO I', 'serial': u'000000000033'}
Regular HDD
Traceback (most recent call last):
File "t.py", line 52, in <module>
devInfo=decode_device_info(deviceRef)
File "t.py", line 9, in decode_device_info
vendor = device['ID_VENDOR'].replace('_',' ')
File "/home/srivathsan/Desktop/pyudev-pyudev-3a26e05/pyudev/device.py", line 831, in __getitem__
raise KeyError(property)
KeyError: 'ID_VENDOR'
Sample Code: 样例代码:
import glib
from pyudev import Context, Monitor
def decode_device_info(device):
''' Accept a device. Return a dict of attributes of given device.
Clean up all strings in the process and make pretty.
'''
vendor = device['ID_VENDOR'].replace('_',' ')
model = device['ID_MODEL'].replace('_',' ')
try:
serial = device['ID_SERIAL_SHORT']
except:
serial = device['ID_SERIAL']
return({'vendor':vendor, 'model':model, 'serial':serial})
def getDevicelist(udevContext):
devices=[]
for device in udevContext.list_devices(subsystem='block', DEVTYPE='disk'):
# Filter out cd drives, loop devices.
if device.get('ID_TYPE', '') == 'cd':
continue
if device.get('UDISKS_PRESENTATION_NOPOLICY', '0') == '1':
continue
devices.append(device)
return devices
try:
from pyudev.glib import MonitorObserver
def device_event(observer, device):
print 'event {0} on device {1}'.format(device.action, device)
except:
from pyudev.glib import GUDevMonitorObserver as MonitorObserver
def device_event(observer, action, device):
print 'event {0} on device {1}'.format(action, device)
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='block')
observer = MonitorObserver(monitor)
observer.connect('device-event', device_event)
monitor.start()
devicesConnected=getDevicelist(context)
if (devicesConnected):
print devicesConnected
for deviceRef in devicesConnected:
devInfo=decode_device_info(deviceRef)
print devInfo
glib.MainLoop().run()
Is there any parameters I am missing.Please give me some pointers. 我是否缺少任何参数。请给我一些提示。
For the KeyError
you are getting, you can replace 对于您得到的
KeyError
,您可以替换
vendor = device['ID_VENDOR'].replace('_',' ')
with one of: 具有以下之一:
try:
vendor = device['ID_VENDOR'].replace('_',' ')
except KeyError:
vendor = ""
or 要么
vendor = device.get('ID_VENDOR', '').replace('_',' ')
or 要么
if 'ID_VENDOR' in device:
vendor = device['ID_VENDOR'].replace('_',' ')
else:
vendor = ""
However, I'm afraid I don't know how/if you can use pyudev
to get 'beyond' the USB/SATA converter to the device connected to it. 但是,恐怕我不知道如何/是否可以使用
pyudev
将USB / SATA转换器“超越”到所连接的设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.