(20250213) Small bug fix in the IPv4 range splitting function.

This commit is contained in:
2025-02-13 17:24:40 +05:30
parent b913f91fbd
commit 04fad8dc49
+49 -36
View File
@@ -290,9 +290,9 @@ class AsyncMikroTik:
# Calculate the total number of IPs in the range:
total_ips = int(end_ip_obj) - int(start_ip_obj) + 1
# Calculate the no. of IPs each VLAN gets,
# Calculate the no. of IPs each VLAN gets (subtract one because counting starts from 0),
# and add three (if asked) because we need IPs for network, gateway and broadcast:
ips_per_target = math.ceil(total_ips / target_count)
ips_per_target = math.ceil(total_ips / target_count) - 1
if consider_reserved_ips: ips_per_target += 3
# Generate subnets for each VLAN:
@@ -2327,9 +2327,10 @@ if __name__ == "__main__":
HS_DHCP_SERVER_NAME = "easyfi-hs-dhcp-" + HS_VLAN_ID
HS_SNMP_COMMUNITY_NAME = "lkjhgfdsa1234567"
HS_PUBLIC_IP_SUBNET = "154.84.218.42/25"
HS_FIRST_PUBLIC_IP = "154.84.218.0"
HS_LAST_PUBLIC_IP = "154.84.218.127"
HS_PUBLIC_IP_SUBNET = "102.210.173.150/26"
HS_FIRST_PUBLIC_IP = "102.210.173.128"
HS_LAST_PUBLIC_IP = "102.210.173.191"
HS_PRIVATE_IP_SUBNET = "100.65.0.0/22"
HS_FIRST_PRIVATE_IP = "100.65.0.0"
HS_LAST_PRIVATE_IP = "100.65.3.255"
@@ -2468,23 +2469,23 @@ if __name__ == "__main__":
# print("MESSAGE:", api_response.message)
# print("JSON:", api_response.data)
# print("\n\n")
# STEP 8:
# Add a new DHCP Network:
api_response = await my_mikrotik.add_dhcp_network(
json_payload = {
"address": HS_PRIVATE_IP_SUBNET,
"gateway": HS_GATEWAY_PRIVATE_IP,
"netmask": HS_PRIVATE_IP_SUBNET.split("/")[-1],
"dns-server": HS_GATEWAY_PRIVATE_IP,
"comment": HS_COMMENT
}
)
print("STEP 8:", api_response.success, f"({api_response.action})")
print("MESSAGE:", api_response.message)
print("JSON:", api_response.data)
print("\n\n")
#
# # STEP 8:
# # Add a new DHCP Network:
# api_response = await my_mikrotik.add_dhcp_network(
# json_payload = {
# "address": HS_PRIVATE_IP_SUBNET,
# "gateway": HS_GATEWAY_PRIVATE_IP,
# "netmask": HS_PRIVATE_IP_SUBNET.split("/")[-1],
# "dns-server": HS_GATEWAY_PRIVATE_IP,
# "comment": HS_COMMENT
# }
# )
# print("STEP 8:", api_response.success, f"({api_response.action})")
# print("MESSAGE:", api_response.message)
# print("JSON:", api_response.data)
# print("\n\n")
#
# # STEP 9:
# # Set up the Walled-Garden IP:
# api_response = await my_mikrotik.add_hotspot_walled_garden_ip(
@@ -2547,20 +2548,32 @@ if __name__ == "__main__":
# print("JSON:", api_response.data)
# print("\n\n")
#
# # STEP 13:
# # Map the private IPs to the public IPs for NAT-ing:
# public_ips = [
# str(ipaddress.IPv4Address(_))
# for _ in range(
# int(ipaddress.IPv4Address(HS_FIRST_PUBLIC_IP)),
# int(ipaddress.IPv4Address(HS_LAST_PUBLIC_IP)) + 1
# )
# ]
# nat_map = my_mikrotik.split_ipv4_range_in_powers_of_two(
# start_ip = HS_FIRST_PRIVATE_IP,
# end_ip = HS_LAST_PRIVATE_IP,
# targets = public_ips
# )
# STEP 13:
# Map the private IPs to the public IPs for NAT-ing:
private_ips = [
str(ipaddress.IPv4Address(_))
for _ in range(
int(ipaddress.IPv4Address(HS_FIRST_PRIVATE_IP)),
int(ipaddress.IPv4Address(HS_LAST_PRIVATE_IP)) + 1
)
]
public_ips = [
str(ipaddress.IPv4Address(_))
for _ in range(
int(ipaddress.IPv4Address(HS_FIRST_PUBLIC_IP)),
int(ipaddress.IPv4Address(HS_LAST_PUBLIC_IP)) + 1
)
]
nat_map = my_mikrotik.split_ipv4_range_in_powers_of_two(
start_ip = HS_FIRST_PRIVATE_IP,
end_ip = HS_LAST_PRIVATE_IP,
targets = public_ips,
consider_reserved_ips = False
)
print("NAT MAP:", json.to_string(nat_map, default=str))
print("PRIVATE IP COUNT:", len(private_ips))
print("PUBLIC IP COUNT:", len(public_ips))
print("RULE COUNT:", len(nat_map))
# for index, nat_rule in enumerate(nat_map):
# api_response = await my_mikrotik.add_firewall_nat(
# json_payload = {