Skip to content

Instantly share code, notes, and snippets.

@gamesguru
Created September 8, 2020 17:53
Show Gist options
  • Select an option

  • Save gamesguru/db0206b4f4448f76cda3aed2db74d945 to your computer and use it in GitHub Desktop.

Select an option

Save gamesguru/db0206b4f4448f76cda3aed2db74d945 to your computer and use it in GitHub Desktop.
Gets CPU/MoBo/HDD info on Linux
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 11 13:55:12 2018
@author: shane
"""
import os, sys
import subprocess
#Command execution function `cmd()`
def cmd(cmds, _cwd='', selfdebug=False):
if _cwd == '':
_cwd = os.getcwd()
try:
if selfdebug:
print(cmds)
_output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=True, cwd=_cwd, encoding='850')
if selfdebug:
print(_output)
return _output.splitlines()
except subprocess.CalledProcessError as e:
_output = e.output
print('==> STANDARD ERROR')
print(cmds)
print(_output)
return _output.splitlines()
###
## Info functions #
###
def cpu():
nproc = int(cmd('nproc')[0])
for line in cmd('cat /proc/cpuinfo'):
if 'model name' in line:
return line.split(': ')[1] + f' × {nproc}'
def mobo():
MANUFACTURER = ''
PRODUCT_NAME = ''
for line in cmd('sudo dmidecode -t 2'):
if 'Manufacturer' in line:
MANUFACTURER = line.split(': ')[1]
elif 'Product Name' in line:
PRODUCT_NAME = line.split(': ')[1]
return MANUFACTURER + ' ' + PRODUCT_NAME
def ram():
TOTAL = 0
for s in cmd('cat /proc/meminfo'):
if s.startswith('MemTotal:'):
TOTAL = int(s.split(':')[1].split()[0]) #kB
MANUFACTURER = ''
FORM_FACTOR = ''
FREQ = 0
for s in cmd('sudo dmidecode -t 17 -q'):
if 'Manufacturer:' in s and MANUFACTURER == '':
MANUFACTURER = s.split(': ')[1]
elif 'Configured Clock Speed:' in s and FREQ == 0:
FREQ = int(s.split(': ')[1].split()[0]) #MHz
elif 'Form Factor:' in s and FORM_FACTOR == '':
FORM_FACTOR = s.split(': ')[1]
return f'{MANUFACTURER} {int(TOTAL / 1000000)}GB ({FREQ}MHz) {FORM_FACTOR}'
def hdd():
MODEL = ''
SIZE = ''
ROTATION_FORM = ''
dev = cmd("df -P ./ | awk 'END{print $1}'")[0]
if '/dev/mapper/luks' in dev:
for s in cmd(f'sudo cryptsetup status {dev}'):
if s.strip().startswith('device:'):
dev = s.split(':')[1].strip()
break
for s in cmd(f'sudo smartctl -i {dev}'):
if s.startswith('Device Model:') and MODEL == '':
MODEL = s.split(':')[1].strip()
elif s.startswith('Rotation Rate:') and ROTATION_FORM == '':
ROTATION_FORM = s.split(':')[1].replace('Solid State Device', 'SSD').strip()
elif s.startswith('User Capacity:') and SIZE == '':
SIZE = s.split('[')[1].split(']')[0].strip()
return f'{MODEL} ({ROTATION_FORM}) {SIZE}'
def git_last_modify_commit():
return cmd('git reflog HEAD --date=local')[0].split('{')[1].split('}')[0]
def exc_main():
for arg in sys.argv:
if arg == 'cpu':
print(cpu())
elif arg == 'mobo':
print(mobo())
elif arg == 'ram' or arg == 'mem':
print(ram())
elif arg == 'hdd':
print(hdd())
elif arg == 'git_active':
print(git_last_modify_commit())
if __name__ == '__main__':
exc_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment