def ipv4_subnet_calc(ip_address, subnet_mask, is_subnet_mask_in_bits):
    '''
    calculates the ipv4 subnet
    '''

    octets = ip_address.split('.')
    for octet in octets:
        print(octet)
    subnet_active_bits = utils.find_active_bits(subnet_mask,
                                                is_subnet_mask_in_bits)
    print(is_subnet_mask_in_bits)
    subnet_maxed_octets = utils.find_maxed_octets(subnet_mask,
                                                  is_subnet_mask_in_bits)
    print(subnet_maxed_octets)

    binary_of_active = utils.to_binary_from_active_bits(subnet_active_bits)
    binary_of_changing_octet = '0b' + bin(int(
        octets[subnet_maxed_octets]))[2:].zfill(8)
    binary_list_of_active = utils.convert_binary_string_to_list(
        binary_of_active)
    binary_list_of_changing_octet = utils.convert_binary_string_to_list(
        binary_of_changing_octet)

    new_binary_for_changing = '0b'
    for i in range(0, 8):
        if int(binary_list_of_active[i]) & int(
                binary_list_of_changing_octet[i]):
            new_binary_for_changing += '1'
        else:
            new_binary_for_changing += '0'
    changed_octet = utils.convert_binary_to_dec(new_binary_for_changing)

    new_ip = ''
    for octet in octets:
        if octets.index(octet) < subnet_maxed_octets:
            new_ip += octet + '.'
        elif octets.index(octet) == subnet_maxed_octets:
            new_ip += str(changed_octet)
        else:
            new_ip += '.0'
    return new_ip
Esempio n. 2
0
 def testFindMaxedOctetsNumberFormMid(self):
     result = utils.find_maxed_octets('255.255.192.0', False)
     self.assertEquals(result, 2)
Esempio n. 3
0
 def testFindMaxedOctetsNumberFormLow(self):
     result = utils.find_maxed_octets('192.0.0.0', False)
     self.assertEquals(result, 0)
Esempio n. 4
0
 def testFindMaxedOctetsBitFormMid(self):
     result = utils.find_maxed_octets('16', True)
     self.assertEquals(result, 2)
Esempio n. 5
0
 def testFindMaxedOctetsBitFormHigh(self):
     result = utils.find_maxed_octets('24', True)
     self.assertEquals(result, 3)