Welcome, guest | Sign In | My Account | Store | Cart

Reading the System information and Softwares installed in Windows NT/XP/2000/9x

Python, 129 lines
  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import win32api
import win32con
import win32file
from StringIO import StringIO

class GetSysInformation:
    def __init__(self):
        # variable to write a flat file
        self.fileHandle = None
        self.HKEY_CLASSES_ROOT = win32con.HKEY_CLASSES_ROOT 
        self.HKEY_CURRENT_USER = win32con.HKEY_CURRENT_USER 
        self.HKEY_LOCAL_MACHINE = win32con.HKEY_LOCAL_MACHINE
        self.HKEY_USERS = win32con.HKEY_USERS
        self.FILE_PATH = "//masblrfs06/karcherarea$/workarea/nishitg/"+ win32api.GetComputerName()
        self.CONST_OS_SUBKEY = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
        self.CONST_PROC_SUBKEY = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"
        self.CONST_SW_SUBKEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"        
    def getSysInfo(self):
        try:
            hCounter=0
            hProcessorName=""
            # connecting to the base
            hHandle = win32api.RegConnectRegistry(None,self.HKEY_LOCAL_MACHINE)
            # opening the sub key to get the processor name
            print "debug1"
            hHandle = win32api.RegOpenKeyEx(self.HKEY_LOCAL_MACHINE,self.CONST_PROC_SUBKEY,0,win32con.KEY_ALL_ACCESS)
            hNoOfKeys = win32api.RegQueryInfoKey(hHandle)[1]
            while hCounter < hNoOfKeys:           
                hData = win32api.RegEnumValue(hHandle,hCounter)
                if hData[0]== "Identifier":
                    hProcessorName = hData[1]
                hCounter = hCounter + 1
            if hProcessorName=="":
                    hProcessorName = "Processor Name Cannot be determined"
                    self.preparefile("Processor Name",hProcessorName)
            hCompName = win32api.GetComputerName()
            self.preparefile("Computer Name",hCompName)
            hDomainName = win32api.GetDomainName()
            self.preparefile("Domain Name",hDomainName)
            hUserName = win32api.GetUserName()
            self.preparefile("User Name",hUserName)
            # getting OS Details
            hCounter=0
            # opening the sub key to get the processor name
            hHandle = win32api.RegOpenKeyEx(self.HKEY_LOCAL_MACHINE,self.CONST_OS_SUBKEY,0,win32con.KEY_ALL_ACCESS)
            hNoOfKeys = win32api.RegQueryInfoKey(hHandle)[1]
            hOSVersion=""
            hOSName=""        
            while hCounter < hNoOfKeys:           
                hData = win32api.RegEnumValue(hHandle,hCounter)
                if hData[0]== "ProductName":
                    hOSName = hData[1]
                    self.preparefile("OS Name",hOSName)
                    break
                hCounter = hCounter + 1
            if hOSName=="":
                    self.preparefile("OS Name","OS Name could not be read from the registry")
            hCounter = 0 
            while hCounter < hNoOfKeys:
                hData = win32api.RegEnumValue(hHandle,hCounter)            
                if hData[0]== "CSDVersion":
                    hOSVersion = hData[1]
                    self.preparefile("OS Version",hOSVersion)
                    break
                hCounter = hCounter + 1
            if hOSVersion=="":
                self.preparefile("OS Version","OS Version could not be read from the registry")
            # inserting master data
            #insertMachineMaster(hCompName,hDomainName,hOSName,hOSVersion,hProcessorName)
        except:
            self.preparefile("Exception","in Exception in getSysDetails")
    def getSoftwareList(self):
        try:
            hCounter=0
            hAttCounter=0
            # connecting to the base
            hHandle = win32api.RegConnectRegistry(None,win32con.HKEY_LOCAL_MACHINE)
            # getting the machine name and domain name
            hCompName = win32api.GetComputerName()
            hDomainName = win32api.GetDomainName()
            # opening the sub key to get the list of Softwares installed
            hHandle = win32api.RegOpenKeyEx(self.HKEY_LOCAL_MACHINE,self.CONST_SW_SUBKEY,0,win32con.KEY_ALL_ACCESS)
            # get the total no. of sub keys
            hNoOfSubNodes = win32api.RegQueryInfoKey(hHandle)
            # delete the entire data and insert it again
            #deleteMachineSW(hCompName,hDomainName)
            # browsing each sub Key which can be Applications installed
            while hCounter < hNoOfSubNodes[0]:
                hAppName = win32api.RegEnumKey(hHandle,hCounter)
                hPath = self.CONST_SW_SUBKEY + "\\" + hAppName
                # initialising hAttCounter
                hAttCounter = 0
                hOpenApp = win32api.RegOpenKeyEx(self.HKEY_LOCAL_MACHINE,hPath,0,win32con.KEY_ALL_ACCESS)
                # [1] will give the no. of attributes in this sub key
                hKeyCount = win32api.RegQueryInfoKey(hOpenApp)
                hMaxKeyCount = hKeyCount[1]
                hSWName = ""
                hSWVersion = ""
                while hAttCounter < hMaxKeyCount:
                    hData = win32api.RegEnumValue(hOpenApp,hAttCounter)                    
                    if hData[0]== "DisplayName":
                        hSWName = hData[1]
                        self.preparefile("SW Name",hSWName)
                    elif hData[0]== "DisplayVersion":
                        hSWVersion = hData[1]
                        self.preparefile("SW Version",hSWVersion)
                    hAttCounter = hAttCounter + 1
                #if (hSWName !=""):
                #insertMachineSW(hCompName,hDomainName,hSWName,hSWVersion)
                hCounter = hCounter + 1           
        except:
            self.preparefile("Exception","In exception in getSoftwareList")           
    def openFile(self):
        try:
            self.fileHandle = open(self.FILE_PATH,'w')
        except:
            print "Exceptions in openFile"
    def preparefile(self,printCaption,printString):
        try:
            printText = printCaption + " : " + printString
            print printText
            self.fileHandle.write(printText + "\n")        
        except:
            print "In Exception of Prepare file"
    def closeFile(self):
        try:
            self.fileHandle.close()
        except:
            print "Exceptions in closeFile"

