Allow MinerFactory to take a list of discrete IPs (#7)
This commit is contained in:
@@ -34,6 +34,8 @@ class MinerNetwork:
|
|||||||
|
|
||||||
if "-" in self.ip_addr:
|
if "-" in self.ip_addr:
|
||||||
self.network = MinerNetworkRange(self.ip_addr)
|
self.network = MinerNetworkRange(self.ip_addr)
|
||||||
|
elif isinstance(self.ip_addr, list):
|
||||||
|
self.network = MinerNetworkRange(self.ip_addr)
|
||||||
else:
|
else:
|
||||||
# if there is no IP address passed, default to 192.168.1.0
|
# if there is no IP address passed, default to 192.168.1.0
|
||||||
if not self.ip_addr:
|
if not self.ip_addr:
|
||||||
|
|||||||
@@ -1,30 +1,35 @@
|
|||||||
import ipaddress
|
import ipaddress
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
class MinerNetworkRange:
|
class MinerNetworkRange:
|
||||||
"""A MinerNetwork that takes a range of IP addresses.
|
"""A MinerNetwork that takes a range of IP addresses.
|
||||||
|
|
||||||
:param ip_range: A range of IP addresses to put in the network.
|
:param ip_range: A range of IP addresses to put in the network, or a list of IPs
|
||||||
Takes a string formatted as
|
Takes a string formatted as
|
||||||
{ip_range_1_start}-{ip_range_1_end}, {ip_range_2_start}-{ip_range_2_end}
|
{ip_range_1_start}-{ip_range_1_end}, {ip_range_2_start}-{ip_range_2_end}
|
||||||
|
Also takes a list of strings formatted as
|
||||||
|
[{ip1}, {ip2}, {ip3}, ...]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, ip_range: str):
|
def __init__(self, ip_range: Union[str, list]):
|
||||||
ip_ranges = ip_range.replace(" ", "").split(",")
|
|
||||||
self.host_ips = []
|
self.host_ips = []
|
||||||
for item in ip_ranges:
|
if isinstance(ip_range, str):
|
||||||
start, end = item.split("-")
|
ip_ranges = ip_range.replace(" ", "").split(",")
|
||||||
start_ip = ipaddress.ip_address(start)
|
for item in ip_ranges:
|
||||||
end_ip = ipaddress.ip_address(end)
|
start, end = item.split("-")
|
||||||
networks = ipaddress.summarize_address_range(start_ip, end_ip)
|
start_ip = ipaddress.ip_address(start)
|
||||||
for network in networks:
|
end_ip = ipaddress.ip_address(end)
|
||||||
self.host_ips.append(network.network_address)
|
networks = ipaddress.summarize_address_range(start_ip, end_ip)
|
||||||
for host in network.hosts():
|
for network in networks:
|
||||||
if host not in self.host_ips:
|
self.host_ips.append(network.network_address)
|
||||||
self.host_ips.append(host)
|
for host in network.hosts():
|
||||||
if network.broadcast_address not in self.host_ips:
|
if host not in self.host_ips:
|
||||||
self.host_ips.append(network.broadcast_address)
|
self.host_ips.append(host)
|
||||||
|
if network.broadcast_address not in self.host_ips:
|
||||||
|
self.host_ips.append(network.broadcast_address)
|
||||||
|
elif isinstance(ip_range, list):
|
||||||
|
self.host_ips = [ipaddress.ip_address(ip_str) for ip_str in ip_range]
|
||||||
|
|
||||||
def hosts(self):
|
def hosts(self):
|
||||||
for x in self.host_ips:
|
for x in self.host_ips:
|
||||||
|
|||||||
Reference in New Issue
Block a user