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.
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.
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.
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.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.