def handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict): try: pidToPortMap = {} # # We need a local pid to port map lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' if USE_SUDO: lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) < 1: continue lsofLine = lsofLine.strip() m = re.search( '\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() # # Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] # # Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass
def handleProcessToPortByPFile(USE_SUDO, localClient, procToPortDict): # # Use PFILES to map each process to a port and create a dictionary ############################################ try: for pID in procToPortDict.keys(): pFilesCmd = 'pfiles ' + pID + ' 2>/dev/null | grep "sockname: AF_INET"' if USE_SUDO: pFilesCmd = 'sudo ' + pFilesCmd pFilesStr = localClient.executeCmd(pFilesCmd) if len(pFilesStr) < 1: continue pFilesLines = dbconnect_utils.splitCommandOutput(pFilesStr.strip()) if pFilesLines == None: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Error: Invalid output from pfiles: ' + pFilesStr) continue for pFilesLine in pFilesLines: pFilesLine = pFilesLine.strip() m = re.search( '.+AF_INET\s+(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if re.search('AFINET6', pFilesLine): m = re.search( '.+AF_INET6.*:(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if (m) and m.group(2) != '0': ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pID] )[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pID])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pID] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] No TCP port associated with PID [' + pID + ']: ' + pFilesLine) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to make a process to port map using pfiles: <%s>' % excInfo) pass
def handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict): try: pidToPortMap = {} # # We need a local pid to port map lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' if USE_SUDO: lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) < 1: continue lsofLine = lsofLine.strip() m = re.search('\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() # # Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] # # Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Found port <%s:%s> for pid <%s>' % ( ipAddress, serverPort, pid)) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass
def handleProcessToPortByPFile(USE_SUDO, localClient, procToPortDict): # # Use PFILES to map each process to a port and create a dictionary ############################################ try: for pID in procToPortDict.keys(): pFilesCmd = 'pfiles ' + pID + ' 2>/dev/null | grep "sockname: AF_INET"' if USE_SUDO: pFilesCmd = 'sudo ' + pFilesCmd pFilesStr = localClient.executeCmd(pFilesCmd) if len(pFilesStr) < 1: continue pFilesLines = dbconnect_utils.splitCommandOutput(pFilesStr.strip()) if pFilesLines == None: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Error: Invalid output from pfiles: ' + pFilesStr) continue for pFilesLine in pFilesLines: pFilesLine = pFilesLine.strip() m = re.search('.+AF_INET\s+(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if re.search('AFINET6', pFilesLine): m = re.search('.+AF_INET6.*:(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if (m) and m.group(2) != '0': ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Adding port <%s:%s> for process <%s>' % ( ipAddress, serverPort, (procToPortDict[pID])[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pID])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pID])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] No TCP port associated with PID [' + pID + ']: ' + pFilesLine) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to make a process to port map using pfiles: <%s>' % excInfo) pass
def getProcToPortDictOnLinux(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd('ps -e -o pid -o uid -o user -o cputime -o command --cols 4000') if psOut == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!') return None psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!') return None for psLine in psLines: psLine = psLine.strip() token = psLine.split(None, 4) # Some checks to make sure line is valid if(len(token) != 5): continue if(not re.search('^\d+$',token[0])): continue if(len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex+1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)',commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ try: netstatLisCmd = 'netstat -anp | grep "LISTEN"' if USE_SUDO == 'true': netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput(netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got nsLine <%s>' % nsLine) m = re.search('tcp.* (\S+):(\d+).*:.*\s+(\d+|-).*', nsLine) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3).strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got port <%s> for pid <%s>' % (serverPort, pid)) if pid != '-' and procToPortDict.has_key(pid): dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Couldn\'t get netstat information (Most likely due to lack of user permissions): ' + nsLine) else: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnAIX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd("ps -e -o 'user,pid,time,args'") psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes!') return None for psLine in psLines: if(re.search('TIME COMMAND', psLine)): continue ## Reg for processes with args res = re.search('(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if(res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search('(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)', psLine) if(res): cleanArgs = '' if(res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1) cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes: <%s>' % excInfo) pass if USE_LSOF.lower().strip() == 'true': ## Use LSOF to map each process to a port and create a dictionary ############################################ try: keydeletion = [] # keys to delete - Daniel La pidToPortMap = {} ## We need a local pid to port map # lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' lsofCmd = '/usr/local/bin/lsof -n -P -i | grep -i listen 2>/dev/null' # Daniel La need to specify full path to lsof. 22/11/10 if USE_SUDO == 'true': lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) <1: continue lsofLine = lsofLine.strip() m = re.search('\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() # pidToPortMap[pid] = [ipAddress, serverPort] # below lines were added - Daniel La logger.debug('serverport is: ', serverPort, ',pid is: ', pid, ',ip is: ', ipAddress) pidnport = str(pid) + '.' + str(serverPort) pidToPortMap[pidnport] = [ipAddress, serverPort] # end of add. if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): logger.debug('before pid is: ', pid) m = re.search('(\w+)\.\w+', pid) pidsplit = m.group(1).strip() logger.debug('after found pid is: ', pidsplit) # if pid in procToPortDict.keys(): if pidsplit in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] #dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) #(procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress #(procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pidsplit)) (procToPortDict[pidsplit])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pidsplit])[dbconnect_utils.PORT_INDEX] = serverPort # create new key and add new entry - Daniel La logger.debug('creating new entry for: ', (procToPortDict[pidsplit])[dbconnect_utils.PROCESSNAME_INDEX], ' pid: ', pidsplit, ' port: ', serverPort) pidnport = str(pidsplit) + '.' + str(serverPort) if dbconnect_utils.populateProcToPortDict(procToPortDict, pidnport, (procToPortDict[pidsplit])[dbconnect_utils.PROCESSNAME_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.PORT_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.IP_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.PATH_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.VERSION_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.STATUS_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.COMMANDLINE_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.USER_INDEX]) == 0: logger.debug('Unable to add ', (procToPortDict[pidsplit])[dbconnect_utils.PROCESSNAME_INDEX]) else: # add key to list of keys to delete later as we've updated them with new entries. keydeletion.append(pidsplit) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: ' + lsofStr) # delete keys which are of no value anymore - Daniel La for key in keydeletion: logger.debug('key is: ', key) if procToPortDict.has_key(key): logger.debug('deleted key: ', key, ' process: ', (procToPortDict[key])[dbconnect_utils.PROCESSNAME_INDEX]) del procToPortDict[key] except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass else: ## Try using netstat and KDB try: pidToPortMap = {} ## We need a local pid to port map netstatLisCmd = 'netstat -Aanf inet | grep "LISTEN"' if USE_SUDO == 'true': netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput(netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() # m = re.search('(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+)\s+\S+', nsLine) m = re.search('(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+).*', nsLine) if (m): ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pid = getAIXpIDfromAddress(localClient, m.group(1).strip(), USE_SUDO) if pid != None: pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnHPUX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd('ps -ef') psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes!') return None for psLine in psLines: ## Reg for processes with args res = re.search('(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if(res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search('(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\-+:/]+)', psLine) if(res): cleanArgs = '' if(res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1).strip() cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes: <%s>' % excInfo) return None ## Use LSOF to map each process to a port and create a dictionary ############################################ try: pidToPortMap = {} ## We need a local pid to port map # lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' lsofCmd = '/usr/local/bin/lsof -n -P -i | grep -i listen 2>/dev/null' # need to specify fullpath - Daniel La if USE_SUDO == 'true': lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) <1: continue lsofLine = lsofLine.strip() m = re.search('\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnSolaris(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris]') procToPortDict = {} ## Get process OSHs ############################################ try: psCmd = 'ps -e -o pid -o uid -o user -o time -o args' if USE_SUDO == 'true': psCmd = 'sudo ' + psCmd psOut = localClient.executeCmd(psCmd) psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes!') return None for line in psLines: line = line.strip() token=line.split(None,4) # Some checks to make sure line is valid if (len(token) != 5): continue if (not re.search('^\d+$',token[0])): continue if (len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex+1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' cleanPath = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath cleanPath = '' else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanPath = res2.group(1) cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes: <%s>' % excInfo) return None ## Use PFILES to map each process to a port and create a dictionary ############################################ try: for pID in procToPortDict.keys(): pFilesCmd = 'pfiles ' + pID + ' 2>/dev/null | grep "sockname: AF_INET"' if USE_SUDO == 'true': pFilesCmd = 'sudo ' + pFilesCmd pFilesStr = localClient.executeCmd(pFilesCmd) if len(pFilesStr) <1: continue pFilesLines = dbconnect_utils.splitCommandOutput(pFilesStr.strip()) if pFilesLines == None: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Error: Invalid output from pfiles: ' + pFilesStr) continue for pFilesLine in pFilesLines: pFilesLine = pFilesLine.strip() m = re.search('.+AF_INET\s+(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if re.search('AFINET6', pFilesLine): m = re.search('.+AF_INET6.*:(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if (m) and m.group(2) != '0': ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pID])[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pID])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pID])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] No TCP port associated with PID [' + pID + ']: ' + pFilesLine) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to make a process to port map using pfiles: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnWindows(localClient, localFramework): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows]') procToPortDict = {} shell = shellutils.ShellUtils(localClient) ntcmdErrStr = 'Remote command returned 1(0x1)' HOST_IP = localClient.getIpAddress() HOSTID = localFramework.getDestinationAttribute('hostId') ## Get process OSHs using NTCMD HR script ############################################ try: processOSHV = ObjectStateHolderVector() if (NTCMD_HR_Dis_Process_Lib.discoverProcessesByWmic( shell, processOSHV, HOSTID, localFramework)) == 1 or ( NTCMD_HR_Dis_Process_Lib.discoverProcesses( shell, processOSHV, HOSTID, localFramework)) == 1: ## We have an OSHV, extract OSHs from it oshvIndex = 0 try: while processOSHV.get(oshvIndex): someOSH = processOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'process': processDict = dbconnect_utils.getAttributeValuesFromOSH( someOSH, [ 'process_pid', 'data_name', 'process_cmdline', 'process_path' ]) ## Name processName = processDict['data_name'] if processName == None or processName == '' or len( processName) < 1: ## We don't care about nameless processes continue pid = processDict['process_pid'] ## PID processPath = processDict['process_path'] ## Path processPath = string.replace(processPath, '"', '') processCmdline = processDict[ 'process_cmdline'] ## Command line processCmdline = string.replace( processCmdline, '"', '') ## Add this to the dictionary dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got PROCESS <%s:%s> with path <%s> and command line <%s>' % (pid, processName, processPath, processCmdline) ) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, processName, 'Running', processPath, processCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting process CIs from OSHV. Ignoring because this is expected...' ) pass else: ## We don't have an OSHV dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes' ) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes: <%s>' % excInfo) pass ## Add windows services to the list of processes ############################################ ## First try WMIC because we can get a PID ## If that doesn't work, fallback on the OOTB NTCMD HR script try: buffer = shell.execCmd( 'wmic service get displayname, pathname, processid, started /format:csv < %SystemRoot%\win.ini' ) dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Output for wmic process command: %s' % buffer) reg_mamRc = shell.getLastCmdReturnCode() if (reg_mamRc == 0): ## WMIC worked!! wmicLines = buffer.split('\n') fakePid = 0 # Each line: HOSTNAME,SERVICENAME,EXE-PATH,PID for wmicLine in wmicLines: tokens = wmicLine.split(',') numTokens = len(tokens) if (tokens == None) or (numTokens < 1): continue if tokens[0].strip() == 'Node': continue if (numTokens < 4): continue serviceName = tokens[numTokens - 4].strip() serviceStatus = dbconnect_utils.UNKNOWN if tokens[numTokens - 1].strip().lower() == 'true': serviceStatus = 'Running' else: serviceStatus = 'Not Running' pid = tokens[numTokens - 2].strip() ## Don't bother with SYSTEM services that have a pid of -1 if (pid != '-1' and pid.isnumeric()): # Get the command line serviceCmdline = tokens[numTokens - 3].strip() serviceCmdline = serviceCmdline.strip()[0:2499] serviceCmdline = string.replace( serviceCmdline, '"', '') # Set process path to command line servicePath = serviceCmdline ## While using services, we sometimes need a fake PID because ## the service may not be running and the corresponding PID will be 0 if pid == '0': pid = 'SERVICE ' + str(fakePid) fakePid = fakePid + 1 dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Using fake PID <%s> for service <%s>' % (pid, serviceName)) ## Got everything, make the array dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got SERVICE <%s (%s)> with PID <%s>, command line <%s>, command path <%s>' % (serviceName, serviceStatus, pid, serviceCmdline, servicePath)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) else: ## WMIC didn't work. Get service OSHs using NTCMD HR script dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] WMIC didn\'t work, trying NTCMD HR script' ) servicesOSHV = NTCMD_HR_REG_Service_Lib.doService( shell, modeling.createHostOSH(HOST_IP)) ## Extract OSHs from vector oshvIndex = 0 try: while servicesOSHV.get(oshvIndex): someOSH = servicesOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'service': serviceDict = dbconnect_utils.getAttributeValuesFromOSH( someOSH, [ 'data_name', 'service_pathtoexec', 'service_commandline', 'service_operatingstatus' ]) ## Name serviceName = serviceDict['data_name'] if serviceName == None or serviceName == '' or len( serviceName) < 1: ## We don't care about nameless services continue pid = 'SERVICE ' + str(oshvIndex) ## PID (fake) servicePath = serviceDict[ 'service_pathtoexec'] ## Install path servicePath = string.replace(servicePath, '"', '') serviceCmdline = serviceDict[ 'service_commandline'] ## Command line serviceCmdline = string.replace( serviceCmdline, '"', '') serviceStatus = serviceDict[ 'service_operatingstatus'] ## Status ## Add this to the dictionary dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and commandline <%s>' % (pid, serviceName, servicePath, serviceCmdline) ) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting service CIs from OSHV. Ignoring because this is expected...' ) pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of services: <%s>' % excInfo) pass ## Add installed software to the list of processes ############################################ try: ## Get installed software OSHs using NTCMD HR script softwareOSHV = ObjectStateHolderVector() gotSoftwareOshs = NTCMD_HR_REG_Software_Lib.doSoftware( shell, modeling.createHostOSH(HOST_IP), softwareOSHV) ## Extract OSHs from vector oshvIndex = 0 while gotSoftwareOshs and softwareOSHV.get(oshvIndex): someOSH = softwareOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'software': softwareDict = dbconnect_utils.getAttributeValuesFromOSH( someOSH, [ 'data_name', 'software_installpath', 'software_version' ]) ## Name softwareName = softwareDict['data_name'] if not softwareName: ## We don't care about nameless software continue pid = 'SOFTWARE ' + str(oshvIndex) ## PID (fake) softwareInstallPath = softwareDict[ 'software_installpath'] ## Install path softwareVersion = softwareDict[ 'software_version'] ## Version ## Add this to the dictionary dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and version <%s>' % (pid, softwareName, softwareInstallPath, softwareVersion)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SOFTWARE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, softwareName, dbconnect_utils.UNKNOWN, softwareInstallPath, dbconnect_utils.UNKNOWN)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting software CIs from OSHV. Ignoring because this is expected...' ) pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of software: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ try: netstatLisStr = shell.execCmd( 'netstat -aon -p tcp | find "LISTENING"') nsStrOk = 'false' nsLisLines = None if netstatLisStr.find(ntcmdErrStr) != -1 or len(netstatLisStr) < 1: nsStrOk = 'false' elif re.search('\r\n', netstatLisStr): nsLisLines = netstatLisStr.split('\r\n') nsStrOk = 'true' elif re.search('\n', netstatLisStr): nsLisLines = netstatLisStr.split('\n') nsStrOk = 'true' if nsStrOk == 'true': for line in nsLisLines: line = line.strip() m = re.search( '\S+\s+(\d+.\d+.\d+.\d+):(\d+)\s+\d+.\d+.\d+.\d+:\d+\s+\S+\s+(\d+).*', line) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3) dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) if pid != '-' and procToPortDict.has_key(pid): dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid])[0])) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) else: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Couldn\'t get process information (Most likely due to lack of user permissions): ' + line) else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning EMPTY process to port dictionary' ) return None except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Exception: <%s>' % excInfo) pass
def registryLookup(procToPortDict, dbInstanceDict, localClient, wmiRegistryClient): try: # Store all found listening Port activeListenerPorts = [] for pid in procToPortDict.keys(): activeListenerPorts.append((procToPortDict[pid])[dbconnect_utils.PORT_INDEX]) ## Locals logger.debug('Initial dbInstanceDict %s' % dbInstanceDict) instanceNameList = [] installNameTointernalInstanceName = {} # If SQL Server is present on this box, get instance names installedInstancesKeypath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server' installedInstances = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, installedInstancesKeypath, 'InstalledInstances') if installedInstances == None or str(installedInstances) == '[[], []]' or str(installedInstances) == '{}': if dbInstanceDict != None and len(dbInstanceDict) > 0: instancesString = '' for dbName in dbInstanceDict.keys(): instancesString = instancesString + dbName.upper() + '\n' installedInstances = {} installedInstances.update({installedInstancesKeypath:instancesString[:-1]}) # chop last \n else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':registryLookup] SQL Server not installed on this box') return None logger.debug("Discovered installed instances %s" % installedInstances) if installedInstances: ## We have SQL Server dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] SQL Server present on this box <%s>' % installedInstances) installedInstanceNames = installedInstances[installedInstancesKeypath] if installedInstanceNames.find('\n') > 0 or installedInstanceNames.find(' _ ') > 0: ## Multiple SQL Server instances installedIstanceNameList = re.split(' _ |\n', installedInstanceNames) else: installedIstanceNameList = [installedInstanceNames] logger.debug('Installed instance name list %s' % installedIstanceNameList) for installedInstanceName in installedIstanceNameList: instanceNameList.append(installedInstanceName.strip()) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Found SQL Server instance <%s>' % installedInstanceName.strip()) internalInstanceNameKeyPath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL' internalInstanceNameDict = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, internalInstanceNameKeyPath, installedInstanceName) internalInstanceName = internalInstanceNameDict[internalInstanceNameKeyPath] if internalInstanceName: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Found registry name <%s> for internal SQL instance name <%s>' % (internalInstanceName, installedInstanceName)) installNameTointernalInstanceName[installedInstanceName.strip()] = internalInstanceName.strip() else: installNameTointernalInstanceName[installedInstanceName.strip()] = installedInstanceName.strip() logger.debug("installNameTointernalInstanceName %s" % installNameTointernalInstanceName) logger.debug("instanceNameList %s " % instanceNameList) # If we're here, one or more SQL Server instances are present # Look for additional SQL Server information sqlServerDetailKeypaths = ['SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\SuperSocketNetLib\\Tcp', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Setup', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\CurrentVersion', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster'] sqlServerDetailFilters = ['TcpPort', 'SQLPath', 'CurrentVersion', 'ClusterIpAddr', 'ClusterName'] for instanceName in instanceNameList: sqlServerDetailValues = [] for sqlServerDetailIndex in range(len(sqlServerDetailKeypaths)): sqlServerDetailKeypath = '' ## Replace instance names in registry key path as appropriate if instanceName == 'MSSQLSERVER': if sqlServerDetailKeypaths[sqlServerDetailIndex].find('luster') > 0: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get(instanceName)) else: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'Microsoft SQL Server\\iNsTaNcEnAmE', 'MSSQLServer') else: if sqlServerDetailKeypaths[sqlServerDetailIndex].find('luster') > 0: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get(instanceName)) else: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', instanceName) regValues = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex]) if regValues == None or str(regValues) == '[[], []]' or str(regValues) == '{}': dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got nothing for key <%s> with filter <%s>' % (sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) sqlServerDetailValues.insert(sqlServerDetailIndex, None) else: sqlServerDetailValues.insert(sqlServerDetailIndex, regValues[sqlServerDetailKeypath]) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got value <%s> for key <%s> with filter <%s>' % (sqlServerDetailValues[sqlServerDetailIndex], sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) logger.debug("instanceNameList %s " % instanceNameList) ## We should have all details for this instance now - add it to DB dictionary listenerPort = sqlServerDetailValues[0] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got port <%s> for instance <%s>' % (listenerPort, instanceName)) installPath = sqlServerDetailValues[1] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got path <%s> for instance <%s>' % (installPath, instanceName)) version = sqlServerDetailValues[2] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got version <%s> for instance <%s>' % (version, instanceName)) ipAddress = dbconnect_utils.fixIP(sqlServerDetailValues[3], localClient.getIpAddress()) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got IP <%s> for instance <%s>' % (ipAddress, instanceName)) clusterName = sqlServerDetailValues[4] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got Cluster Name <%s> for instance <%s>' % (clusterName, instanceName)) if clusterName: clusterIp = netutils.getHostAddress(clusterName) if clusterIp and netutils.isValidIp(clusterIp): ipAddress = clusterIp ## If the instance is already in the DB dict, don't overwrite all values if instanceName == 'MSSQLSERVER': dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got unnamed SQL Server instance') instanceName = dbconnect_utils.getServerName(localClient) else: instanceName = dbconnect_utils.getServerName(localClient) + '\\' + instanceName.lower() installPath = installPath.lower() if instanceName in dbInstanceDict.keys(): statusFlag = (dbInstanceDict[instanceName])[dbconnect_utils.STATUS_INDEX] # If port is already populated, don't overwrite it because # port number information from active processes (above) is # guaranteed to be correct and the registry may not be up-to-date if (dbInstanceDict[instanceName])[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: if listenerPort not in activeListenerPorts: listenerPort = (dbInstanceDict[instanceName])[dbconnect_utils.PORT_INDEX] dbInstanceDict[instanceName] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Found known SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) else: dbInstanceDict[instanceName] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, dbconnect_utils.UNKNOWN] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Added SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) logger.debug("instanceNameList %s " % instanceNameList) logger.debug("dbInstanceDict %s" % dbInstanceDict) ## Replace dictionary entry of serverName\sqlInstanceName with clusterName\sqlInstanceName if clusterName and instanceName in dbInstanceDict.keys(): if instanceName.find('\\') > 0 : newInstanceName = clusterName + '\\' + instanceName[instanceName.find('\\')+1:] else: newInstanceName = clusterName dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Replacing SQL Server instance <%s> with <%s> because it is part of a cluster' % (instanceName, newInstanceName)) dbInstanceDict[newInstanceName] = dbInstanceDict[instanceName] del dbInstanceDict[instanceName] logger.debug("dbInstanceDict %s" % dbInstanceDict) #print dbInstanceDict logger.debug("instanceNameList %s " % instanceNameList) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':registryLookup] Exception: <%s>' % excInfo) pass
def registryLookup(procToPortDict, dbInstanceDict, localClient, wmiRegistryClient): try: # Store all found listening Port activeListenerPorts = [] for pid in procToPortDict.keys(): activeListenerPorts.append( (procToPortDict[pid])[dbconnect_utils.PORT_INDEX]) ## Locals logger.debug('Initial dbInstanceDict %s' % dbInstanceDict) instanceNameList = [] installNameTointernalInstanceName = {} # If SQL Server is present on this box, get instance names installedInstancesKeypath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server' installedInstances = dbconnect_utils.getRegValues( localClient, wmiRegistryClient, installedInstancesKeypath, 'InstalledInstances') if installedInstances == None or str( installedInstances) == '[[], []]' or str( installedInstances) == '{}': if dbInstanceDict != None and len(dbInstanceDict) > 0: instancesString = '' for dbName in dbInstanceDict.keys(): instancesString = instancesString + dbName.upper() + '\n' installedInstances = {} installedInstances.update( {installedInstancesKeypath: instancesString[:-1]}) # chop last \n else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':registryLookup] SQL Server not installed on this box') return None logger.debug("Discovered installed instances %s" % installedInstances) if installedInstances: ## We have SQL Server dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] SQL Server present on this box <%s>' % installedInstances) installedInstanceNames = installedInstances[ installedInstancesKeypath] if installedInstanceNames.find( '\n') > 0 or installedInstanceNames.find(' _ ') > 0: ## Multiple SQL Server instances installedIstanceNameList = re.split(' _ |\n', installedInstanceNames) else: installedIstanceNameList = [installedInstanceNames] logger.debug('Installed instance name list %s' % installedIstanceNameList) for installedInstanceName in installedIstanceNameList: instanceNameList.append(installedInstanceName.strip()) dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Found SQL Server instance <%s>' % installedInstanceName.strip()) internalInstanceNameKeyPath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL' internalInstanceNameDict = dbconnect_utils.getRegValues( localClient, wmiRegistryClient, internalInstanceNameKeyPath, installedInstanceName) internalInstanceName = internalInstanceNameDict[ internalInstanceNameKeyPath] if internalInstanceName: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Found registry name <%s> for internal SQL instance name <%s>' % (internalInstanceName, installedInstanceName)) installNameTointernalInstanceName[ installedInstanceName.strip( )] = internalInstanceName.strip() else: installNameTointernalInstanceName[ installedInstanceName.strip( )] = installedInstanceName.strip() logger.debug("installNameTointernalInstanceName %s" % installNameTointernalInstanceName) logger.debug("instanceNameList %s " % instanceNameList) # If we're here, one or more SQL Server instances are present # Look for additional SQL Server information sqlServerDetailKeypaths = [ 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\SuperSocketNetLib\\Tcp', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Setup', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\CurrentVersion', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster' ] sqlServerDetailFilters = [ 'TcpPort', 'SQLPath', 'CurrentVersion', 'ClusterIpAddr', 'ClusterName' ] for instanceName in instanceNameList: sqlServerDetailValues = [] for sqlServerDetailIndex in range(len(sqlServerDetailKeypaths)): sqlServerDetailKeypath = '' ## Replace instance names in registry key path as appropriate if instanceName == 'MSSQLSERVER': if sqlServerDetailKeypaths[sqlServerDetailIndex].find( 'luster') > 0: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get( instanceName)) else: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'Microsoft SQL Server\\iNsTaNcEnAmE', 'MSSQLServer') else: if sqlServerDetailKeypaths[sqlServerDetailIndex].find( 'luster') > 0: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get( instanceName)) else: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', instanceName) regValues = dbconnect_utils.getRegValues( localClient, wmiRegistryClient, sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex]) if regValues == None or str(regValues) == '[[], []]' or str( regValues) == '{}': dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got nothing for key <%s> with filter <%s>' % (sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) sqlServerDetailValues.insert(sqlServerDetailIndex, None) else: sqlServerDetailValues.insert( sqlServerDetailIndex, regValues[sqlServerDetailKeypath]) dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got value <%s> for key <%s> with filter <%s>' % (sqlServerDetailValues[sqlServerDetailIndex], sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) logger.debug("instanceNameList %s " % instanceNameList) ## We should have all details for this instance now - add it to DB dictionary listenerPort = sqlServerDetailValues[0] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got port <%s> for instance <%s>' % (listenerPort, instanceName)) installPath = sqlServerDetailValues[1] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got path <%s> for instance <%s>' % (installPath, instanceName)) version = sqlServerDetailValues[2] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got version <%s> for instance <%s>' % (version, instanceName)) ipAddress = dbconnect_utils.fixIP(sqlServerDetailValues[3], localClient.getIpAddress()) dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got IP <%s> for instance <%s>' % (ipAddress, instanceName)) clusterName = sqlServerDetailValues[4] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got Cluster Name <%s> for instance <%s>' % (clusterName, instanceName)) if clusterName: clusterIp = netutils.getHostAddress(clusterName) if clusterIp and netutils.isValidIp(clusterIp): ipAddress = clusterIp ## If the instance is already in the DB dict, don't overwrite all values if instanceName == 'MSSQLSERVER': dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got unnamed SQL Server instance') instanceName = dbconnect_utils.getServerName(localClient) else: instanceName = dbconnect_utils.getServerName( localClient) + '\\' + instanceName.lower() installPath = installPath.lower() if instanceName in dbInstanceDict.keys(): statusFlag = ( dbInstanceDict[instanceName])[dbconnect_utils.STATUS_INDEX] # If port is already populated, don't overwrite it because # port number information from active processes (above) is # guaranteed to be correct and the registry may not be up-to-date if (dbInstanceDict[instanceName] )[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: if listenerPort not in activeListenerPorts: listenerPort = (dbInstanceDict[instanceName] )[dbconnect_utils.PORT_INDEX] dbInstanceDict[instanceName] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag ] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Found known SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) else: dbInstanceDict[instanceName] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, dbconnect_utils.UNKNOWN ] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Added SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) logger.debug("instanceNameList %s " % instanceNameList) logger.debug("dbInstanceDict %s" % dbInstanceDict) ## Replace dictionary entry of serverName\sqlInstanceName with clusterName\sqlInstanceName if clusterName and instanceName in dbInstanceDict.keys(): if instanceName.find('\\') > 0: newInstanceName = clusterName + '\\' + instanceName[ instanceName.find('\\') + 1:] else: newInstanceName = clusterName dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Replacing SQL Server instance <%s> with <%s> because it is part of a cluster' % (instanceName, newInstanceName)) dbInstanceDict[newInstanceName] = dbInstanceDict[instanceName] del dbInstanceDict[instanceName] logger.debug("dbInstanceDict %s" % dbInstanceDict) #print dbInstanceDict logger.debug("instanceNameList %s " % instanceNameList) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':registryLookup] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnAIX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd("ps -e -o 'user,pid,time,args'") psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes!' ) return None for psLine in psLines: if (re.search('TIME COMMAND', psLine)): continue ## Reg for processes with args res = re.search( '(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if (res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search( '(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)', psLine) if (res): cleanArgs = '' if (res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1) cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine) ) if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes: <%s>' % excInfo) pass if USE_LSOF: ## Use LSOF to map each process to a port and create a dictionary ############################################ try: pidToPortMap = {} ## We need a local pid to port map lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' if USE_SUDO: lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) < 1: continue lsofLine = lsofLine.strip() m = re.search( '\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass else: ## Try using netstat and KDB try: pidToPortMap = {} ## We need a local pid to port map netstatLisCmd = 'netstat -Aanf inet | grep "LISTEN"' if USE_SUDO: netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput( netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() # m = re.search('(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+)\s+\S+', nsLine) m = re.search( '(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+).*', nsLine) if (m): ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pid = getAIXpIDfromAddress(localClient, m.group(1).strip(), USE_SUDO) if pid != None: pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning EMPTY process to port dictionary' ) return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnLinux(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd( 'ps -e -o pid -o uid -o user -o cputime -o command --cols 4000' ) if psOut == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!' ) return None psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!' ) return None for psLine in psLines: psLine = psLine.strip() token = psLine.split(None, 4) # Some checks to make sure line is valid if (len(token) != 5): continue if (not re.search('^\d+$', token[0])): continue if (len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex + 1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ try: netstatLisCmd = 'netstat -anp | grep "LISTEN"' if USE_SUDO: netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput( netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got nsLine <%s>' % nsLine) m = re.search('tcp.* (\S+):(\d+).*:.*\s+(\d+|-).*', nsLine) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3).strip() dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got port <%s> for pid <%s>' % (serverPort, pid)) if pid != '-' and procToPortDict.has_key(pid): dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid] )[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Couldn\'t get netstat information (Most likely due to lack of user permissions): ' + nsLine) else: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning EMPTY process to port dictionary' ) return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnWindows(localClient, localFramework): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows]') procToPortDict = {} shell = shellutils.ShellUtils(localClient) ntcmdErrStr = 'Remote command returned 1(0x1)' HOST_IP = localClient.getIpAddress() HOSTID = localFramework.getDestinationAttribute('hostId') ## Get process OSHs using NTCMD HR script ############################################ try: processOSHV = ObjectStateHolderVector() if (NTCMD_HR_Dis_Process_Lib.discoverProcessesByWmic(shell, processOSHV, HOSTID, localFramework)) == 1 or (NTCMD_HR_Dis_Process_Lib.discoverProcesses(shell, processOSHV, HOSTID, localFramework)) == 1: ## We have an OSHV, extract OSHs from it oshvIndex = 0 try: while processOSHV.get(oshvIndex): someOSH = processOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'process': processDict = dbconnect_utils.getAttributeValuesFromOSH(someOSH, ['process_pid', 'data_name', 'process_cmdline', 'process_path']) ## Name processName = processDict['data_name'] if processName == None or processName == '' or len(processName) <1: ## We don't care about nameless processes continue pid = processDict['process_pid'] ## PID processPath = processDict['process_path'] ## Path processPath = string.replace(processPath, '"', '') processCmdline = processDict['process_cmdline'] ## Command line processCmdline = string.replace(processCmdline, '"', '') ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got PROCESS <%s:%s> with path <%s> and command line <%s>' % (pid, processName, processPath, processCmdline)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, processName, 'Running', processPath, processCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting process CIs from OSHV. Ignoring because this is expected...') pass else: ## We don't have an OSHV dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes') except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes: <%s>' % excInfo) pass ## Add windows services to the list of processes ############################################ ## First try WMIC because we can get a PID ## If that doesn't work, fallback on the OOTB NTCMD HR script try: buffer = shell.execCmd('wmic service get displayname, pathname, processid, started /format:csv < %SystemRoot%\win.ini') dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Output for wmic process command: %s' % buffer) reg_mamRc = shell.getLastCmdReturnCode() if (reg_mamRc == 0): ## WMIC worked!! wmicLines = buffer.split('\n') fakePid = 0 # Each line: HOSTNAME,SERVICENAME,EXE-PATH,PID for wmicLine in wmicLines: tokens = wmicLine.split(',') numTokens = len(tokens) if (tokens == None) or (numTokens < 1) : continue if tokens[0].strip() == 'Node': continue if (numTokens < 4): continue serviceName = tokens[numTokens - 4].strip() serviceStatus = dbconnect_utils.UNKNOWN if tokens[numTokens - 1].strip().lower() == 'true': serviceStatus = 'Running' else: serviceStatus = 'Not Running' pid = tokens[numTokens - 2].strip() ## Don't bother with SYSTEM services that have a pid of -1 if(pid != '-1' and pid.isnumeric()): # Get the command line serviceCmdline = tokens[numTokens - 3].strip() serviceCmdline = serviceCmdline.strip()[0:2499] serviceCmdline = string.replace(serviceCmdline, '"', '') # Set process path to command line servicePath = serviceCmdline ## While using services, we sometimes need a fake PID because ## the service may not be running and the corresponding PID will be 0 if pid == '0': pid = 'SERVICE ' + str(fakePid) fakePid = fakePid + 1 dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Using fake PID <%s> for service <%s>' % (pid, serviceName)) ## Got everything, make the array dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got SERVICE <%s (%s)> with PID <%s>, command line <%s>, command path <%s>' % (serviceName, serviceStatus, pid, serviceCmdline, servicePath)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) else: ## WMIC didn't work. Get service OSHs using NTCMD HR script dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] WMIC didn\'t work, trying NTCMD HR script') servicesOSHV = NTCMD_HR_REG_Service_Lib.doService(shell, modeling.createHostOSH(HOST_IP)) ## Extract OSHs from vector oshvIndex = 0 try: while servicesOSHV.get(oshvIndex): someOSH = servicesOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'service': serviceDict = dbconnect_utils.getAttributeValuesFromOSH(someOSH, ['data_name', 'service_pathtoexec', 'service_commandline', 'service_operatingstatus']) ## Name serviceName = serviceDict['data_name'] if serviceName == None or serviceName == '' or len(serviceName) <1: ## We don't care about nameless services continue pid = 'SERVICE ' + str(oshvIndex) ## PID (fake) servicePath = serviceDict['service_pathtoexec'] ## Install path servicePath = string.replace(servicePath, '"', '') serviceCmdline = serviceDict['service_commandline'] ## Command line serviceCmdline = string.replace(serviceCmdline, '"', '') serviceStatus = serviceDict['service_operatingstatus'] ## Status if serviceStatus == 'true': serviceStatus = 'Running' else: serviceStatus = 'Not Running' ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and commandline <%s>' % (pid, serviceName, servicePath, serviceCmdline)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting service CIs from OSHV. Ignoring because this is expected...') pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of services: <%s>' % excInfo) pass ## Add installed software to the list of processes ############################################ try: ## Get installed software OSHs using NTCMD HR script softwareOSHV = ObjectStateHolderVector() gotSoftwareOshs = NTCMD_HR_REG_Software_Lib.doSoftware(shell, modeling.createHostOSH(HOST_IP), softwareOSHV) ## Extract OSHs from vector oshvIndex = 0 while gotSoftwareOshs and softwareOSHV.get(oshvIndex): someOSH = softwareOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'software': softwareDict = dbconnect_utils.getAttributeValuesFromOSH(someOSH, ['data_name', 'software_installpath', 'software_version']) ## Name softwareName = softwareDict['data_name'] if not softwareName: ## We don't care about nameless software continue pid = 'SOFTWARE ' + str(oshvIndex) ## PID (fake) softwareInstallPath = softwareDict['software_installpath'] ## Install path softwareVersion = softwareDict['software_version'] ## Version ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and version <%s>' % (pid, softwareName, softwareInstallPath, softwareVersion)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SOFTWARE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, softwareName, dbconnect_utils.UNKNOWN, softwareInstallPath, dbconnect_utils.UNKNOWN)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting software CIs from OSHV. Ignoring because this is expected...') pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of software: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ ## Updated by Daniel La: updated to handle situation where a process can listen on multiple ports. try: keydeletion = [] # keys to delete - Daniel La netstatLisStr = shell.execCmd('netstat -aon -p tcp | find "LISTENING"') nsStrOk = 'false' nsLisLines = None if netstatLisStr.find(ntcmdErrStr) != -1 or len(netstatLisStr) < 1: nsStrOk = 'false' elif re.search('\r\n', netstatLisStr): nsLisLines = netstatLisStr.split('\r\n') nsStrOk = 'true' elif re.search('\n', netstatLisStr): nsLisLines = netstatLisStr.split('\n') nsStrOk = 'true' if nsStrOk == 'true': for line in nsLisLines: line = line.strip() m = re.search('\S+\s+(\d+.\d+.\d+.\d+):(\d+)\s+\d+.\d+.\d+.\d+:\d+\s+\S+\s+(\d+).*', line) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) # modified if statement slightly to include check to see if listener is listening on port 80, 2481, or 2482. If so, these can be ignored. - Daniel La if pid != '-' and procToPortDict.has_key(pid) and not (serverPort in ['80', '2481', '2482'] and re.search('tns', (procToPortDict[pid])[0].lower())): dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid])[0])) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort # create new key and add new entry - Daniel La pidnport = str(pid) + '.' + str(serverPort) if dbconnect_utils.populateProcToPortDict(procToPortDict, pidnport, (procToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX], (procToPortDict[pid])[dbconnect_utils.PORT_INDEX], (procToPortDict[pid])[dbconnect_utils.IP_INDEX], (procToPortDict[pid])[dbconnect_utils.PATH_INDEX], (procToPortDict[pid])[dbconnect_utils.VERSION_INDEX], (procToPortDict[pid])[dbconnect_utils.STATUS_INDEX], (procToPortDict[pid])[dbconnect_utils.COMMANDLINE_INDEX], (procToPortDict[pid])[dbconnect_utils.USER_INDEX]) == 0: logger.debug('Unable to add ', (procToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX]) else: # add key to list of keys to delete later as we've updated them with new entries. keydeletion.append(pid) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Couldn\'t get process information (Most likely due to lack of user permissions): ' + line) # delete keys which are of no value anymore - Daniel La for key in keydeletion: if procToPortDict.has_key(key): del procToPortDict[key] logger.debug('deleted key: ', key) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning EMPTY process to port dictionary') return None except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Exception: <%s>' % excInfo) pass
def findTnsnamesOra(localClient, p2pDict, dbDict, isWindows, installLocs): try: ## Locals searchLocations = [] # Try locations in the database dictionary if len(dbDict) > 0: logger.debug('dbDict locations') for sid in dbDict.keys(): if (dbDict[sid])[dbconnect_utils.PATH_INDEX] != dbconnect_utils.UNKNOWN: path = (dbDict[sid])[dbconnect_utils.PATH_INDEX].lower() if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> is not a valid path' % path) continue # Try locations in the p2p dictionary if len(p2pDict) > 0: logger.debug('p2pDict locations') for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX].lower() if re.search('tns', processName) or re.search('dbconsole', processName) or re.search('jobscheduler', processName) or re.search('oradb', processName) or re.search('oracle', processName) or re.search('ora_', processName): ## Remove /bin/tnslsnr from TNS process path if re.search('/bin/tnslsnr', path): path = path[:path.find('/bin/tnslsnr')] if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> is not a valid path' % path) continue # If we have no search locations so far, try some known/standard ones if 1: #len(searchLocations) < 1: if isWindows == 'true': logger.debug('additional locations') searchLocations.append('%HOMEDRIVE%\oracle') searchLocations.append('%SYSTEMDRIVE%\oracle') searchLocations.append('%PROGRAMFILES%\oracle') searchLocations.append('%PROGRAMFILES(x86)%\oracle') searchLocations.append('%ORA_HOME%') searchLocations.append('%ORACLE_HOME%') #searchLocations.append('%ORACLE_HOME%\\network\\admin') else: searchLocations.append('/u01') searchLocations.append('/u02') searchLocations.append('/opt') searchLocations.append('/usr/local') searchLocations.append('$ORACLE_HOME') #searchLocations.append('$ORACLE_HOME/network/admin') searchLocations.append('$ORA_HOME') searchLocations.append('$ORACLE_BASE') # Add oracle paths found from other sources if installLocs and len(installLocs) > 0: logger.debug('other locations') for installLoc in installLocs: if installLoc and len(installLoc) > 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [3] Adding <%s> to search locations' % installLoc) searchLocations.append(installLoc) logger.debug('list of locations...') for location in searchLocations: logger.debug('*** location: ', location) # Search filesystem and parse tnsnames.ora entries for location in searchLocations: tnsnamesLocations = dbconnect_utils.findFile(localClient, 'tnsnames.ora', location, isWindows) if tnsnamesLocations == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] No tnsnames.ora found in <%s>' % location) continue for tnsnamesLocation in tnsnamesLocations: # We don't want the sample TNSNAMES.ORA which is # installed by default if re.search('sample', tnsnamesLocation.lower()): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping sample tnsnames.ora in <%s>' % tnsnamesLocation) continue tnsnamesContent = dbconnect_utils.getFileContent(localClient, tnsnamesLocation, isWindows) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Got content of <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) if tnsnamesContent != None or tnsnamesContent != '' or len(tnsnamesContent) <1: tnsEntries = dbutils.parseTNSNames(tnsnamesContent, '') for tnsEntry in tnsEntries: sidFound = tnsEntry[3].strip().lower() ## Truncate domain name if this is fully qualified SID if sidFound.find('.') > 0: shortSID = sidFound[:sidFound.find('.')] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Stripping domain from SID <%s> to <%s>' % (sidFound, shortSID)) sidFound = shortSID tnslsnrPort = tnsEntry[2].strip().lower() ipAddress = dbconnect_utils.fixIP(tnsEntry[5].strip().lower(), localClient.getIpAddress()) if ipAddress == dbconnect_utils.UNKNOWN: dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping instance <%s> listening at port <%s> because it\'s IP address is not valid' % (sidFound, tnslsnrPort)) continue if sidFound in dbDict.keys(): installPath = (dbDict[sidFound])[dbconnect_utils.PATH_INDEX] version = (dbDict[sidFound])[dbconnect_utils.VERSION_INDEX] statusFlag = (dbDict[sidFound])[dbconnect_utils.STATUS_INDEX] # If port and IP are already populated, don't overwrite them # because this information from active processes (above) is # guaranteed to be correct and tnsnames.ora may not be up-to-date ## Vinay 01/04/2010 - Commenting out conditional update below ## because the port and IP for an Oracle instance is not on the Oracle ## process but on the TNS listener process which may be listening for ## multiple instances on different ports. This makes associating an ## Oracle instance to its corresponding port impossible. ## So any ports found in TNSnames.ora will be used to overwrite ## previously found ports # if (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: # tnslsnrPort = (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] # if (dbDict[sidFound])[dbconnect_utils.IP_INDEX] != dbconnect_utils.UNKNOWN: # ipAddress = (dbDict[sidFound])[dbconnect_utils.IP_INDEX] dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, installPath, version, statusFlag] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Found known Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) # Don't want to add non running databases - Daniel La # else: # dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Added Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) else: logger.debug('[' + SCRIPT_NAME + ':findTnsnamesOra] Invalid TNSNAMES.ORA at <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Exception: <%s>' % excInfo) pass
def findTnsnamesOra(localClient, p2pDict, dbDict, isWindows, installLocs): try: ## Locals searchLocations = [] # Try locations in the database dictionary if len(dbDict) > 0: for sid in dbDict.keys(): if (dbDict[sid])[dbconnect_utils.PATH_INDEX] != dbconnect_utils.UNKNOWN: path = (dbDict[sid])[dbconnect_utils.PATH_INDEX].lower() if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> is not a valid path' % path) continue # Try locations in the p2p dictionary if len(p2pDict) > 0: for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX].lower() if re.search('tns', processName) or re.search('dbconsole', processName) or re.search('jobscheduler', processName) or re.search('oradb', processName) or re.search('oracle', processName) or re.search('ora_', processName): ## Remove /bin/tnslsnr from TNS process path if re.search('/bin/tnslsnr', path): path = path[:path.find('/bin/tnslsnr')] if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> is not a valid path' % path) continue # If we have no search locations so far, try some known/standard ones if 1: #len(searchLocations) < 1: if isWindows == 'true': searchLocations.append('%HOMEDRIVE%\oracle') searchLocations.append('%SYSTEMDRIVE%\oracle') searchLocations.append('%PROGRAMFILES%\oracle') searchLocations.append('%PROGRAMFILES(x86)%\oracle') searchLocations.append('%ORA_HOME%') searchLocations.append('%ORACLE_HOME%') #searchLocations.append('%ORACLE_HOME%\\network\\admin') else: searchLocations.append('/u01') searchLocations.append('/u02') searchLocations.append('/opt') searchLocations.append('/usr/local') searchLocations.append('$ORACLE_HOME') #searchLocations.append('$ORACLE_HOME/network/admin') searchLocations.append('$ORA_HOME') searchLocations.append('$ORACLE_BASE') # Add oracle paths found from other sources if installLocs and len(installLocs) > 0: for installLoc in installLocs: if installLoc and len(installLoc) > 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [3] Adding <%s> to search locations' % installLoc) searchLocations.append(installLoc) # Search filesystem and parse tnsnames.ora entries for location in searchLocations: tnsnamesLocations = dbconnect_utils.findFile(localClient, 'tnsnames.ora', location, isWindows) if tnsnamesLocations == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] No tnsnames.ora found in <%s>' % location) continue for tnsnamesLocation in tnsnamesLocations: # We don't want the sample TNSNAMES.ORA which is # installed by default if re.search('sample', tnsnamesLocation.lower()): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping sample tnsnames.ora in <%s>' % tnsnamesLocation) continue tnsnamesContent = dbconnect_utils.getFileContent(localClient, tnsnamesLocation, isWindows) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Got content of <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) if tnsnamesContent != None or tnsnamesContent != '' or len(tnsnamesContent) <1: tnsEntries = dbutils.parseTNSNames(tnsnamesContent, '') for tnsEntry in tnsEntries: sidFound = tnsEntry[3].strip().lower() ## Truncate domain name if this is fully qualified SID if sidFound.find('.') > 0: shortSID = sidFound[:sidFound.find('.')] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Stripping domain from SID <%s> to <%s>' % (sidFound, shortSID)) sidFound = shortSID tnslsnrPort = tnsEntry[2].strip().lower() ipAddress = dbconnect_utils.fixIP(tnsEntry[5].strip().lower(), localClient.getIpAddress()) if ipAddress == dbconnect_utils.UNKNOWN: dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping instance <%s> listening at port <%s> because it\'s IP address is not valid' % (sidFound, tnslsnrPort)) continue if sidFound in dbDict.keys(): installPath = (dbDict[sidFound])[dbconnect_utils.PATH_INDEX] version = (dbDict[sidFound])[dbconnect_utils.VERSION_INDEX] statusFlag = (dbDict[sidFound])[dbconnect_utils.STATUS_INDEX] # If port and IP are already populated, don't overwrite them # because this information from active processes (above) is # guaranteed to be correct and tnsnames.ora may not be up-to-date ## Vinay 01/04/2010 - Commenting out conditional update below ## because the port and IP for an Oracle instance is not on the Oracle ## process but on the TNS listener process which may be listening for ## multiple instances on different ports. This makes associating an ## Oracle instance to its corresponding port impossible. ## So any ports found in TNSnames.ora will be used to overwrite ## previously found ports # if (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: # tnslsnrPort = (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] # if (dbDict[sidFound])[dbconnect_utils.IP_INDEX] != dbconnect_utils.UNKNOWN: # ipAddress = (dbDict[sidFound])[dbconnect_utils.IP_INDEX] dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, installPath, version, statusFlag] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Found known Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) else: dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Added Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) else: logger.debug('[' + SCRIPT_NAME + ':findTnsnamesOra] Invalid TNSNAMES.ORA at <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Exception: <%s>' % excInfo) pass