def getRangeByNetwork(netAddress, netMask): netAddressInt = convertIpToInt(netAddress) netMaskInt = convertIpToInt(netMask) firstIp = netMaskInt & netAddressInt lastIp = (netutils.negateNetMask(netMaskInt)) | firstIp deviation = 0 if (lastIp - firstIp) > 1: deviation = 1 return Range( IPv4(firstIp + deviation), IPv4(lastIp - deviation)) # cutting off network address and broadcast address
def setObjectVectorByResStringArray(objectVector, ipsResult, virtualMode, netAddress=None, netMask=None): # create new network for link object if netAddress and netMask: networkOSHForLink = modeling.createNetworkOSH(netAddress, netMask) else: networkOSHForLink = None # run on the string array (Holding all live pinged ips) for ipResult in ipsResult: isVirtual = 0 # pingedIp - the ip we sent the ping to # replyIp - the ip replied to the ping # Break the curr result by ':' <Reply-IP>:<Pinged-IP> token = ipResult.split(':') if (len(token) == 2): # In case where we ping a virtual ip we get the reply from the real ip # If we are pinging a virtual ip if virtualMode: replyIp, pingedIp = token[0], token[1] isVirtual = 1 else: replyIp, pingedIp = token[1], token[1] else: replyIp, pingedIp = ipResult, ipResult # Create Ip OSH and add to vector pingedIpOSH = modeling.createIpOSH(ip_addr.IPAddress(pingedIp), netMask) objectVector.add(pingedIpOSH) if networkOSHForLink: # Create MEMBER link and set end1(discovered network) and end2(host) objectVector.add( modeling.createLinkOSH('member', networkOSHForLink, pingedIpOSH)) if isVirtual: # Create Ip OSH replyIpOSH = modeling.createIpOSH(replyIp, netMask) # Create a depend link and set end1(pingedIp) and end2(replyIp) newDependLink = modeling.createLinkOSH('depend', pingedIpOSH, replyIpOSH) objectVector.add(replyIpOSH) objectVector.add(newDependLink) if networkOSHForLink: replyIpNetAddress = IPv4(replyIp, netMask).getFirstIp().toString() if replyIpNetAddress == netAddress: # Create MEMBER link and set end1(discovered network) and end2(host) objectVector.add( modeling.createLinkOSH('member', networkOSHForLink, replyIpOSH))
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() snmpMethod = getFrameworkParameter(Framework, 'snmpMethod', SnmpQueries.defaultSnmpMethod) backupSnmpMethod = getFrameworkParameter(Framework, 'backupSnmpMethod', SnmpQueries.defaultBackupSnmpMethod) moonWalkBulkSize = int(getFrameworkParameter(Framework, 'moonWalkBulkSize', SnmpQueries.defaultMoonWalkBulkSize)) moonWalkSleep = long(getFrameworkParameter(Framework, 'moonWalkSleep', SnmpQueries.defaultMoonWalkSleep)) snmpBulkSize = int(getFrameworkParameter(Framework, 'snmpBulkSize', SnmpQueries.defaultSnmpBulkSize)) discoverUnknownIPs = Boolean.parseBoolean(Framework.getParameter('discoverUnknownIPs')) #getting DestinationData from Framework; the method is protected. destination = Framework.getCurrentDestination() discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) logger.debug('Discover ARP by %s returned %s objects' % (snmpMethod, str(discoveredHostIpList.size()))) if (discoveredHostIpList.size() == 0) and (snmpMethod != backupSnmpMethod): discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) logger.debug('Discover ARP by %s returned %s objects' % (backupSnmpMethod, str(discoveredHostIpList.size()))) if (discoveredHostIpList.size()==0): Framework.reportWarning('Failed to discover SNMP IP data') return OSHVResult discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) discoveredHostArpList.addAll(SnmpQueries.getSnmpArpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, Boolean.TRUE, destination)) if (discoveredHostArpList.size()==0) and (snmpMethod != backupSnmpMethod): discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) discoveredHostArpList.addAll(SnmpQueries.getSnmpArpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, Boolean.TRUE, destination)) if (discoveredHostArpList.size()==0): Framework.reportWarning('Failed to discover SNMP ARP data') return OSHVResult networkOSH = None for i in range(discoveredHostArpList.size()): currArp = discoveredHostArpList.get(i) for currIp in discoveredHostIpList: if networkOSH is None: networkOSH = modeling.createNetworkOSH(currIp.netaddr, currIp.netmask) OSHVResult.add(networkOSH) if (currIp.domain == 'unknown') and not discoverUnknownIPs: continue if not netutils.isValidMac(currArp.designatedMacAddress): continue #report start designatedIpNetAddress = IPv4(currArp.designatedIpAddress, currIp.netmask).getFirstIp().toString(); if designatedIpNetAddress == currIp.netaddr: hostOSH = modeling.createHostOSH(currArp.designatedIpAddress) OSHVResult.add(hostOSH) OSHVResult.add(modeling.createLinkOSH('member', networkOSH, hostOSH)) ipOsh = modeling.createIpOSH(currArp.designatedIpAddress, currIp.netmask) OSHVResult.add(ipOsh) OSHVResult.add(modeling.createLinkOSH('member', networkOSH, ipOsh)) ifOsh = modeling.createInterfaceOSH(netutils.parseMac(currArp.designatedMacAddress), hostOSH) OSHVResult.add(ifOsh) OSHVResult.add(modeling.createLinkOSH('containment', ifOsh, ipOsh)) return OSHVResult
def getShortMask(netmask): """ Returns the count of set bit in given network mask e.g. for mask '255.255.255.0' return value will be 24 @type netmask: string @rtype: integer """ shortMask = 0 if IPv4.isValidIp(netmask): octets = netmask.split('.') for octet in octets: shortMask += getLeadingOnesCount(int(octet)) return shortMask
def isIpBroadcast(ipAddress, netmask): """ Checks whether the given IP is a broadcast IP @param ipAddress: IP address to check @type ipAddress: string @param netmask: corresponding IP network mask @type netmask: string @return: boolean """ bcast = None if ipAddress and netmask and isValidIp(ipAddress): netMask = parseNetMask(netmask) parsedIp = IPv4(ipAddress, netMask) if netMask != "255.255.255.255" and netMask != "255.255.255.254": broadcastIp = parsedIp.getLastIp() if parsedIp == broadcastIp: bcast = 1 return bcast
def isValidIp(ipAddr, filter_client_ip=None): """ @deprecated: this method only supports IPv4, use ip_addr.isValidIpAddress if you need IPv6 support Checks whether the given IP address is a valid IPv4 address. @param ipAddr: IP address to check @type ipAddr: string @return: true if the IP is valid IPv4 address, else false @rtype: Boolean """ if ipAddr and filter_client_ip and DOMAIN_SCOPE_MANAGER.isClientIp(ipAddr): return None # in some cases windows machines report such IP address for DHCP Server # because of misconfiguration if ipAddr and ipAddr.strip() == '255.255.255.255': return None if ipAddr and ipAddr.strip() == '0.0.0.0': return None return IPv4.isValidIp(ipAddr)
def createRouteObjects(routeList, ifList, ip_address, host_id): routeArrayList = [] routeObjects = ObjectStateHolderVector() for route in routeList: if route.ipRouteType and int(route.ipRouteType) != 4: continue if str(route.ipRouteNextHop).startswith('0.') or str(route.ipRouteNextHop).startswith('127.'): continue if route.ipRouteIfIndex == 0: #Host (next hop) nextHopHostOSH = __createRouterIncompleteHostByIp(route.ipRouteNextHop) #Ip (next hop) nextHopIpOSH = modeling.createIpOSH(route.ipRouteNextHop) routeObjects.add(nextHopHostOSH) routeObjects.add(nextHopIpOSH) currIf = getInterfaceByIndex(ifList, route.ipRouteIfIndex) if not currIf: continue if len(currIf.ipList) == 0 or currIf.ipList[0].netaddr == None: #Host (next hop) nextHopHostOSH = __createRouterIncompleteHostByIp(route.ipRouteNextHop) #Ip (next hop) nextHopIpOSH = modeling.createIpOSH(route.ipRouteNextHop) discoveredHostOSH = modeling.createOshByCmdbId("host", host_id) unnumberedLinkOSHHostHost = modeling.createLinkOSH("unnumbered", discoveredHostOSH, nextHopHostOSH) #Add the next hop and the link routeObjects.add(nextHopHostOSH) routeObjects.add(nextHopIpOSH) routeObjects.add(unnumberedLinkOSHHostHost) else: for ip in currIf.ipList: nextHopNetAddress = IPv4(route.ipRouteNextHop, ip.ipNetMask).getFirstIp().toString() if nextHopNetAddress != ip.netaddr: continue nextHopIpDomain = DomainScopeManager.getDomainByIp(route.ipRouteNextHop, ip.domain) routeFound = 0 for currRoute in routeArrayList: if currRoute['localIpAddress'] == ip.ipAddr and currRoute['localIpDomain'] == ip.domain and currRoute['nextHopIp'] == route.ipRouteNextHop and currRoute['nextHopIpDomain'] == nextHopIpDomain: currRoute['destinationList'].append(route.ipRouteDest) break routeFound += 1 if routeFound >= len(routeArrayList): currRoute = {} currRoute['destAddress'] = route.ipRouteDest currRoute['destinationList'] = [] currRoute['destinationList'].append(route.ipRouteDest) currRoute['ifIndex'] = route.ipRouteIfIndex currRoute['localIpAddress'] = ip.ipAddr currRoute['localIpDomain'] = ip.domain currRoute['localIpMask'] = ip.ipNetMask currRoute['localIpNetClass'] = ip.netclass currRoute['nextHopNetAddr'] = nextHopNetAddress currRoute['nextHopIp'] = route.ipRouteNextHop currRoute['nextHopIpDomain'] = DomainScopeManager.getDomainByIp(currRoute['nextHopIp'], currRoute['localIpDomain']) currRoute['type'] = route.ipRouteType currRoute['ifAdminStatus'] = currIf.ifAdminStatus routeArrayList.append(currRoute) for currRouteData in routeArrayList: #Ip (next hop) nextHopIpOSH = modeling.createIpOSH(currRouteData['nextHopIp'], currRouteData['localIpMask']) routeObjects.add(nextHopIpOSH) # Ip (local for link) localIpOSHForLink = modeling.createIpOSH(currRouteData['localIpAddress']) routeLinkOSHIpIp = modeling.createLinkOSH('route', localIpOSHForLink, nextHopIpOSH) for ipDest in currRouteData['destinationList']: routeLinkOSHIpIp.addAttributeToList(AttributeStateHolder("route_netaddress", ipDest)) # Network (for link) nextHopNetworkOSH = modeling.createNetworkOSH(currRouteData['nextHopNetAddr'], currRouteData['localIpMask']) nextHopHostOSH = __createRouterIncompleteHostByIp(currRouteData['nextHopIp']) #Member (Connecting the next hop host to the next hop network) memberLinkOSHHostNetwork = modeling.createLinkOSH('member', nextHopNetworkOSH, nextHopHostOSH) #Member (Connecting the next hop ip to the next hop network) memberLinkOSHIpNetwork = modeling.createLinkOSH('member', nextHopNetworkOSH, nextHopIpOSH) routeObjects.add(nextHopHostOSH) routeObjects.add(memberLinkOSHHostNetwork) routeObjects.add(memberLinkOSHIpNetwork) routeObjects.add(routeLinkOSHIpIp) return routeObjects
queryBuilder.addQueryElement(1, 'ipAddr') queryBuilder.addQueryElement(2, 'ipIfIndex') queryBuilder.addQueryElement(3, 'ipNetMask') ipListResult = snmpAgent.getSnmpData(queryBuilder) if (ipListResult == None) or (len(ipListResult) == 0): logger.warn('no data returned on query ', str(BASE_IP_OID)) else: for ip in ipListResult: if not isValidNetMask(str(ip.ipNetMask)): logger.warn('Received invalid netmask [', str(ip.ipNetMask),'] for ip ['+ ip.ipAddr +'], skipping') elif ip.ipAddr == None or len(ip.ipAddr) == 0: logger.warn('Received invalid ip: ' + ip.ipAddr + ', skipping') else: setattr(ip, 'domain', DomainScopeManager.getDomainByIp(ip.ipAddr, discoveredIpDomain)) setattr(ip, 'netaddr', str(IPv4(ip.ipAddr, ip.ipNetMask).getFirstIp())) ipv4 = IPv4("1.1.1.1", ip.ipNetMask) setattr(ip, 'netclass', ipv4.getIpClassName()) ipList.append(ip) return ipList def getInterfaceNameAndAlias(client, indx2if): logger.debug('Running ifname and ifalias') resultSet = client.executeQuery('1.3.6.1.2.1.31.1.1.1.1,1.3.6.1.2.1.31.1.1.1.2,string,1.3.6.1.2.1.31.1.1.1.18,string') table = resultSet.asTable() for rowIndex in range(len(table)): ifIndex = table[rowIndex][0] ifName = table[rowIndex][1] ifAlias = table[rowIndex][2]