Beispiel #1
0
def getNetworkByIP(self,ip,section,server_key=False,cluster_id=False):
    networks = []
    version = ipcalc.IP(ip).version()
    if server_key:
        networks = [ key for key in self.getKey(section,server_key,False).keys() if not key=='hostname']
    elif cluster_id:
        for server in self.getListOfKeys(section,self.getKey(section,self.host_key_detect)+cluster_id,False):
            for key in server[0].keys():
                if not key=='hostname' and key not in networks:
                    networks.append(key)
    for network_id in networks:
        if version == 4 and self.has_option(network_id, 'netmask'):
            if ip in ipcalc.Network('%s/%s' % (self.get(network_id, 'network') ,ipcalc.netmask2int(self.get(network_id, 'netmask')))):
                return network_id
        elif version == 6 and self.has_option(network_id, 'prefix'):
            if ip in ipcalc.Network('%s/%s' % (self.get(network_id, 'network') ,self.get(network_id, 'prefix'))):
                return network_id
    return False
Beispiel #2
0
def networkSectionList(sysConfig):
    """
    Build network_sections Dict as:
    <Section network name> : <IP-calc obj for this network>
    """
    network_sections = {}
    for section in sysConfig.sections():
        if sysConfig.has_option(section, 'type') and sysConfig.get(section, 'type') == 'network':
            netsec = ipcalc.Network(sysConfig.get(section, 'network'))
            if netsec.v == 4:
                network_sections[section] =  ipcalc.Network('%s/%s' % (sysConfig.get(section, 'network'), ipcalc.netmask2int(sysConfig.get(section, 'netmask'))))
                delimiter = '.'
            elif netsec.v == 6:
                network_sections[section] =  ipcalc.Network('%s/%s' % (sysConfig.get(section, 'network') ,sysConfig.get(section, 'prefix')))
                delimiter = ':'
            else:
                raise Exception, 'networkSectionList: Failed to calc relevant network section for %s network' % section 
            sysConfig.set(section, 'version' , {'version' :str(netsec.v),'delimiter' : delimiter})          
    sysConfig.set('RUNNER', 'network_sections', network_sections)
    return network_sections