def check_port_dhcp_assignment(self, leasesfile):
     """
     sets ``self._ip_addr_assign`` to ``1`` if port is
     a DHCP enabled interface
     """
     try:
         filehandler = open(leasesfile)
         lines = filehandler.read()
         filehandler.close()
     except IOError:
         return
     lines = re.sub('\n', '', lines)
     last_lease = re.split(r'lease\s*{', lines)[-1]
     _m0 = re.search(r'expire\s+\d+\s+([0-9/]+\s+[0-9:]+)', last_lease)
     if _m0:
         lease_expires_on = _m0.group(1)
     else:
         return
     fmt = '%Y/%m/%d %H:%M:%S'
     lease_expires_on = datetime.strptime(lease_expires_on, fmt)
     lease_valid = lease_expires_on > datetime.now()
     if lease_valid:
         regex = re.compile(r'fixed-address\s*([0-9.]+)' +
                            r'.*subnet-mask\s*([0-9.]+)')
         match = re.search(regex, last_lease)
         if match:
             ip_addr = match.group(1)
             smask = match.group(2)
             smask = common.netmask_dot_notation_to_cidr(smask)
             dhcpaddr = ip_addr + '/' + str(smask)
             if self.ip_address.allentries:
                 for i in self.ip_address.allentries:
                     if dhcpaddr == i:
                         self._ip_addr_assign = 1
                         return
 def check_port_dhcp_assignment(self, leasesfile):
     """
     sets ``self._ip_addr_assign`` to ``1`` if port is
     a DHCP enabled interface
     """
     try:
         filehandler = open(leasesfile)
         lines = filehandler.read()
         filehandler.close()
     except IOError:
         return
     lines = re.sub('\n', '', lines)
     last_lease = re.split(r'lease\s*{', lines)[-1]
     _m0 = re.search(r'expire\s+\d+\s+([0-9/]+\s+[0-9:]+)',
                     last_lease)
     if _m0:
         lease_expires_on = _m0.group(1)
     else:
         return
     fmt = '%Y/%m/%d %H:%M:%S'
     lease_expires_on = datetime.strptime(lease_expires_on, fmt)
     lease_valid = lease_expires_on > datetime.now()
     if lease_valid:
         regex = re.compile(r'fixed-address\s*([0-9.]+)' +
                            r'.*subnet-mask\s*([0-9.]+)')
         match = re.search(regex, last_lease)
         if match:
             ip_addr = match.group(1)
             smask = match.group(2)
             smask = common.netmask_dot_notation_to_cidr(smask)
             dhcpaddr = ip_addr + '/' + str(smask)
             if self.ip_address.allentries:
                 for i in self.ip_address.allentries:
                     if dhcpaddr == i:
                         self._ip_addr_assign = 1
                         return
Exemple #3
0
def test_block_to_cidr_netmask():
    "common - convert block netmask to cidr"
    ipv6mask = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe'
    assert_equals(common.netmask_dot_notation_to_cidr(ipv6mask), 127)
    ipv4mask = '255.255.254.0'
    assert_equals(common.netmask_dot_notation_to_cidr(ipv4mask), 23)