Added in CIDR, ranges, and glob ranges

This commit is contained in:
ProDigySML
2019-01-06 00:49:25 -08:00
parent 8eff132e8f
commit 8484ac64ba

View File

@@ -1,4 +1,5 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from netaddr import IPNetwork, IPRange, IPGlob
import os.path import os.path
@@ -18,25 +19,73 @@ class InputHelper(object):
return arg return arg
@staticmethod
def _get_ips_from_range(ip_range):
ips = set()
ip_range = ip_range.split("-")
# parsing the above structure into an array and then making into an IP address with the end value
end_ip = ".".join(ip_range[0].split(".")[0:-1]) + "." + ip_range[1]
# creating an IPRange object to get all IPs in between
range_obj = IPRange(ip_range[0], end_ip)
for ip in range_obj:
ips.add(str(ip))
return ips
@staticmethod
def _get_ips_from_glob(glob_ips):
ip_glob = IPGlob(glob_ips)
ips = set()
for ip in ip_glob:
ips.add(str(ip))
return ips
@staticmethod
def _get_cidr_to_ips(cidr_range):
ips = set()
for ip in IPNetwork(cidr_range):
ips.add(str(ip))
return ips
@staticmethod @staticmethod
def process_commands(arguments): def process_commands(arguments):
commands = set() commands = set()
ranges = set()
targets = set() targets = set()
final_commands = set() final_commands = set()
# process targets first # process targets first
if arguments.target: if arguments.target:
targets.add(arguments.target) ranges.add(arguments.target)
else: else:
for target in arguments.target_list: for target in arguments.target_list:
targets.add(target.strip()) ranges.add(target.strip())
# removing elements that may have spaces (helpful for easily processing comma notation)
for target in ranges:
target = target.replace(" ", "")
# todo: take list of targets and expand CIDR / comma notation # todo: take list of targets and expand CIDR / comma notation
if not arguments.nocidr: for ips in target.split(","):
# todo: expand CIDR from net addr # checking for CIDR
pass if not arguments.nocidr and "/" in ips:
targets.update(InputHelper._get_cidr_to_ips(ips))
# todo: expand comma notation # checking for IPs in a range
elif "-" in ips:
targets.update(InputHelper._get_ips_from_range(ips))
# checking for glob ranges
elif "*" in ips:
targets.update(InputHelper._get_ips_from_glob(ips))
else:
targets.add(ips)
if arguments.command: if arguments.command:
commands.add(arguments.command) commands.add(arguments.command)