added the ability to scan a range of IPs as part of the miner network by passing a string formatted as {ip_range_1_start}-{ip_range_1_end}, {ip_range_2_start}-{ip_range_2_end} to the miner network

This commit is contained in:
UpstreamData
2022-03-17 12:05:58 -06:00
parent 6d2e40c81d
commit d488c8458c
4 changed files with 49 additions and 18 deletions

View File

@@ -3,8 +3,8 @@ from settings import DEBUG
logging.basicConfig(
filename="logfile.txt",
filemode="a",
# filename="logfile.txt",
# filemode="a",
format='[%(levelname)s][%(asctime)s](%(name)s) - %(message)s',
datefmt='%x %X'
)

View File

@@ -1,5 +1,7 @@
import ipaddress
import asyncio
from network.net_range import MinerNetworkRange
from miners.miner_factory import MinerFactory
from settings import NETWORK_PING_RETRIES as PING_RETRIES, NETWORK_PING_TIMEOUT as PING_TIMEOUT, \
NETWORK_SCAN_THREADS as SCAN_THREADS
@@ -18,29 +20,32 @@ class MinerNetwork:
def __repr__(self):
return str(self.network)
def get_network(self) -> ipaddress.ip_network:
"""Get the network using the information passed to the MinerNetwork or from cache."""
# if we have a network cached already, use that
if self.network:
return self.network
# if there is no IP address passed, default to 192.168.1.0
if not self.ip_addr:
default_gateway = "192.168.1.0"
# if we do have an IP address passed, use that
if "-" in self.ip_addr:
print("getting network")
self.network = MinerNetworkRange(self.ip_addr)
else:
default_gateway = self.ip_addr
# if there is no IP address passed, default to 192.168.1.0
if not self.ip_addr:
default_gateway = "192.168.1.0"
# if we do have an IP address passed, use that
else:
default_gateway = self.ip_addr
# if there is no subnet mask passed, default to /24
if not self.mask:
subnet_mask = "24"
# if we do have a mask passed, use that
else:
subnet_mask = str(self.mask)
# if there is no subnet mask passed, default to /24
if not self.mask:
subnet_mask = "24"
# if we do have a mask passed, use that
else:
subnet_mask = str(self.mask)
# save the network and return it
self.network = ipaddress.ip_network(f"{default_gateway}/{subnet_mask}", strict=False)
# save the network and return it
self.network = ipaddress.ip_network(f"{default_gateway}/{subnet_mask}", strict=False)
return self.network
async def scan_network_for_miners(self) -> None or list:

26
network/net_range.py Normal file
View File

@@ -0,0 +1,26 @@
import ipaddress
class MinerNetworkRange:
"""A MinerNetwork that takes a range of IP addresses.
:param ip_range: A range of IP addresses to put in the network.
Takes a string formatted as
{ip_range_1_start}-{ip_range_1_end}, {ip_range_2_start}-{ip_range_2_end}
"""
def __init__(self, ip_range: str):
ip_ranges = ip_range.replace(" ", "").split(",")
self.host_ips = []
for item in ip_ranges:
start, end = item.split("-")
start_ip = ipaddress.ip_address(start)
end_ip = ipaddress.ip_address(end)
networks = ipaddress.summarize_address_range(start_ip, end_ip)
for network in networks:
for host in network.hosts():
self.host_ips.append(host)
def hosts(self):
for x in self.host_ips:
yield x

View File

@@ -16,5 +16,5 @@ whatsminer_pwd = "admin"
### DEBUG MODE ###
# change this to debug = true
# to enable debug mode.
debug = false
# debug = true
# debug = false
debug = true