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

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

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.

Related Posts


Comments
Comments are disabled in preview mode.
Loading animation