3 comments

Mohammad Alam 20 years, 2 months ago  # | flag

scanning program. The program is running without giving error .BuT I don see any output of the program.Can anyone tell me whats going on in this program??

Peter DP 19 years, 4 months ago  # | flag

hm, wouldn't call this a cookbook recipe, looks more like an ingredient.

alter this line to make sense on you pc
self.FILE_PATH = "//masblrfs06/karcherarea$/workarea/nishitg/"+ win32api.GetComputerName()
eg : self.FILE_PATH = "c:/temp/info_logging.log"

then to get rid of the exceptions in preparefile do this

    def preparefile(self,printCaption,printString):
        try:
            printText = printCaption + " : " + printString
            print printText
            if self.fileHandle:
              self.fileHandle.write(printText + "\n")
        except:
            print "In Exception of Prepare file"
which means, only log to a file if you have a file open !!

at this point get rid of the line
print "debug1"
really gives you confidence hey !

what we have now is a class definition, but we don't use it yet so add this at the end

if __name__ == "__main__":
  sysinfo=GetSysInformation()
  sysinfo.openFile()
  sysinfo.getSysInfo()
  sysinfo.getSoftwareList()

remarks:
openFile and closeFile are things I would do in __init__ __del__ and hand a parameter to __init__ telling it to do logging or not ...

{-- just my 2p--}
I don't think this is a good example of getting the installed software, one should rather use the wmi interface ...
{/-- just my 2p--}
Hiten Pandya 18 years, 9 months ago  # | flag

Problems with using WMI. Using the registry is sometimes the best option for retrieving the list of ALL installed applications on a machine.

Some application entries and their vendor names (weirdly) go missing when WMI is used for retrieving the entries. This seems to be an issue shared by PHP and Python COM interface.

Any input on this issue would be helpful, ofcourse.