Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
script   2   28127
Python Script 9: Getting System Information in Linux using python script

Finding system information in Ubuntu like Number and type of processors, memory usage, uptime etc are extremely easy.

You can use Linux system commands like free -m uname -a and uptime to find these details. But there is no fun in doing that.

If you love coding in python, you want to do everything in python. So we will see how to find this information using python program. And in the process will learn something about Linux system in addition to python.

To find few details we will use python module, platform. We will be running this script using python3 interpreter and this script is tested on Ubuntu 16.04.


General Info:

So platform module is used to Access the underlying platform’s identifying data.

We will be using some of the method available in this module.

To get Architecture, call architecture method. It return a tuple (bits, linkage). 

To get the Linux distribution, call dist() or linux_distribution() method. It also returns a tuple.

import platform

# Architecture
print("Architecture: " + platform.architecture()[0])

# machine
print("Machine: " + platform.machine())

# node
print("Node: " + platform.node())

# system
print("System: " + platform.system())

# distribution
dist = platform.dist()
dist = " ".join(x for x in dist)
print("Distribution: " + dist)


Now to get other information we need to go into /proc/ directory of your system. If you look at files you will get an idea where system stores this type of information.


Processor Info:

Processor information is stored in cpuinfo file. Read the file and count the number and model name of processor.

# processor
print("Processors: ")
with open("/proc/cpuinfo", "r")  as f:
    info = f.readlines()

cpuinfo = [x.strip().split(":")[1] for x in info if "model name"  in x]
for index, item in enumerate(cpuinfo):
    print("    " + str(index) + ": " + item)


Memory Usage:

Memory details are stored in /proc/meminfo file. First line is the Total memory in system and second line is the free memory available at the moment.

# Memory
print("Memory Info: ")
with open("/proc/meminfo", "r") as f:
    lines = f.readlines()

print("     " + lines[0].strip())
print("     " + lines[1].strip())


Uptime:

How long your system has been up.

# uptime
uptime = None
with open("/proc/uptime", "r") as f:
    uptime = f.read().split(" ")[0].strip()
uptime = int(float(uptime))
uptime_hours = uptime // 3600
uptime_minutes = (uptime % 3600) // 60
print("Uptime: " + str(uptime_hours) + ":" + str(uptime_minutes) + " hours")


Average Load:

# Load
with open("/proc/loadavg", "r") as f:
    print("Average Load: " + f.read().strip())



Code:

Complete code is available on Github.

#
# Python script to fetch system information
# Author -  ThePythonDjango.Com
# Tested with Python3 on Ubuntu 16.04
# 

import platform

# Architecture
print("Architecture: " + platform.architecture()[0])

# machine
print("Machine: " + platform.machine())

# node
print("Node: " + platform.node())

# processor
print("Processors: ")
with open("/proc/cpuinfo", "r")  as f:
    info = f.readlines()

cpuinfo = [x.strip().split(":")[1] for x in info if "model name"  in x]
for index, item in enumerate(cpuinfo):
    print("    " + str(index) + ": " + item)

# system
print("System: " + platform.system())

# distribution
dist = platform.dist()
dist = " ".join(x for x in dist)
print("Distribution: " + dist)

# Load
with open("/proc/loadavg", "r") as f:
    print("Average Load: " + f.read().strip())

# Memory
print("Memory Info: ")
with open("/proc/meminfo", "r") as f:
    lines = f.readlines()

print("     " + lines[0].strip())
print("     " + lines[1].strip())

# uptime
uptime = None
with open("/proc/uptime", "r") as f:
    uptime = f.read().split(" ")[0].strip()
uptime = int(float(uptime))
uptime_hours = uptime // 3600
uptime_minutes = (uptime % 3600) // 60
print("Uptime: " + str(uptime_hours) + ":" + str(uptime_minutes) + " hours")


Run the script: python3 system_information.py

Output:


python script 9 getting system information in linux using python script


Host your Django App for free on PythonAnyWhere Server.

script   2   28127
2 comments on 'Python Script 9: Getting System Information In Linux Using Python Script'
Login to comment

Florian Kell Oct. 26, 2018, 12:29 p.m.
I Will Promote Your Business By Contacting Site Webmasters (CONTACT FORMS AT SITES) Via 60,000,000 Sites From Worldwide To Any Niche ... So you can contact the right audience with offers, driving traffic to your website: - Show your business/services/video’s to a targeted audience
Aly Chiman May 19, 2019, 8:08 p.m.
Hello there, My name is Aly and I would like to know if you would have any interest to have your website here at pythoncircle.com promoted as a resource on our blog alychidesign.com ? We are updating our do-follow broken link resources to include current and up to date resources for our readers. If you may be interested in being included as a resource on our blog, please let me know. Thanks, Aly

Related Articles:
Python program to convert Linux file permissions from octal number to rwx string
Python program to convert Linux file permissions from octal number to rwx string, Linux file conversion python program, Python script to convert Linux file permissions...
Python Script 17: Setting bing image of the day as desktop wallpaper
Python Script to Set bing image of the day as desktop wallpaper, Automating the desktop wallpaper change to bing image of day using python code, changing desktop wallpaper using python, downloading an image using python code, updating the desktop wallpaper daily using python...
Python Script 16: Generating word cloud image of a text using python
word cloud python, generating a word cloud of a text using python code, python code to generate a word cloud image of a text. Using word frequencies to generate a word cloud image using a python script...
Python Script 13: Generating ascii code from Image
Generating ascii art from image, converting colored image to ascii code, python script to convert image to ascii code, python code to generate the ascii image from jpg image....
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap