System information (OS, version, machine type, processor)
Boot time
CPU information (cores, frequencies)
Memory usage
Disk information (partitions, usage)
Network information (hostname, IP, MAC address)
Battery information (if available)
The script uses several Python libraries:
- : For system and OS information
- : For hardware information (CPU, memory, disk, battery)
- : For network information
- : For MAC address
- : For formatting boot time
- : For system operations
The script formats the output in a readable way with section separators and proper indentation. The
helper function converts bytes into human-readable formats (KB, MB, GB, etc.).
Note that some of these functions might require administrative/root privileges on certain systems, and some information might not be available on all platforms. The script includes basic error handling to prevent crashes in such cases.
import platform
import psutil
import socket
import uuid
import datetime
import os
def get_size(bytes, suffix="B"):
"""
Scale bytes to its proper format
"""
factor = 1024
for unit in ["", "K", "M", "G", "T", "P"]:
if bytes < factor:
return f"{bytes:.2f}{unit}{suffix}"
bytes /= factor
def get_system_info():
print("="*40 + "System Information" + "="*40)
# System information
uname = platform.uname()
print(f"System: {uname.system}")
print(f"Node Name: {uname.node}")
print(f"Release: {uname.release}")
print(f"Version: {uname.version}")
print(f"Machine: {uname.machine}")
print(f"Processor: {uname.processor}")
# Boot Time
print("\n" + "="*40 + "Boot Time" + "="*40)
boot_time_timestamp = psutil.boot_time()
bt = datetime.datetime.fromtimestamp(boot_time_timestamp)
print(f"Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}")
# CPU information
print("\n" + "="*40 + "CPU Info" + "="*40)
print("Physical cores:", psutil.cpu_count(logical=False))
print("Total cores:", psutil.cpu_count(logical=True))
# CPU frequencies
cpufreq = psutil.cpu_freq()
print(f"Max Frequency: {cpufreq.max:.2f}Mhz")
print(f"Min Frequency: {cpufreq.min:.2f}Mhz")
print(f"Current Frequency: {cpufreq.current:.2f}Mhz")
# Memory Information
print("\n" + "="*40 + "Memory Information" + "="*40)
svmem = psutil.virtual_memory()
print(f"Total: {get_size(svmem.total)}")
print(f"Available: {get_size(svmem.available)}")
print(f"Used: {get_size(svmem.used)}")
print(f"Percentage: {svmem.percent}%")
# Disk Information
print("\n" + "="*40 + "Disk Information" + "="*40)
partitions = psutil.disk_partitions()
for partition in partitions:
print(f"\nDrive: {partition.device}")
print(f" Mountpoint: {partition.mountpoint}")
print(f" File system type: {partition.fstype}")
try:
partition_usage = psutil.disk_usage(partition.mountpoint)
print(f" Total Size: {get_size(partition_usage.total)}")
print(f" Used: {get_size(partition_usage.used)}")
print(f" Free: {get_size(partition_usage.free)}")
print(f" Percentage: {partition_usage.percent}%")
except Exception:
continue
# Network information
print("\n" + "="*40 + "Network Information" + "="*40)
print(f"Hostname: {socket.gethostname()}")
print(f"IP Address: {socket.gethostbyname(socket.gethostname())}")
print(f"MAC Address: {':'.join(('%012x' % uuid.getnode())[i:i+2] for i in range(0, 12, 2))}")
# Battery information
print("\n" + "="*40 + "Battery Information" + "="*40)
battery = psutil.sensors_battery()
if battery:
print(f"Battery percentage: {battery.percent}%")
print(f"Power plugged in: {battery.power_plugged}")
minutes_left = battery.secsleft / 60
if minutes_left > 0:
print(f"Battery time left: {minutes_left:.2f} minutes")
else:
print("No battery detected")
if __name__ == "__main__":
get_system_info()
Comments
Post a Comment