This code uses pywin32 and the _winreg module to get some vital information about the system the script is running on, including OS version, installed browsers and free space on the main HD.
Enjoy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import sys
import os
import ctypes
import _winreg
def get_registry_value(key, subkey, value):
if sys.platform != 'win32':
raise OSError("get_registry_value is only supported on Windows")
import _winreg
key = getattr(_winreg, key)
handle = _winreg.OpenKey(key, subkey)
(value, type) = _winreg.QueryValueEx(handle, value)
return value
class SystemInformation:
def __init__(self):
self.os = self._os_version().strip()
self.cpu = self._cpu().strip()
self.browsers = self._browsers()
self.totalRam, self.availableRam = self._ram()
self.totalRam = self.totalRam / (1024*1024)
self.availableRam = self.availableRam / (1024*1024)
self.hdFree = self._disk_c() / (1024*1024*1024)
def _os_version(self):
def get(key):
return get_registry_value(
"HKEY_LOCAL_MACHINE",
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
key)
os = get("ProductName")
sp = get("CSDVersion")
build = get("CurrentBuildNumber")
return "%s %s (build %s)" % (os, sp, build)
def _cpu(self):
return get_registry_value(
"HKEY_LOCAL_MACHINE",
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
"ProcessorNameString")
def _firefox_version(self):
try:
version = get_registry_value(
"HKEY_LOCAL_MACHINE",
"SOFTWARE\\Mozilla\\Mozilla Firefox",
"CurrentVersion")
version = (u"Mozilla Firefox", version)
except WindowsError:
version = None
return version
def _iexplore_version(self):
try:
version = get_registry_value(
"HKEY_LOCAL_MACHINE",
"SOFTWARE\\Microsoft\\Internet Explorer",
"Version")
version = (u"Internet Explorer", version)
except WindowsError:
version = None
return version
def _browsers(self):
browsers = []
firefox = self._firefox_version()
if firefox:
browsers.append(firefox)
iexplore = self._iexplore_version()
if iexplore:
browsers.append(iexplore)
return browsers
def _ram(self):
kernel32 = ctypes.windll.kernel32
c_ulong = ctypes.c_ulong
class MEMORYSTATUS(ctypes.Structure):
_fields_ = [
('dwLength', c_ulong),
('dwMemoryLoad', c_ulong),
('dwTotalPhys', c_ulong),
('dwAvailPhys', c_ulong),
('dwTotalPageFile', c_ulong),
('dwAvailPageFile', c_ulong),
('dwTotalVirtual', c_ulong),
('dwAvailVirtual', c_ulong)
]
memoryStatus = MEMORYSTATUS()
memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))
return (memoryStatus.dwTotalPhys, memoryStatus.dwAvailPhys)
def _disk_c(self):
drive = unicode(os.getenv("SystemDrive"))
freeuser = ctypes.c_int64()
total = ctypes.c_int64()
free = ctypes.c_int64()
ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive,
ctypes.byref(freeuser),
ctypes.byref(total),
ctypes.byref(free))
return freeuser.value
if __name__ == "__main__":
s = SystemInformation()
print s.os
print s.cpu
print "Browsers: "
print "\n".join([" %s %s" % b for b in s.browsers])
print "RAM : %dMb total" % s.totalRam
print "RAM : %dMb free" % s.availableRam
print "System HD : %dGb free" % s.hdFree
|
Other recipes that do similar things:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66455 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52276
Tags: sysadmin
Calling ctypes like that is wrong. The way you are calling GetDiskFreeSpaceEx is very wrong, since not only you don't check return error code, you are relying on the fact that SystemDrive environment variable only has ansi characters (otherwise unicode(...) won't work), which is true these days, but who can tell about future? Plus your code won't work on Win9x, since they don't have unicode versions of this function. The better (imho at least) way to wrap such native functions is like below:
GREAT PROGRAM.
_winreg has changed to winreg!