<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

How to Create a Computer Inventory Script in Python for Linux-MacOS

Adam Bertram| August 24 2018

| IT insights

how-to-create-a-computer-inventory-script-in-python-for-linux-macos

Basic details of your computer like operating system and version, memory, network adapter and hard drive are never stored in one central location. 

You always have to go clicking around for it. But if you’ve got Python installed which all Linux and MacOS users do, we can build a script that collects all of that information and returns all of the details right at the console.

In this article, I’d like to use Python on a Linux or MacOS system to gather a few items.

  • Operating system name and version
  • Total storage
  • Free storage space
  • Total RAM
  • NIC IP address

To do this, we'll be using Python 2.7.10 although the code we’ll we writing will probably work with other 2.x versions as well.

Enhance your IT career by learning how to automate with Python. Get started  with this free Python guide.

First, let’s grab the operating system and version. To do this, we can use the platform module.

>>> import platform
>>> platform.system()
'Darwin'
>>> platform.release()
'17.4.0'

Next, I’ll grab total storage. This one is a little tricky. It’s not Python 101 stuff so instead of creating our own code, why not use code from the community? I’ve decided to create a script called GetDiskUsage.py with the code from this activestate.com article.

Once I’ve created the script, I can then invoke it using the python interpreter in my console and get the total, used and free hard disk space.

# python GetDiskUsage.py
usage(total=499963170816, used=445335408640, free=49640325120)

Next up is total RAM. There are few ways to do this, but the best way seems to be to use the psutil module. This isn’t a standard Python module, but can easily be installed via pip.

# pip install psutil

Once psutil is installed, we can then fire up Python and import the virtual_memory module, which will then allow us to find total physical memory with just a couple lines easily.

>>> from psutil import virtual_memory
>>> mem = virtual_memory()
>>> mem.total
17179869184

Finally, we need to collect the NIC’s IPv4 address. This time no external module is available. We can use the socket module. The socket module has a gethostbyname() method which does a name lookup. Since we don’t need to go outside of our own computer, we need to resolve the hostname of the computer itself. To do that, we can first get the hostname of the local computer using the gethostname() method.

Once we’ve got the current host’s name, we can then call the gethostbyname() method to resolve that name to the machine’s IP address.

>>> import socket
>>> socket.gethostname()
'AdamsMacBook.local'
>>> socket.gethostbyname(socket.gethostname())
'192.168.86.141'

We’ve now got the code built to pull each of the individual components. Finally, we need to pull all of this information together into a single Python script that will return everything at once.

#!/usr/bin/env python

import platform
from psutil import virtual_memory
import socket
import os
import subprocess

print('OS is: {0} {1}'.format(platform.system(), platform.release()))

diskstats = subprocess.Popen(
    ['python', '/Users/adam/GetDiskUsage.py'], stdout=subprocess.PIPE).communicate()[0]
print('Disk statistics: {0}'.format(diskstats))
print('Total Memory: {0}'.format(virtual_memory().total))
print('Local IP address: {0}'.format(
    socket.gethostbyname(socket.gethostname())))

Here’s the output we finally come up with.

OS is: Darwin 17.4.0
Disk statistics: usage(total=499963170816, used=445346160640, free=49629573120)

Total Memory: 17179869184
Local IP address: 192.168.86.141

Of course, you can spice this up as much as you want, but this template will give you a great head start in building a useful computer inventory script in Python on Linux or MacOS.

Topics: IT insights

Leave a Reply

Your email address will not be published. Required fields are marked *

THIS POST WAS WRITTEN BY Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.