Skip to content

Instantly share code, notes, and snippets.

@ri0day
Forked from amitsaha/disks_lshw.py
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ri0day/3f7a4dd2c42092f60ed9 to your computer and use it in GitHub Desktop.
Save ri0day/3f7a4dd2c42092f60ed9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Parse disk:* from lshw -xml
"""
Sample Output:
ATA Disk Hitachi HDS72105 /dev/sda
Disk Space: 500107862016
Sector size: 512
ATA Disk Hitachi HDS72105 /dev/sdb
Disk Space: 500107862016
Sector size: 512
Num disks 2
Total disk space 953880
"""
from lxml import etree
from subprocess import Popen,PIPE
inventory = Popen(['lshw', '-xml', '-numeric'], stdout=PIPE).communicate()[0]
inventory = etree.XML(inventory)
find_disks = etree.XPath(".//node[@class='disk']")
numdisks = 0
diskspace = 0
for disk in find_disks(inventory):
# has to be a hard-disk
if disk.find('size') is not None:
numdisks = numdisks + 1
diskspace = diskspace + int(disk.find('size').text)
print disk.find('description').text, disk.find('product').text, disk.find('logicalname').text
print 'Disk Space: ', disk.find('size').text
print 'Sector size: ',disk.find('configuration/setting/[@id="sectorsize"]').get('value')
print
print 'Num disks', numdisks
print 'Total disk space', diskspace/(1024**2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment